SpringBoot使用拦截器步骤 - 极悦

SpringBoot教程

SpringBoot使用拦截器步骤

项目名称:029-springboot-springmvc-interceptor

1.创建一个SpringBoot Web项目

pom.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <!--父工程-->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.5.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <!--当前项目GAV坐标-->
    <groupId>com.abc.springboot</groupId>
    <artifactId>029-springboot-springmvc-interceptor</artifactId>
    <version>1.0.0</version>
    
    <name>029-springboot-springmvc-interceptor</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <!--SpringBoot Web项目起步依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

2.实现一个登录拦截器

package com.abc.springboot.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * ClassName:LoginInterceptor
 * Package:com.abc.springboot.interceptor
 * Description:

 */
public class LoginInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        //编写业务逻辑
            //true:通过
            //false:被拦截

        System.out.println("-----登录拦截器-------");
        return true;
    }
}

3.通过配置类注册拦截器

在项目中创建一个config包,创建一个配置类InterceptorConfig,并实现WebMvcConfigurer接口, 覆盖接口中的addInterceptors方法,并为该配置类添加@Configuration注解,标注此类为一个配置类,让Spring Boot 扫描到,这里的操作就相当于SpringMVC的注册拦截器 ,@Configuration就相当于一个applicationContext-mvc.xml。

package com.abc.springboot.config;

import com.abc.springboot.interceptor.LoginInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * ClassName:InterceptorConfig
 * Package:com.abc.springboot.config
 * Description:

 */
@Configuration //注册一个拦截器,相当于applicationContext-mvc.xml配置
public class InterceptorConfig implements WebMvcConfigurer {

    @Override
    public void addInterceptors(InterceptorRegistry registry) {

        //定义需要拦截的路径
        String [] addPathPatterns = {
                "/springBoot/**"
        };

        //定义不需要拦截的路径
        String [] excludePathPatterns = {
                "/springBoot/login",
                "/springBoot/register"
        };

        registry.addInterceptor(new LoginInterceptor())
        .addPathPatterns(addPathPatterns)
        .excludePathPatterns(excludePathPatterns);
    }
}

4.浏览器访问测试是否拦截成功

技术文档推荐

更多>>

视频教程推荐

更多>>