springboot之路径拦截器

方式一:不推荐,在代码中添加路径

  1. 写一个拦截器,继承HandlerInterceptor类

复制代码

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
public class ConfigPathInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("执行了拦截器");
        if(request.getMethod().equalsIgnoreCase("GET")){
            return true;
        }

        return false;
    }
}

复制代码

  1. 将拦截器添加至拦截器配置中

复制代码

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import java.util.ArrayList;
import java.util.List;

@Configuration
public class InterceptorConfig implements WebMvcConfigurer {

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        List<String> includePathLists= new ArrayList<>();
        includePathLists.add("/showUser");
        List<String> excludePathLists= new ArrayList<>();
        excludePathLists.add("/mybatisPlus");
//        registry.addInterceptor(authenticationInterceptor())
//                .addPathPatterns("/**");    // 拦截所有请求,通过判断是否有 @LoginRequired 注解 决定是否需要登录
        registry.addInterceptor(configPathInterceptor())
                .excludePathPatterns(excludePathLists)   //  不拦截
                .addPathPatterns(includePathLists); //  拦截
    }
    @Bean
    public AuthenticationInterceptor authenticationInterceptor() {
        return new AuthenticationInterceptor();
    }

    @Bean
    public ConfigPathInterceptor configPathInterceptor() {
        return new ConfigPathInterceptor();
    }
}

复制代码

这样就完成了拦截器

方式二:推荐,在配置文件中添加路径

  1. 在application.yml文件中添加路径

复制代码

#默认使用配置
spring:
  profiles:
    active: @activatedProperties@
  redis:
    host: 127.0.0.1
    port: 6379
    password:
    lettuce:
      pool:
        max-active: 100
        max-idle: 10
        max-wait: 100000

#公共配置与profiles选择无关
mybatis-plus:
  typeAliasesPackage: com.cn.commodity.entity
  mapperLocations: classpath:mapper/*.xml

logging:
  level:
    com.cn.commodity.dao : debug

config:
  path:
    #该路径下GET请求放行,其他拦截
    normal:

    #该路径下任何类型请求均拦截
    special:
      - /showUser
    #该路径下任何请求均放行
    exclude:
      - /mybatisPlus

复制代码

  1. 创建拦截器ConfigPathInterceptor继承HandlerInterceptor

复制代码

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
public class ConfigPathInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("执行了拦截器");
        if(request.getMethod().equalsIgnoreCase("GET")){
            return true;
        }
        return false;
    }
}

复制代码

  1. 将拦截器添加到配置中

复制代码

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import java.util.ArrayList;
import java.util.List;

@Configuration
@Profile({"prod","test"})  //只有测试和生产环境,拦截器才启作用
@ConfigurationProperties(prefix = "config.path")  //读取配置文件
public class InterceptorConfig implements WebMvcConfigurer {

    private List<String> normal = new ArrayList<>();

    private List<String> special = new ArrayList<>();

    private List<String> exclude = new ArrayList<>();

    public void setExclude(List<String> exclude) {
        this.exclude = exclude;
    }

    public void setNormal(List<String> normal) {
        this.normal = normal;
    }

    public void setSpecial(List<String> special) {
        this.special = special;
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
//        registry.addInterceptor(authenticationInterceptor())
//                .addPathPatterns("/**");    // 拦截所有请求,通过判断是否有 @LoginRequired 注解 决定是否需要登录
        registry.addInterceptor(configPathInterceptor())
                .excludePathPatterns(exclude)   //  不拦截
                .addPathPatterns(special); //  拦截

    }
    @Bean
    public AuthenticationInterceptor authenticationInterceptor() {
        return new AuthenticationInterceptor();
    }

    @Bean
    public ConfigPathInterceptor configPathInterceptor() {
        return new ConfigPathInterceptor();
    }
}

复制代码


🐞标题:springboot之路径拦截器
👽作者:ruige
🐾地址:https://jjdhhc.com/articles/2020/11/07/1604720266008.html
🙏感恩:谢谢您的打赏与支持!中间图片是我的微信公众号,扫码关注哦!
支付宝支付 微信公众号 微信支付