拦截器
SpringMVC中的拦截器用于拦截控制器方法的执行
SpringMVC中的拦截器需要实现HandlerInterceptor
# 浏览器 -> filter -> DispatcherServlet -> controller
# preHandle方法则是在controller执行前执行
preHandle:控制器方法执行之前执行preHandle(),其boolean类型的返回值表示是否拦截或放行,返回true为放行,即调用控制器方法;返回false表示拦截,即不调用控制器方法
postHandle:控制器方法执行之后执行postHandle()
afterComplation:处理完视图和模型数据,渲染视图完毕之后执行afterComplation()
# 双击shift,搜索DispatcherServlet,点击右上角的下载源码,查看DispatcherServlet.java
# 查看1061行
// 调用所有的拦截器preHandle方法
if (!mappedHandler.applyPreHandle(processedRequest, response)) {
return;
}
// 调用控制器的方法
// Actually invoke the handler.
mv = ha.handle(processedRequest, response, mappedHandler.getHandler());
if (asyncManager.isConcurrentHandlingStarted()) {
return;
}
// 调用所有所有拦截器的postHandle方法
applyDefaultViewName(processedRequest, mv);
mappedHandler.applyPostHandle(processedRequest, response, mv);
}
catch (Exception ex) {
dispatchException = ex;
}
catch (Throwable err) {
// As of 4.3, we're processing Errors thrown from handler methods as well,
// making them available for @ExceptionHandler methods and other scenarios.
dispatchException = new NestedServletException("Handler dispatch failed", err); // 这个方法中有渲染视图的方法,ctrl查看
}
# 查看processDispatchResult方法,在方法的最下面,也即是视图渲染完后会执行afterComplation方法
processDispatchResult(processedRequest, response, mappedHandler, mv, dispatchException);
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) {
logger.debug("ModelAndViewDefiningException encountered", exception);
mv = ((ModelAndViewDefiningException) exception).getModelAndView();
}
else {
Object handler = (mappedHandler != null ? mappedHandler.getHandler() : null);
mv = processHandlerException(request, response, handler, exception);
errorView = (mv != null);
}
}
// Did the handler return a view to render?
if (mv != null && !mv.wasCleared()) {
render(mv, request, response);
if (errorView) {
WebUtils.clearErrorRequestAttributes(request);
}
}
else {
if (logger.isTraceEnabled()) {
logger.trace("No view rendering, null ModelAndView returned.");
}
}
if (WebAsyncUtils.getAsyncManager(request).isConcurrentHandlingStarted()) {
// Concurrent handling started during a forward
return;
}
if (mappedHandler != null) {
// Exception (if any) is already handled..
mappedHandler.triggerAfterCompletion(request, response, null);
}
}
# springmvc配置文件中配置视图控制器,用于跳转到index.html
<mvc:view-controller path="/" view-name="index"></mvc:view-controller>
# springmvc配置文件中配置拦截器
<mvc:interceptors>
<bean class="com.atguigu.mvc.interceptors.FirstInterceptor"></bean>
</mvc:interceptors>
# 编写1个控制器,用于测试
@Controller
public class TestController {
@RequestMapping("/**/testInterceptor")
public String testInterceptor(){
return "success";
}
}
# 编写1个拦截器
@Component
public class FirstInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("FirstInterceptor-->preHandle");
// 为true表示放行
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
System.out.println("FirstInterceptor-->postHandle");
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
System.out.println("FirstInterceptor-->afterCompletion");
}
}
# 测试
http://localhost:8080/上下文路径/testInterceptor
# 控制台打印
FirstInterceptor-->preHandle
FirstInterceptor-->postHandle
FirstInterceptor-->afterCompletion
# 如果将拦截器中的preHandle方法改为不放行
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("FirstInterceptor-->preHandle");
// 为false表示不放行
return false;
}
# 再次测试,控制台打印如下
FirstInterceptor-->preHandle
# 这是因为访问testInterceptor接口时,被拦截了,后面的postHandle方法和afterCompletion方法就不会执行了
# springmvc配置文件中配置拦截器,如下方式,会拦截所有请求
<mvc:interceptors>
<bean class="com.atguigu.mvc.interceptors.FirstInterceptor"></bean>
</mvc:interceptors>
# 编写1个控制器,用于测试
@Controller
public class TestController {
@RequestMapping("/**/testInterceptor")
public String testInterceptor(){
return "success";
}
}
# 编写1个拦截器
@Component
public class FirstInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("FirstInterceptor-->preHandle");
// 为true表示放行
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
System.out.println("FirstInterceptor-->postHandle");
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
System.out.println("FirstInterceptor-->afterCompletion");
}
}
# 测试
http://localhost:8080/上下文路径/testInterceptor
# 控制台打印
FirstInterceptor-->preHandle
FirstInterceptor-->postHandle
FirstInterceptor-->afterCompletion
# 拦截器FirstInterceptor添加注解@Component,将其注册为1个bean
# springmvc配置文件中开启组件扫描,确保拦截器能够被扫描到
<context:component-scan base-package="com.atguigu.mvc"></context:component-scan>
# springmvc配置文件中配置拦截器,如下方式,会拦截所有请求
<mvc:interceptors>
<ref bean="firstInterceptor"></ref>
</mvc:interceptors>
# 编写1个控制器,用于测试
@Controller
public class TestController {
@RequestMapping("/**/testInterceptor")
public String testInterceptor(){
return "success";
}
}
# 编写1个拦截器
@Component
public class FirstInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("FirstInterceptor-->preHandle");
// 为true表示放行
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
System.out.println("FirstInterceptor-->postHandle");
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
System.out.println("FirstInterceptor-->afterCompletion");
}
}
# 测试
http://localhost:8080/上下文路径/testInterceptor
# 控制台打印
FirstInterceptor-->preHandle
FirstInterceptor-->postHandle
FirstInterceptor-->afterCompletion
# 拦截器FirstInterceptor添加注解@Component,将其注册为1个bean
# springmvc配置文件中开启组件扫描,确保拦截器能够被扫描到
<context:component-scan base-package="com.atguigu.mvc"></context:component-scan>
# springmvc配置文件中配置拦截器,如下方式,会拦截指定请求
<mvc:interceptors>
<mvc:interceptor>
<!-- 表示拦截一级目录的所有请求 -->
<mvc:mapping path="/*"/>
<!-- 表示放行该请求 -->
<mvc:exclude-mapping path="/"/>
<ref bean="firstInterceptor"></ref>
</mvc:interceptor>
</mvc:interceptors>
# 编写1个控制器,用于测试
@Controller
public class TestController {
@RequestMapping("/**/testInterceptor")
public String testInterceptor(){
return "success";
}
}
# 编写1个拦截器
@Component
public class FirstInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("FirstInterceptor-->preHandle");
// 为true表示放行
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
System.out.println("FirstInterceptor-->postHandle");
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
System.out.println("FirstInterceptor-->afterCompletion");
}
}
# 测试
http://localhost:8080/上下文路径/
# 控制台打印没有打印,说明指定的根路径被放行了
# 测试
http://localhost:8080/上下文路径/testInterceptor
# 控制台打印
FirstInterceptor-->preHandle
FirstInterceptor-->postHandle
FirstInterceptor-->afterCompletion
# 测试二级目录
http://localhost:8080/上下文路径/test/testInterceptor
# 控制台打印没有打印说明没有拦截
# 拦截器FirstInterceptor添加注解@Component,将其注册为1个bean
# springmvc配置文件中开启组件扫描,确保拦截器能够被扫描到
<context:component-scan base-package="com.atguigu.mvc"></context:component-scan>
# springmvc配置文件中配置拦截器,如下方式,会拦截指定请求
<mvc:interceptors>
<mvc:interceptor>
<!-- 表示拦截所有请求 -->
<mvc:mapping path="/**"/>
<!-- 表示放行该请求 -->
<mvc:exclude-mapping path="/"/>
<ref bean="firstInterceptor"></ref>
</mvc:interceptor>
</mvc:interceptors>
# 编写1个控制器,用于测试
@Controller
public class TestController {
@RequestMapping("/**/testInterceptor")
public String testInterceptor(){
return "success";
}
}
# 编写1个拦截器
@Component
public class FirstInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("FirstInterceptor-->preHandle");
// 为true表示放行
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
System.out.println("FirstInterceptor-->postHandle");
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
System.out.println("FirstInterceptor-->afterCompletion");
}
}
# 测试
http://localhost:8080/上下文路径/
# 控制台打印没有打印,说明指定的根路径被放行了
# 测试
http://localhost:8080/上下文路径/testInterceptor
# 控制台打印
FirstInterceptor-->preHandle
FirstInterceptor-->postHandle
FirstInterceptor-->afterCompletion
# 测试二级目录
http://localhost:8080/上下文路径/test/testInterceptor
# 控制台打印
FirstInterceptor-->preHandle
FirstInterceptor-->postHandle
FirstInterceptor-->afterCompletion
# 在拦截器上添加注解@Component,将其注册为bean
# springmvc配置文件中开启组件扫描,确保拦截器能够被扫描到
<context:component-scan base-package="com.atguigu.mvc"></context:component-scan>
# springmvc配置文件中配置拦截器,如下方式,会拦截指定请求
<mvc:interceptors>
<ref bean="firstInterceptor"></ref>
<ref bean="secondInterceptor"></ref>
</mvc:interceptors>
# 编写1个控制器,用于测试
@Controller
public class TestController {
@RequestMapping("/**/testInterceptor")
public String testInterceptor(){
return "success";
}
}
# 编写1个拦截器
@Component
public class FirstInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("FirstInterceptor-->preHandle");
// 为true表示放行
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
System.out.println("FirstInterceptor-->postHandle");
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
System.out.println("FirstInterceptor-->afterCompletion");
}
}
# 编写第2个拦截器
@Component
public class SecondInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("SecondInterceptor-->preHandle");
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
System.out.println("SecondInterceptor-->postHandle");
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
System.out.println("SecondInterceptor-->afterCompletion");
}
}
# 测试1:将2个拦截器改为true
http://localhost:8080/上下文路径/testInterceptor
# 控制台打印
FirstInterceptor-->preHandle
SecondInterceptor-->preHandle
SecondInterceptor-->postHandle
FirstInterceptor-->postHandle
SecondInterceptor-->afterCompletion
FirstInterceptor-->afterCompletion
# 测试2:将2个拦截器改为false
13:43:49.883 [http-nio-8081-exec-1] DEBUG org.springframework.web.servlet.DispatcherServlet - GET "/mvc/", parameters={}
13:43:49.890 [http-nio-8081-exec-1] DEBUG org.springframework.web.servlet.handler.SimpleUrlHandlerMapping - Mapped to ParameterizableViewController [view="index"]
FirstInterceptor-->preHandle
13:43:49.893 [http-nio-8081-exec-1] DEBUG org.springframework.web.servlet.DispatcherServlet - Completed 200 OK
13:43:50.150 [http-nio-8081-exec-3] DEBUG org.springframework.web.servlet.DispatcherServlet - GET "/mvc/", parameters={}
13:43:50.150 [http-nio-8081-exec-3] DEBUG org.springframework.web.servlet.handler.SimpleUrlHandlerMapping - Mapped to ParameterizableViewController [view="index"]
FirstInterceptor-->preHandle
# 结论:
a>若每个拦截器的preHandle()都返回true
此时多个拦截器的执行顺序和拦截器在SpringMVC的配置文件的配置顺序有关:
preHandle()会按照配置的顺序执行,而postHandle()和afterComplation()会按照配置的反序执行
b>若某个拦截器的preHandle()返回了false
preHandle()返回false和它之前的拦截器的preHandle()都会执行,postHandle()都不执行,返回false的拦截器之前的拦截器的afterComplation()会执行
# 打开DispatcherServlet.java 1061行
# 首先进入如下代码,该执行链中放的是3个拦截器(1个是springmvc自己创建的,2个是我们自己创建的),执行链包括2个部分(控制器方法、处理控制器方法的拦截器)
if (!mappedHandler.applyPreHandle(processedRequest, response)) {
return;
}
# 当以上的方法,返回false时执行
# 进入applyPreHandle方法,遍历执行链中的拦截器,所以preHandle方法是按顺序执行
boolean applyPreHandle(HttpServletRequest request, HttpServletResponse response) throws Exception {
for (int i = 0; i < this.interceptorList.size(); i++) {
HandlerInterceptor interceptor = this.interceptorList.get(i);
if (!interceptor.preHandle(request, response, this.handler)) {
triggerAfterCompletion(request, response, null);
return false;
}
this.interceptorIndex = i;
}
return true;
}
# 之后执行postHandle方法
applyDefaultViewName(processedRequest, mv);
mappedHandler.applyPostHandle(processedRequest, response, mv);
# 进入postHandle方法,所以postHandle是反序执行
void applyPostHandle(HttpServletRequest request, HttpServletResponse response, @Nullable ModelAndView mv)
throws Exception {
for (int i = this.interceptorList.size() - 1; i >= 0; i--) {
HandlerInterceptor interceptor = this.interceptorList.get(i);
interceptor.postHandle(request, response, this.handler, mv);
}
}
# 最后执行afterComplation方法
finally {
if (asyncManager.isConcurrentHandlingStarted()) {
// Instead of postHandle and afterCompletion
if (mappedHandler != null) {
mappedHandler.applyAfterConcurrentHandlingStarted(processedRequest, response);
}
}
else {
// Clean up any resources used by a multipart request.
if (multipartRequestParsed) {
cleanupMultipart(processedRequest);
}
}
}
# 进入applyAfterConcurrentHandlingStarted,所以afterComplation是反序执行的
void applyAfterConcurrentHandlingStarted(HttpServletRequest request, HttpServletResponse response) {
for (int i = this.interceptorList.size() - 1; i >= 0; i--) {
HandlerInterceptor interceptor = this.interceptorList.get(i);
if (interceptor instanceof AsyncHandlerInterceptor) {
try {
AsyncHandlerInterceptor asyncInterceptor = (AsyncHandlerInterceptor) interceptor;
asyncInterceptor.afterConcurrentHandlingStarted(request, response, this.handler);
}
catch (Throwable ex) {
if (logger.isErrorEnabled()) {
logger.error("Interceptor [" + interceptor + "] failed in afterConcurrentHandlingStarted", ex);
}
}
}
}
}