标签:拦截器 addInterceptor ------------- WebMvcConfigurer new 拦截 public interceptorRegis
结论:每个拦截器的addPathPatterns,excludePathPatterns添加的路径是各自独立的,如果添加的一个拦截器没有addPathPattern任何一个url则默认拦截所有请求,如果没有excludePathPatterns任何一个请求,则默认不放过任何一个请求。
验证过程:
两个拦截器:
public class TestInterceptor1 implements HandlerInterceptor { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { response.getOutputStream().write("To TestInterceptor1...".getBytes()); return true; } } public class TestInterceptor2 implements HandlerInterceptor { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { response.getOutputStream().write("To TestInterceptor2...".getBytes()); return true; } }
情景一:两个拦截器的拦截路径不同
@Configuration public class WebMvcConfig implements WebMvcConfigurer { @Override public void addInterceptors(InterceptorRegistry interceptorRegistry){ interceptorRegistry.addInterceptor(new TestInterceptor1()).addPathPatterns("/ready"); interceptorRegistry.addInterceptor(new TestInterceptor2()).addPathPatterns("/health"); } }
-------------测试结果-------------
情景二 :其中一个拦截器不配置拦截路径
@Configuration public class WebMvcConfig implements WebMvcConfigurer { @Override public void addInterceptors(InterceptorRegistry interceptorRegistry){ interceptorRegistry.addInterceptor(new TestInterceptor1()).addPathPatterns("/ready"); interceptorRegistry.addInterceptor(new TestInterceptor2()); } }
-------------测试结果-------------
情景三:
@Configuration public class WebMvcConfig implements WebMvcConfigurer { @Override public void addInterceptors(InterceptorRegistry interceptorRegistry){ interceptorRegistry.addInterceptor(new TestInterceptor1()); interceptorRegistry.addInterceptor(new TestInterceptor2()).addPathPatterns("/health"); } }
-------------测试结果-------------
情景四:其中一个拦截器的拦截路径在另一个拦截器的不拦截路径内
@Configuration public class WebMvcConfig implements WebMvcConfigurer { @Override public void addInterceptors(InterceptorRegistry interceptorRegistry){ interceptorRegistry.addInterceptor(new TestInterceptor1()).addPathPatterns("/ready"); interceptorRegistry.addInterceptor(new TestInterceptor2()).excludePathPatterns("/ready"); } }
-------------测试结果-------------
从源码上看每个拦截器interceptor的includePatterns和excludePatterns也确实是独立的,可以参考下
public InterceptorRegistration addInterceptor(HandlerInterceptor interceptor) { InterceptorRegistration registration = new InterceptorRegistration(interceptor); this.registrations.add(registration); return registration; } public InterceptorRegistration addPathPatterns(List<String> patterns) { this.includePatterns.addAll(patterns); return this; } public InterceptorRegistration excludePathPatterns(List<String> patterns) { this.excludePatterns.addAll(patterns); return this; }
标签:拦截器,addInterceptor,-------------,WebMvcConfigurer,new,拦截,public,interceptorRegis 来源: https://www.cnblogs.com/aland-1415/p/14343975.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。