当前位置 主页 > 网站技术 > 代码类 >

    SpringBoot拦截器实现登录拦截的方法示例

    栏目:代码类 时间:2019-09-17 11:02

    源码

    GitHub:https://github.com/291685399/springboot-learning/tree/master/springboot-interceptor01

    SpringBoot拦截器可以做什么

    可以对URL路径进行拦截,可以用于权限验证、解决乱码、操作日志记录、性能监控、异常处理等

    SpringBoot拦截器实现登录拦截

    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.0.0.RELEASE</version>    <relativePath/>  </parent>  <groupId>com.wyj</groupId>  <artifactId>springboot-interceptor01</artifactId>  <version>0.0.1-SNAPSHOT</version>  <name>springboot-interceptor01</name>  <description>springboot拦截器</description>  <properties>    <java.version>1.8</java.version>  </properties>  <dependencies>    <!-- springboot -->    <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>    <dependency>      <groupId>org.springframework.boot</groupId>      <artifactId>spring-boot-devtools</artifactId>      <scope>runtime</scope>      <optional>true</optional>    </dependency>    <!-- lombok -->    <dependency>      <groupId>org.projectlombok</groupId>      <artifactId>lombok</artifactId>      <optional>true</optional>    </dependency>    <!-- thymeleaf -->    <dependency>      <groupId>org.springframework.boot</groupId>      <artifactId>spring-boot-starter-thymeleaf</artifactId>    </dependency>  </dependencies>  <build>    <finalName>springboot-interceptor01</finalName>    <plugins>      <plugin>        <groupId>org.springframework.boot</groupId>        <artifactId>spring-boot-maven-plugin</artifactId>      </plugin>    </plugins>  </build></project>

    WebMvcConfigurer:继承WebMvcConfigurationSupport类,重写addInterceptors方法

    /** * 在springboot2.0.0之后,WebMvcConfigurerAdapter已经过时了 * 会使用WebMvcConfigurer或者WebMvcConfigurationSupport替代 * * @author wyj * @create 2019-06-01 21:48 */@Configurationpublic class WebMvcConfigurer extends WebMvcConfigurationSupport {  /**   * 在springboot2.0.0之前继承WebMvcConfigurerAdapter类,重写addInterceptors方法   *   * @param registry   *///  @Override//  public void addInterceptors(InterceptorRegistry registry) {//    /**//     * 拦截器按照顺序执行,如果不同拦截器拦截存在相同的URL,前面的拦截器会执行,后面的拦截器将不执行//     *///    registry.addInterceptor(new AuthorityInterceptor())//        .addPathPatterns("/user/**");//    super.addInterceptors(registry);//  }  /**   * 在springboot2.0.0之后实现WebMvcConfigurer接口,重写addInterceptors方法   *   * @param registry   *///  @Override//  public void addInterceptors(InterceptorRegistry registry) {//    /**//     * 拦截器按照顺序执行,如果不同拦截器拦截存在相同的URL,前面的拦截器会执行,后面的拦截器将不执行//     *///    registry.addInterceptor(new AuthorityInterceptor())//        .addPathPatterns("/user/**");//  }  /**   * 在springboot2.0.0之后继承WebMvcConfigurationSupport类,重写addInterceptors方法   *   * @param registry   */  @Override  protected void addInterceptors(InterceptorRegistry registry) {    /**     * 拦截器按照顺序执行,如果不同拦截器拦截存在相同的URL,前面的拦截器会执行,后面的拦截器将不执行     */    registry.addInterceptor(new AuthorityInterceptor())        .addPathPatterns("/user/**");    super.addInterceptors(registry);  }}