SpringBoot配置攔截器的示例
在SpringBoot中配置攔截器,主要有下面兩個步驟:
1、繼承接口 HandlerInterceptor,根據(jù)需要重寫其中的三個類。
2、在配置類中注入該類。
public class MyInterceptor implements HandlerInterceptor { //controller執(zhí)行之前 @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { System.out.println('preHandler......'); return true; } //執(zhí)行完controller執(zhí)行之后、視圖渲染前調(diào)用,可以在該方法里獲取或者修改model @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { System.out.println('postHandler......'); } //一般用于清理資源 @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { System.out.println('afterCompletion......'); }}
@Configurationpublic class WebMvcConfig implements WebMvcConfigurer { @Override public void addInterceptors(InterceptorRegistry registry) { //1、全部攔截// registry.addInterceptor(myInterceptor()).addPathPatterns('/**'); //2、攔截指定路徑 registry.addInterceptor(myInterceptor()).addPathPatterns('/hello'); } @Bean MyInterceptor myInterceptor(){ return new MyInterceptor(); }}
寫個controller測試一下
@RestControllerpublic class HelloController { @RequestMapping('/hello') public String hello(){ System.out.println('hello'); return 'hello'; } @RequestMapping('/world') public String world(){ System.out.println('world'); return 'world'; }}
測試結(jié)果:
preHandler......hellopostHandler......afterCompletion......world
SpringBoot中還有一終攔截器,WebRequestInterceptor
public class MyWebRequestInterceptor implements WebRequestInterceptor { @Override public void preHandle(WebRequest webRequest) throws Exception { } @Override public void postHandle(WebRequest webRequest, ModelMap modelMap) throws Exception { } @Override public void afterCompletion(WebRequest webRequest, Exception e) throws Exception { }}
和HandlerInterceptor比較相似,但是可以發(fā)現(xiàn),該攔截器的preHandler返回值為空,說明該方法并不影響后面方法的執(zhí)行。那么這個攔截器存在的目的是什么吶?
點進WebRequest:
public interface WebRequest extends RequestAttributes { @Nullable String getHeader(String var1); @Nullable String[] getHeaderValues(String var1); Iterator<String> getHeaderNames(); @Nullable String getParameter(String var1); @Nullable String[] getParameterValues(String var1); Iterator<String> getParameterNames(); Map<String, String[]> getParameterMap(); Locale getLocale(); String getContextPath(); @Nullable String getRemoteUser(); @Nullable Principal getUserPrincipal(); boolean isUserInRole(String var1); boolean isSecure();
發(fā)現(xiàn)對reques請求中參數(shù)做了進一步處理(@Nullable表示可以為空),更加的方便調(diào)用。所以兩個攔截器的側(cè)重點不同,HandlerInterceptor功能較為強大,可以攔截請求,可以實現(xiàn)WebRequestInterceptor的所有功能,只是要寫的邏輯代碼要多一點。更而WebRequestInterceptor傾向于簡化獲取request參數(shù)的過程以及預(yù)設(shè)參數(shù)供后面的流程使用。
以上就是SpringBoot配置攔截器的示例的詳細內(nèi)容,更多關(guān)于SpringBoot配置攔截器的資料請關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. Python如何批量生成和調(diào)用變量2. windows服務(wù)器使用IIS時thinkphp搜索中文無效問題3. Python基于requests實現(xiàn)模擬上傳文件4. python利用opencv實現(xiàn)顏色檢測5. Python sorted排序方法如何實現(xiàn)6. Python 中如何使用 virtualenv 管理虛擬環(huán)境7. 通過CSS數(shù)學(xué)函數(shù)實現(xiàn)動畫特效8. ASP.NET MVC實現(xiàn)橫向展示購物車9. ASP.Net Core(C#)創(chuàng)建Web站點的實現(xiàn)10. Python獲取B站粉絲數(shù)的示例代碼
