文章插图
图片
- Spring中的拦截器(Interceptor) 用于拦截控制器方法的执行,可以在方法执行前后添加自定义逻辑类似于AOP编程思想 。
- Inteceptor的作用时机是在请求(request)进入servlet后,在进入Controller之前进行预处理 。
- Inteceptor的实际应用包括:认证授权、日志记录、字符编码转换,敏感词过滤等等 。
- Inteceptor中责任链的实现会从处理器的介绍,责任链的构建以及责任链的执行三个角度进行阐述 。
public interface HandlerInterceptor {boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception;void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception;void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception;} @Componentpublic class TimeInterceptor extends HandlerInterceptorAdapter {@Overridepublic boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {// 前置处理System.out.println("time interceptor preHandle");return true;}@Overridepublic void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {// 后置处理System.out.println("time interceptor postHandle");}@Overridepublic void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {System.out.println("time interceptor afterCompletion");}}
说明:- 处理器Interceptor的接口HandlerInterceptor定义了三个方法,可在控制器方法执行前后添加自定义逻辑 。
- 自定义处理器如上的TimeInterceptor需要自定义实现上述3个方法实现自我的逻辑 。
- 所有的自定义处理会串联在HandlerExecutionChain类实现的责任链上 。
public class HandlerExecutionChain {private final Object handler;private HandlerInterceptor[] interceptors;private List<HandlerInterceptor> interceptorList;private int interceptorIndex = -1;public void addInterceptor(HandlerInterceptor interceptor) {// 添加拦截器initInterceptorList().add(interceptor);}public void addInterceptors(HandlerInterceptor... interceptors) {if (!ObjectUtils.isEmpty(interceptors)) {CollectionUtils.mergeArrayIntoCollection(interceptors, initInterceptorList());}}private List<HandlerInterceptor> initInterceptorList() {if (this.interceptorList == null) {this.interceptorList = new ArrayList<HandlerInterceptor>();if (this.interceptors != null) {// An interceptor array specified through the constructorCollectionUtils.mergeArrayIntoCollection(this.interceptors, this.interceptorList);}}this.interceptors = null;return this.interceptorList;}}
说明:- HandlerExecutionChain类作为串联Interceptor处理器的责任链负责责任链的构建和执行 。
- HandlerExecutionChain类通过集合对象interceptorList保存所有相关的处理器对象 。
public class DispatcherServlet extends FrameworkServlet {protected void doDispatch(HttpServletRequest request, HttpServletResponse response) throws Exception {try {try {// mappedHandler代表的是HandlerExecutionChain责任链mappedHandler = getHandler(processedRequest);HandlerAdapter ha = getHandlerAdapter(mappedHandler.getHandler());// 1、执行mappedHandler的applyPreHandle方法if (!mappedHandler.applyPreHandle(processedRequest, response)) {return;}// 2、执行controller的执行逻辑mv = ha.handle(processedRequest, response, mappedHandler.getHandler());if (asyncManager.isConcurrentHandlingStarted()) {return;}applyDefaultViewName(processedRequest, mv);// 执行mappedHandler的applyPostHandle方法mappedHandler.applyPostHandle(processedRequest, response, mv);}catch (Exception ex) {}processDispatchResult(processedRequest, response, mappedHandler, mv, dispatchException);}catch (Exception ex) {}finally {}}}public class HandlerExecutionChain {private final Object handler;private HandlerInterceptor[] interceptors;private List<HandlerInterceptor> interceptorList;private int interceptorIndex = -1;boolean applyPreHandle(HttpServletRequest request, HttpServletResponse response) throws Exception {HandlerInterceptor[] interceptors = getInterceptors();if (!ObjectUtils.isEmpty(interceptors)) {// 责任链从前往后的顺序执行for (int i = 0; i < interceptors.length; i++) {HandlerInterceptor interceptor = interceptors[i];if (!interceptor.preHandle(request, response, this.handler)) {triggerAfterCompletion(request, response, null);return false;}this.interceptorIndex = i;}}return true;}void applyPostHandle(HttpServletRequest request, HttpServletResponse response, ModelAndView mv) throws Exception {HandlerInterceptor[] interceptors = getInterceptors();if (!ObjectUtils.isEmpty(interceptors)) {// 责任链从后往前的顺序执行for (int i = interceptors.length - 1; i >= 0; i--) {HandlerInterceptor interceptor = interceptors[i];interceptor.postHandle(request, response, this.handler, mv);}}}}
推荐阅读
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- CORS 跨域资源共享在Spring Boot中的实现
- Java面试题之SpringBoot 框架
- 一文读懂Spring框架中的不同类型事件
- RouterSploit:一款功能强大的嵌入式设备渗透测试框架
- 校友眼中的李雪琴是个“怪人”,内耗严重,父母离婚是病因!
- 杨梅核能吞吗?杨梅核吞入胃中的好处
- 杭州电子厂老板,25年来待客只用“假茅台”,行家:高手中的高手
- 筱在名字中的寓意是什么
- 薛平贵中的西凉是哪里 西凉是哪里
- 祭祀之礼是指 祭祀之礼是指五礼中的