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;
    }
}

2、将拦截器添加至拦截器配置中

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

2、创建拦截器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;
    }
}

3、将拦截器添加到配置中

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();
    }
}

 

posted @ 2019-08-09 16:37  小白啊小白,Fighting  阅读(9347)  评论(0编辑  收藏  举报