WebMvcConfigurer:拦截器的注册类
HandlerInterceptorAdapter:拦截组件
拦截组件HandlerInterceptorAdapter可以有多个,需要注册到WebMvcConfigurer里面,在WebMvcConfigurer里面拦截器是按顺序执行的。
@SpringBootConfiguration
public class InterceptorConfig implements WebMvcConfigurer {
/** 拦截器配置,与业务相关,有着强烈的前后依赖顺序
请不要在datasourceInterceptor这个拦截器之后再添加,这个拦截器preHandler是进入业务代码最后一个拦截器
@Override
public void addInterceptors(InterceptorRegistry registry){
WebMvcConfigurer.super.addInterceptors(registry);
registry.addInterceptor(loggerInterceptor());
registry.addInterceptor(authenticateInterceptor());
registry.addInterceptor(datasourceInterceptor());
}
//日志拦截
@Bean(“loggerInterceptor”)
public LoggerInterceptor loggerInterceptor(){
LoggerInterceptor interceptor = new LoggerInterceptor ();
return interceptor;
}
//认证拦截,安全校验相关的拦截操作
@Bean(“authenticateInterceptor”)
public AuthenticateInterceptor authenticateInteceptor(){
AuthenticateInterceptor interceptor =new AuthenticateInterceptor();
return interceptor;
}
}
public class AuthenticateInterceptor implements HandlerInterceptor {
//无需拦截的url
private HashSet
public AuthenticateInterceptor (){
set .add(“/test/login.do”);
…….
}
@Override
public boolean preHandle(HttpServletRequest request,HttpServletResponse response,Object handler) throw Exception{
//具体拦截业务
}
}