基础
自定义拦截器必须实现HandlerInterceptor接口:
①preHandle方法:该方法在业务处理器处理请求之前被调用,在该方法中对用户request请求进行处理;如果程序员决定该拦截器对请求进行拦截处理后还要继续调用其他拦截器,或者继续让业务处理器去处理,则返回true,否则返回false;可以处理权限这块;
②postHandle方法:该方法在业务处理器处理完请求后,DispatcherServlet向客户端返回响应之前被调用,可以对用户请求request进行处理;
③afterCompletion方法:该方法在DispatcherServlet完全处理完请求后被调用,可以在该方法中进行一些资源清理操作;
配置拦截器
自定义拦截器之后,注册到<mvc:interceptors/>中即可;
<mvc:interceptors>
<bean/>配置自定义拦截器,默认拦截所有的URL
<mvc:interceptor/>配置拦截器所要拦截/不拦截的URL路径
</mvc:interceptors>
拦截器执行顺序
①preHandle:按照拦截器配置的顺序执行,下面源码部分如下:
for(int i = 0; i < interceptors.length; this.interceptorIndex = i++),这里i是从0,递减到最大值;
②postHandle、afterCompletion:按照拦截器配置的反序执行,下面源码部分如下:
for(int i = interceptors.length - 1; i >= 0; --i)
for(int i = this.interceptorIndex; i >= 0; --i)
这里i是从最大值,递减到0;
源码分析
DispatcherServlet.doDispatch关键代码:
if (!mappedHandler.applyPreHandle(processedRequest, response)) {这里调用preHandle方法 return; } mv = ha.handle(processedRequest, response, mappedHandler.getHandler()); if (asyncManager.isConcurrentHandlingStarted()) { return; } this.applyDefaultViewName(processedRequest, mv); mappedHandler.applyPostHandle(processedRequest, response, mv);这里调用postHandle方法 this.processDispatchResult(processedRequest, response, mappedHandler, mv, (Exception)dispatchException);这里调用afterCompletion方法 |
HandlerExecutionChain.applyPreHandle关键代码:
boolean applyPreHandle(HttpServletRequest request, HttpServletResponse response) throws Exception { HandlerInterceptor[] interceptors = this.getInterceptors(); if (!ObjectUtils.isEmpty(interceptors)) { for(int i = 0; i < interceptors.length; this.interceptorIndex = i++) { HandlerInterceptor interceptor = interceptors[i]; if (!interceptor.preHandle(request, response, this.handler)) { this.triggerAfterCompletion(request, response, (Exception)null); return false; } } } return true; }
void applyPostHandle(HttpServletRequest request, HttpServletResponse response, @Nullable ModelAndView mv) throws Exception { HandlerInterceptor[] interceptors = this.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); } } } |
DispatcherServlet.processDispatchResult关键代码:
private void processDispatchResult(HttpServletRequest request, HttpServletResponse response, @Nullable HandlerExecutionChain mappedHandler, @Nullable ModelAndView mv, @Nullable Exception exception) throws Exception { boolean errorView = false; if (exception != null) { if (exception instanceof ModelAndViewDefiningException) { this.logger.debug("ModelAndViewDefiningException encountered", exception); mv = ((ModelAndViewDefiningException)exception).getModelAndView(); } else { Object handler = mappedHandler != null ? mappedHandler.getHandler() : null; mv = this.processHandlerException(request, response, handler, exception); errorView = mv != null; } }
if (mv != null && !mv.wasCleared()) { this.render(mv, request, response); if (errorView) { WebUtils.clearErrorRequestAttributes(request); } } else if (this.logger.isTraceEnabled()) { this.logger.trace("No view rendering, null ModelAndView returned."); }
if (!WebAsyncUtils.getAsyncManager(request).isConcurrentHandlingStarted()) { if (mappedHandler != null) { mappedHandler.triggerAfterCompletion(request, response, (Exception)null); }
} } |
HandlerExecutionChain.triggerAfterCompletion关键代码:
void triggerAfterCompletion(HttpServletRequest request, HttpServletResponse response, @Nullable Exception ex) throws Exception { HandlerInterceptor[] interceptors = this.getInterceptors(); if (!ObjectUtils.isEmpty(interceptors)) { for(int i = this.interceptorIndex; i >= 0; --i) { HandlerInterceptor interceptor = interceptors[i];
try { interceptor.afterCompletion(request, response, this.handler, ex); } catch (Throwable var8) { logger.error("HandlerInterceptor.afterCompletion threw exception", var8); } } } } |