Spring 攔截器 學習
阿新 • • 發佈:2019-02-25
客戶端發起請求到 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
@SpringBootApplication public 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處理器完成後方法