javascript
Spring Boot spring mvc 拦截器
2019獨角獸企業重金招聘Python工程師標準>>>
上一篇對過濾器的定義做了說明,也比較簡單。過濾器屬于Servlet范疇的API,與Spring 沒什么關系。?
Web開發中,我們除了使用 Filter 來過濾請web求外,還可以使用Spring提供的HandlerInterceptor(攔截器)。
HandlerInterceptor 的功能跟過濾器類似,但是提供更精細的的控制能力:在request被響應之前、request被響應之后、視圖渲染之前以及request全部結束之后。我們不能通過攔截器修改request內容,但是可以通過拋出異常(或者返回false)來暫停request的執行。
實現 UserRoleAuthorizationInterceptor 的攔截器有:?
ConversionServiceExposingInterceptor?
CorsInterceptor?
LocaleChangeInterceptor?
PathExposingHandlerInterceptor?
ResourceUrlProviderExposingInterceptor?
ThemeChangeInterceptor?
UriTemplateVariablesHandlerInterceptor?
UserRoleAuthorizationInterceptor
其中 LocaleChangeInterceptor 和 ThemeChangeInterceptor 比較常用。
配置攔截器也很簡單,Spring 為什么提供了基礎類WebMvcConfigurerAdapter ,我們只需要重寫 addInterceptors 方法添加注冊攔截器。
實現自定義攔截器只需要3步:?
1、創建我們自己的攔截器類并實現 HandlerInterceptor 接口。?
2、創建一個Java類繼承WebMvcConfigurerAdapter,并重寫 addInterceptors 方法。?
2、實例化我們自定義的攔截器,然后將對像手動添加到攔截器鏈中(在addInterceptors方法中添加)。?
PS:本文重點在如何在Spring-Boot中使用攔截器,關于攔截器的原理請大家查閱資料了解。
代碼示例:
MyInterceptor1.java
package org.springboot.sample.interceptor;import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView;/*** 自定義攔截器1** @author 單紅宇(365384722)* @myblog http://blog.csdn.net/catoop/* @create 2016年1月7日*/ public class MyInterceptor1 implements HandlerInterceptor {@Overridepublic boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)throws Exception {System.out.println(">>>MyInterceptor1>>>>>>>在請求處理之前進行調用(Controller方法調用之前)");return true;// 只有返回true才會繼續向下執行,返回false取消當前請求}@Overridepublic void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,ModelAndView modelAndView) throws Exception {System.out.println(">>>MyInterceptor1>>>>>>>請求處理之后進行調用,但是在視圖被渲染之前(Controller方法調用之后)");}@Overridepublic void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)throws Exception {System.out.println(">>>MyInterceptor1>>>>>>>在整個請求結束之后被調用,也就是在DispatcherServlet 渲染了對應的視圖之后執行(主要是用于進行資源清理工作)");}}MyInterceptor2.java
package org.springboot.sample.interceptor;import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView;/*** 自定義攔截器2** @author 單紅宇(365384722)* @myblog http://blog.csdn.net/catoop/* @create 2016年1月7日*/ public class MyInterceptor2 implements HandlerInterceptor {@Overridepublic boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)throws Exception {System.out.println(">>>MyInterceptor2>>>>>>>在請求處理之前進行調用(Controller方法調用之前)");return true;// 只有返回true才會繼續向下執行,返回false取消當前請求}@Overridepublic void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,ModelAndView modelAndView) throws Exception {System.out.println(">>>MyInterceptor2>>>>>>>請求處理之后進行調用,但是在視圖被渲染之前(Controller方法調用之后)");}@Overridepublic void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)throws Exception {System.out.println(">>>MyInterceptor2>>>>>>>在整個請求結束之后被調用,也就是在DispatcherServlet 渲染了對應的視圖之后執行(主要是用于進行資源清理工作)");}}MyWebAppConfigurer.java
package org.springboot.sample.config;import org.springboot.sample.interceptor.MyInterceptor1; import org.springboot.sample.interceptor.MyInterceptor2; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;@Configuration public class MyWebAppConfigurer extends WebMvcConfigurerAdapter {@Overridepublic void addInterceptors(InterceptorRegistry registry) {// 多個攔截器組成一個攔截器鏈// addPathPatterns 用于添加攔截規則// excludePathPatterns 用戶排除攔截registry.addInterceptor(new MyInterceptor1()).addPathPatterns("/**");registry.addInterceptor(new MyInterceptor2()).addPathPatterns("/**");super.addInterceptors(registry);}}然后在瀏覽器輸入地址:?http://localhost:8080/index?后,控制臺的輸出為:
>>>MyInterceptor1>>>>>>>在請求處理之前進行調用(Controller方法調用之前) >>>MyInterceptor2>>>>>>>在請求處理之前進行調用(Controller方法調用之前) >>>MyInterceptor2>>>>>>>請求處理之后進行調用,但是在視圖被渲染之前(Controller方法調用之后) >>>MyInterceptor1>>>>>>>請求處理之后進行調用,但是在視圖被渲染之前(Controller方法調用之后) >>>MyInterceptor2>>>>>>>在整個請求結束之后被調用,也就是在DispatcherServlet 渲染了對應的視圖之后執行(主要是用于進行資源清理工作) >>>MyInterceptor1>>>>>>>在整個請求結束之后被調用,也就是在DispatcherServlet 渲染了對應的視圖之后執行(主要是用于進行資源清理工作)根據輸出可以了解攔截器鏈的執行順序(具體原理介紹,大家找度娘一問便知)
最后強調一點:只有經過DispatcherServlet 的請求,才會走攔截器鏈,我們自定義的Servlet 請求是不會被攔截的,比如我們自定義的Servlet地址?http://localhost:8080/xs/myservlet?是不會被攔截器攔截的。并且不管是屬于哪個Servlet 只要復合過濾器的過濾規則,過濾器都會攔截。
最后說明下,我們上面用到的 WebMvcConfigurerAdapter 并非只是注冊添加攔截器使用,其顧名思義是做Web配置用的,它還可以有很多其他作用,通過下面截圖便可以大概了解,具體每個方法都是干什么用的,留給大家自己研究(其實都大同小異也很簡單)。?
轉載于:https://my.oschina.net/xiaominmin/blog/1612816
《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀總結
以上是生活随笔為你收集整理的Spring Boot spring mvc 拦截器的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux复盘:mysql基础
- 下一篇: 【Storm篇】--Storm基础概念