拦截器配置
@Configuration
public class InterceptorConfiguration implements WebMvcConfigurer {
/**
* 配置跨域信息
* @param corsRegistry
*/
@Override
public void addCorsMappings(CorsRegistry corsRegistry){
}
//加一个@Bean让它先注入,关键就是这里
@Bean
public Interceptor getInterceptor(){
return new Interceptor();//得到自定义的一个拦截器
}
/**
* 需要拦截与不拦截路径
* @param registry
*/
@Override
public void addInterceptors(InterceptorRegistry registry){
//注册拦截器
InterceptorRegistration interceptorRegistration = registry.addInterceptor(getInterceptor());没有改动前:new Interceptor()//
//拦截的路径
interceptorRegistration.addPathPatterns("/**");
//不需要拦截的路径
interceptorRegistration.excludePathPatterns(
"/UserController/verificationCode/{phone}",
"/UserController/getLogin",
"/doc.html",
"classpath:/META-INF/resources/",
"/error",
"/webjars/**",
"/swagger-resources");
}
拦截器的类:
public class Interceptor implements HandlerInterceptor {
@Autowired
private RedisTemplate redisTemplate;
/**
* 在调用controller之前调用拦截
* @return
*/
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response,Object handler){
try {
System.out.println("拦截已启动"+request.getRequestURI());
ValueOperations valueOperations = redisTemplate.opsForValue();//这样,redis就不会报null
不懂的留言