方式一:不推荐,在代码中添加路径
1、写一个拦截器,继承HandlerInterceptor类
importorg.springframework.stereotype.Component;importorg.springframework.web.servlet.HandlerInterceptor;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;
@Componentpublic class ConfigPathInterceptor implementsHandlerInterceptor {
@Overridepublic boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throwsException {
System.out.println("执行了拦截器");if(request.getMethod().equalsIgnoreCase("GET")){return true;
}return false;
}
}
2、将拦截器添加至拦截器配置中
importorg.springframework.context.annotation.Bean;importorg.springframework.context.annotation.Configuration;importorg.springframework.web.servlet.config.annotation.InterceptorRegistry;importorg.springframework.web.servlet.config.annotation.WebMvcConfigurer;importjava.util.ArrayList;importjava.util.List;
@Configurationpublic class InterceptorConfig implementsWebMvcConfigurer {
@Overridepublic voidaddInterceptors(InterceptorRegistry registry) {
List includePathLists= new ArrayList<>();
includePathLists.add("/showUser");
List excludePathLists= new ArrayList<>();
excludePathLists.add("/mybatisPlus");//registry.addInterceptor(authenticationInterceptor())//.addPathPatterns("/**");//拦截所有请求,通过判断是否有 @LoginRequired 注解 决定是否需要登录
registry.addInterceptor(configPathInterceptor())
.excludePathPatterns(excludePathLists)//不拦截
.addPathPatterns(includePathLists); //拦截
}
@BeanpublicAuthenticationInterceptor authenticationInterceptor() {return newAuthenticationInterceptor();
}
@BeanpublicConfigPathInterceptor configPathInterceptor() {return newConfigPathInterceptor();
}
}
这样就完成了拦截器
方式二:推荐,在配置文件中添加路径
1、在application.yml文件中添加路径
#默认使用配置
spring:
profiles:
active: @[email protected]
redis:
host:127.0.0.1port:6379password:
lettuce:
pool:
max-active: 100max-idle: 10max-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
importorg.springframework.stereotype.Component;importorg.springframework.web.servlet.HandlerInterceptor;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;
@Componentpublic class ConfigPathInterceptor implementsHandlerInterceptor {
@Overridepublic boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throwsException {
System.out.println("执行了拦截器");if(request.getMethod().equalsIgnoreCase("GET")){return true;
}return false;
}
}
3、将拦截器添加到配置中
importorg.springframework.boot.context.properties.ConfigurationProperties;importorg.springframework.context.annotation.Bean;importorg.springframework.context.annotation.Configuration;importorg.springframework.context.annotation.Profile;importorg.springframework.web.servlet.config.annotation.InterceptorRegistry;importorg.springframework.web.servlet.config.annotation.WebMvcConfigurer;importjava.util.ArrayList;importjava.util.List;
@Configuration
@Profile({"prod","test"}) //只有测试和生产环境,拦截器才启作用
@ConfigurationProperties(prefix = "config.path") //读取配置文件
public class InterceptorConfig implementsWebMvcConfigurer {private List normal = new ArrayList<>();private List special = new ArrayList<>();private List exclude = new ArrayList<>();public void setExclude(Listexclude) {this.exclude =exclude;
}public void setNormal(Listnormal) {this.normal =normal;
}public void setSpecial(Listspecial) {this.special =special;
}
@Overridepublic voidaddInterceptors(InterceptorRegistry registry) {//registry.addInterceptor(authenticationInterceptor())//.addPathPatterns("/**");//拦截所有请求,通过判断是否有 @LoginRequired 注解 决定是否需要登录
registry.addInterceptor(configPathInterceptor())
.excludePathPatterns(exclude)//不拦截
.addPathPatterns(special); //拦截
}
@BeanpublicAuthenticationInterceptor authenticationInterceptor() {return newAuthenticationInterceptor();
}
@BeanpublicConfigPathInterceptor configPathInterceptor() {return newConfigPathInterceptor();
}
}
原文:https://www.cnblogs.com/ywjfx/p/11328171.html