您好,登錄后才能下訂單哦!
如何使用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......"); } }
@Configuration public 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測試一下
@RestController public 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......
hello
postHandler......
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ù)的過程以及預設(shè)參數(shù)供后面的流程使用。
看完上述內(nèi)容是否對您有幫助呢?如果還想對相關(guān)知識有進一步的了解或閱讀更多相關(guān)文章,請關(guān)注億速云行業(yè)資訊頻道,感謝您對億速云的支持。
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。