WebMvcConfigurer添加多个拦截器的拦截路径问题
2021/5/8 10:25:16
本文主要是介绍WebMvcConfigurer添加多个拦截器的拦截路径问题,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
结论:每个拦截器的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; }
这篇关于WebMvcConfigurer添加多个拦截器的拦截路径问题的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-26MATLAB 中 A(7)=[];什么意思?-icode9专业技术文章分享
- 2024-11-26UniApp 中如何实现使用输入法时保持页面列表不动的效果?-icode9专业技术文章分享
- 2024-11-26在 UniApp 中怎么实现输入法弹出时禁止页面向上滚动?-icode9专业技术文章分享
- 2024-11-26WebSocket是什么,怎么使用?-icode9专业技术文章分享
- 2024-11-26页面有多个ref 要动态传入怎么实现?-icode9专业技术文章分享
- 2024-11-26在 UniApp 中实现一个底部输入框的常见方法有哪些?-icode9专业技术文章分享
- 2024-11-26RocketMQ入门指南:搭建与使用全流程详解
- 2024-11-26RocketMQ入门教程:轻松搭建与使用指南
- 2024-11-26手写RocketMQ:从入门到实践的简单教程
- 2024-11-25【机器学习(二)】分类和回归任务-决策树(Decision Tree,DT)算法-Sentosa_DSML社区版