SpringBoot配置攔截器的示例
在SpringBoot中配置攔截器,主要有下面兩個(gè)步驟:
1、繼承接口 HandlerInterceptor,根據(jù)需要重寫其中的三個(gè)類。
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(); }}
寫個(gè)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í)行。那么這個(gè)攔截器存在的目的是什么吶?
點(diǎn)進(jìn)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ù)做了進(jìn)一步處理(@Nullable表示可以為空),更加的方便調(diào)用。所以兩個(gè)攔截器的側(cè)重點(diǎn)不同,HandlerInterceptor功能較為強(qiáng)大,可以攔截請求,可以實(shí)現(xiàn)WebRequestInterceptor的所有功能,只是要寫的邏輯代碼要多一點(diǎn)。更而WebRequestInterceptor傾向于簡化獲取request參數(shù)的過程以及預(yù)設(shè)參數(shù)供后面的流程使用。
以上就是SpringBoot配置攔截器的示例的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot配置攔截器的資料請關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. HTML DOM setInterval和clearInterval方法案例詳解2. XML 非法字符(轉(zhuǎn)義字符)3. 不要在HTML中濫用div4. HTML5實(shí)戰(zhàn)與剖析之觸摸事件(touchstart、touchmove和touchend)5. CSS清除浮動(dòng)方法匯總6. HTTP協(xié)議常用的請求頭和響應(yīng)頭響應(yīng)詳解說明(學(xué)習(xí))7. XML入門的常見問題(三)8. .NET Core 分布式任務(wù)調(diào)度ScheduleMaster詳解9. jscript與vbscript 操作XML元素屬性的代碼10. XML在語音合成中的應(yīng)用
