如何用iis做网站,电子商务网站建设的论文,我赢网提供的高水平网页设计师,公司网站建设好大家好#xff0c;我是烤鸭#xff1a;
今天遇到一个拦截器失效的问题#xff0c;具体看源码分析下。
环境: springboot 2.x spring 5.x
1. 先说下业务场景
需求是对请求进入时和离开时对和线程id绑定#xff0c;用的Threadlocal#xff0c;现在有一个问题#xff0…大家好我是烤鸭
今天遇到一个拦截器失效的问题具体看源码分析下。
环境: springboot 2.x spring 5.x
1. 先说下业务场景
需求是对请求进入时和离开时对和线程id绑定用的Threadlocal现在有一个问题利用拦截器的方式不生效。
2. 拦截器创建的几种方式
2.1 extends WebMvcConfigurationSupport
Configuration
public class WebMvcAutoConfigurationAdapter extends WebMvcConfigurationSupport {AutowiredMapiHttpRequestInterceptor mapiHttpRequestInterceptor;Overridepublic void addInterceptors(InterceptorRegistry registry) {InterceptorRegistration addInterceptor registry.addInterceptor(mapiHttpRequestInterceptor);// 拦截配置addInterceptor.addPathPatterns(/**);}
}Component
class MapiHttpRequestInterceptor implements HandlerInterceptor{
}
2.2 implements WebMvcConfigurer
Configuration
public class WebMvcAutoConfiguration3Adapter implements WebMvcConfigurer {AutowiredMapiHttpRequest3Interceptor mapiHttpRequestInterceptor;Overridepublic void addInterceptors(InterceptorRegistry registry) {InterceptorRegistration addInterceptor registry.addInterceptor(mapiHttpRequestInterceptor);// 拦截配置addInterceptor.addPathPatterns(/**);}
}Component
class MapiHttpRequest3Interceptor implements HandlerInterceptor{}
先说下结论如果项目中出现了一次 extends WebMvcConfigurationSupport 其他的 extends WebMvcConfigurationSupport 和 implements WebMvcConfigurer 会失效 。
3. 看下源码为啥呢
先看下 WebMvcConfigurationSupport 这个类 addInterceptors 这个方法默认继承的是 DelegatingWebMvcConfiguration这个类就是获取 所有 实现 WebMvcConfigurer 的子类调用他们的方法如果有多个 通过实现 WebMvcConfigurer 创建的拦截器是都可以生效的。 那多个 继承 WebMvcConfigurationSupport 为啥只有一个生效呢答案在这个类WebMvcAutoConfiguration 的 ConditionalOnMissingBean 注解只实例化一个Bean多个继承也只有一个生效。 再看下 addInterceptors 啥时候触发的获取拦截器的时候获取过就不再获取了所以 addInterceptors 在项目启动触发才有效。而 getInterceptors 这个方法是在 handerMapping映射的时候触发的(比如 RequestMappingHandlerMapping、BeanNameUrlHandlerMapping)。 4. 解决方案
针对不同的场景解决方案也不一样我想到的有3个方案。
不继承 WebMvcConfigurationSupport 拦截器全部通过实现 WebMvcConfigurer 接口推荐只继承一次 WebMvcConfigurationSupport ,在这个类管理所有的拦截器(不推荐耦合性太高)针对我的场景我通过 过滤器实现的。注入的代码就不贴了before 和 fater 方法实现了类似拦截器的 preHandle 和 afterCompletion。有一点需要注意的是指定过滤器的排序(默认已经是最高了可以忽略)由于过滤器是链式调用如果想当拦截器用必须指定最先加载还有就是过滤器会拦截静态资源做好对静态资源的放行。
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {if (servletRequest instanceof HttpServletRequest) {HttpServletRequest request (HttpServletRequest)servletRequest;this.before(request);filterChain.doFilter(servletRequest, servletResponse);this.after();}
}
Bean
public FilterRegistrationBean mapiXxxFilterRegister() {FilterRegistrationBean filterRegistrationBean new FilterRegistrationBean();filterRegistrationBean.setOrder(2147483647);filterRegistrationBean.setFilter(this.mapiXxxFilter);filterRegistrationBean.setName(filter-xxx);filterRegistrationBean.addUrlPatterns(new String[]{/*});return filterRegistrationBean;
}
这两篇文章也是类似的问题大家也可以看下
https://www.lyscms.info/blog/detail/33A55BEE2FD94E66B40990EA4967D3F7
https://blog.csdn.net/pengdandezhi/article/details/81182701