拦截器, 技术java反射技术, 不依赖Spring容器, 可以使用 Spring 容器管理的bean.
客户端发起请求到 DispatcherServlet , 然后根据 HandlerMapping 匹配处理器, 返回一个 HandlerExecution 对象, 包含 处理器 和 拦截器, 这里的拦截器会对处理器进行拦截, 通过拦截器可以增强处理器的功能.
1. 创建拦截器, 实现 HandlerInterceptor 接口
package com.codingos.springboot.test.interceptor;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.springframework.web.servlet.HandlerInterceptor;import org.springframework.web.servlet.ModelAndView;public class Interceptor1 implements HandlerInterceptor { /** * 在 controller 方法之前执行, 返回false则结束, 返回true则继续下一步. */ @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { System.out.println("****** 处理器前方法 *******"); return true;// request.getRequestDispatcher("/file/uploadPage").forward(request, response);// return false; } /** * 在处理器方法之后, 视图渲染之前执行. */ @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { System.out.println("****** 处理器后方法 *******"); HandlerInterceptor.super.postHandle(request, response, handler, modelAndView); } /** * 在视图渲染之后执行 */ @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { System.out.println("****** 处理器完成后方法 *******"); HandlerInterceptor.super.afterCompletion(request, response, handler, ex); }}
package com.codingos.springboot.test.interceptor;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.springframework.web.servlet.HandlerInterceptor;import org.springframework.web.servlet.ModelAndView;public class MultiInterceptor1 implements HandlerInterceptor { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { System.out.println(this.getClass().getName() + "处理器前方法"); return true; } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { System.out.println(this.getClass().getName() + "处理器后方法"); HandlerInterceptor.super.postHandle(request, response, handler, modelAndView); } @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { System.out.println(this.getClass().getName() + "处理器完成后方法"); HandlerInterceptor.super.afterCompletion(request, response, handler, ex); }}
2. 注册拦截器, springboot 主类 实现 WebMvcConfigurer
@SpringBootApplicationpublic class SpringBootTestApplication implements WebMvcConfigurer{ /** * 注册拦截器 */ @Override public void addInterceptors(InterceptorRegistry registry) {// registry.addInterceptor(new Interceptor1()).addPathPatterns("/interceptor/*"); registry.addInterceptor(new MultiInterceptor1()).addPathPatterns("/interceptor/*"); registry.addInterceptor(new MultiInterceptor2()).addPathPatterns("/interceptor/*"); registry.addInterceptor(new MultiInterceptor3()).addPathPatterns("/interceptor/*"); } public static void main(String[] args) { SpringApplication.run(SpringBootTestApplication.class, args); }}
3. 多个拦截器的顺序
com.codingos.springboot.test.interceptor.MultiInterceptor1处理器前方法com.codingos.springboot.test.interceptor.MultiInterceptor2处理器前方法com.codingos.springboot.test.interceptor.MultiInterceptor3处理器前方法执行处理器逻辑com.codingos.springboot.test.interceptor.MultiInterceptor3处理器后方法com.codingos.springboot.test.interceptor.MultiInterceptor2处理器后方法com.codingos.springboot.test.interceptor.MultiInterceptor1处理器后方法视图渲染com.codingos.springboot.test.interceptor.MultiInterceptor3处理器完成后方法com.codingos.springboot.test.interceptor.MultiInterceptor2处理器完成后方法com.codingos.springboot.test.interceptor.MultiInterceptor1处理器完成后方法