如何正確的使用spring boot攔截器?很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學習下,希望你能有所收獲。
創(chuàng)新互聯(lián)是一家專業(yè)提供湖北企業(yè)網(wǎng)站建設,專注與成都網(wǎng)站建設、網(wǎng)站制作、成都h5網(wǎng)站建設、小程序制作等業(yè)務。10年已為湖北眾多企業(yè)、政府機構等服務。創(chuàng)新互聯(lián)專業(yè)網(wǎng)站建設公司優(yōu)惠進行中。
1.spring boot攔截器默認有:
HandlerInterceptorAdapter
AbstractHandlerMapping
UserRoleAuthorizationInterceptor
LocaleChangeInterceptor
ThemeChangeInterceptor
其中 LocaleChangeInterceptor 和 ThemeChangeInterceptor 比較常用。
2.實現(xiàn)自定義攔截器只需要3步:
1)、創(chuàng)建我們自己的攔截器類并實現(xiàn) HandlerInterceptor 接口。
2)、創(chuàng)建一個Java類繼承WebMvcConfigurerAdapter,并重寫 addInterceptors 方法。
3)、實例化我們自定義的攔截器,然后將對像手動添加到攔截器鏈中(在addInterceptors方法中添加)。
3.代碼示例
IndexInterceptor.java類代碼:
package com.example.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 IndexInterceptor implements HandlerInterceptor{ @Override public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3) throws Exception { System.out.println(">>>IndexInterceptor>>>>>>>在整個請求結束之后被調(diào)用,也就是在DispatcherServlet 渲染了對應的視圖之后執(zhí)行(主要是用于進行資源清理工作)"); } @Override public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3) throws Exception { System.out.println(">>>IndexInterceptor>>>>>>>請求處理之后進行調(diào)用,但是在視圖被渲染之前(Controller方法調(diào)用之后)"); } @Override public boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2) throws Exception { System.out.println(">>>IndexInterceptor>>>>>>>在請求處理之前進行調(diào)用(Controller方法調(diào)用之前)"); // 只有返回true才會繼續(xù)向下執(zhí)行,返回false取消當前請求 return true; } }
IndexInterceptor2.java類代碼:
package com.example.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 IndexInterceptor2 implements HandlerInterceptor{ @Override public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3) throws Exception { System.out.println(">>>IndexInterceptor2>>>>>>>在整個請求結束之后被調(diào)用,也就是在DispatcherServlet 渲染了對應的視圖之后執(zhí)行(主要是用于進行資源清理工作)"); } @Override public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3) throws Exception { System.out.println(">>>IndexInterceptor2>>>>>>>請求處理之后進行調(diào)用,但是在視圖被渲染之前(Controller方法調(diào)用之后)"); } @Override public boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2) throws Exception { System.out.println(">>>IndexInterceptor2>>>>>>>在請求處理之前進行調(diào)用(Controller方法調(diào)用之前)"); // 只有返回true才會繼續(xù)向下執(zhí)行,返回false取消當前請求 return false; } }
SimpleWebAppConfigurer.java類代碼:
package com.example.config; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; import com.example.interceptor.IndexInterceptor; import com.example.interceptor.IndexInterceptor2; //只要能被springboot掃描到即可 @Configuration public class SimpleWebAppConfigurer extends WebMvcConfigurerAdapter{ @Override public void addInterceptors(InterceptorRegistry registry) { // 多個攔截器組成一個攔截器鏈 // addPathPatterns 用于添加攔截規(guī)則 // excludePathPatterns 用戶排除攔截 registry.addInterceptor(new IndexInterceptor()).addPathPatterns("/**"); registry.addInterceptor(new IndexInterceptor2()).addPathPatterns("/**"); super.addInterceptors(registry); } }
4.攔截器解析說明
preHandle**:預處理回調(diào)方法,實現(xiàn)處理器的預處理(如登錄檢查),第三個參數(shù)為響應的處理器(如我們上一章的Controller實現(xiàn));
返回值:true表示繼續(xù)流程(如調(diào)用下一個攔截器或處理器);
false表示流程中斷(如登錄檢查失敗),不會繼續(xù)調(diào)用其他的攔截器或處理器,此時我們需要通過response來產(chǎn)生響應;
postHandle**:后處理回調(diào)方法,實現(xiàn)處理器的后處理(但在渲染視圖之前),此時我們可以通過modelAndView(模型和視圖對象)對模型數(shù)據(jù)進行處理或?qū)σ晥D進行處理,modelAndView也可能為null。
afterCompletion**:整個請求處理完畢回調(diào)方法,即在視圖渲染完畢時回調(diào),如性能監(jiān)控中我們可以在此記錄結束時間并輸出消耗時間,還可以進行一些資源清理,類似于try-catch-finally中的finally,但僅調(diào)用處理器執(zhí)行鏈中preHandle返回true的攔截器的afterCompletion**。
看完上述內(nèi)容是否對您有幫助呢?如果還想對相關知識有進一步的了解或閱讀更多相關文章,請關注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對創(chuàng)新互聯(lián)的支持。
本文標題:如何正確的使用springboot攔截器
標題來源:http://aaarwkj.com/article10/gpiggo.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站排名、網(wǎng)站制作、外貿(mào)建站、云服務器、品牌網(wǎng)站設計、營銷型網(wǎng)站建設
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)