SpringBoot攔截器excludePathPatterns方法不生效的解決方案
SpringBoot攔截器excludePathPatterns方法不生效
在攔截器用 excludePathPatterns() 方法排除訪問路徑時,發(fā)現(xiàn)不生效,配置代碼如下
/** * @author 程序員大佬超 * @date 2023-03-01 15:33. */ @Configuration public class WebMvcConfig implements WebMvcConfigurer { @Autowired AuthTokenInterceptor authTokenInterceptor; @Override public void addInterceptors(InterceptorRegistry registry) { List<String> patterns = new ArrayList<>(); patterns.add("/**/login/login"); registry.addInterceptor(authTokenInterceptor) .addPathPatterns("/**") .excludePathPatterns(patterns); } }
接口訪問地址:http://127.0.0.1:8088/api-im/login/login
其實這個問題大部分情況下無非兩個原因:
1、要排除的這個訪問路徑配置馬虎寫錯了。
2、要排除的這個訪問路徑就不存在,或者請求參數(shù)不對解析出錯,這時SpringBoot會將路徑自動變成/error,具體可以在自定義攔截器里打斷點驗證一下。
我這里就是第二個原因,可以看到自定義的攔截器里 requestURI 變成了 /error。
解決方法
保證接口正確或者檢查請求參數(shù),反正要保證能正常進入,我這就是請求頭里少了一個參數(shù),然后解析的時候日志里其實已經提示缺失請求頭了。
WARN 15628 --- [nio-8088-exec-1] .w.s.m.s.DefaultHandlerExceptionResolver :
Resolved [org.springframework.web.bind.MissingRequestHeaderException: Required request header 'test' for method parameter type String is not present]
然后,加上即可
springboot攔截器實現(xiàn)
1.創(chuàng)建一個類,實現(xiàn)WebMvxConfigurer,重寫addInterceptors,添加需要攔截的路徑,以及放行的路徑
import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; @Configuration public class MyWebMvcConfig implements WebMvcConfigurer { ? ? @Override ? ? public void addInterceptors(InterceptorRegistry registry) { ? ? ? ? try { //MyLoginInterception這個類是攔截的條件(如是否需要登錄,需要單獨來寫,請看下面) //excludePathPatterns這個是不需要進行攔截的操作路徑。 registry.addInterceptor(MyLoginInterception.class.newInstance()).addPathPatterns("/**") ? ? ? ? ? ? ? ? ? ? .excludePathPatterns("/login", "/register", "/**/*.js", "/**/*.css", "/**/*.html"); ? ? ? ? } catch (InstantiationException e) { ? ? ? ? ? ? throw new RuntimeException(e); ? ? ? ? } catch (IllegalAccessException e) { ? ? ? ? ? ? throw new RuntimeException(e); ? ? ? ? } ? ? } }
2.創(chuàng)建類,對攔截路徑的請求做條件判斷
import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @Configuration public class MyLoginInterception implements HandlerInterceptor { ? ? @Override ? ? public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { ? ? ? ? System.out.println("------------start------------"); //這里方便測試,對請求頭帶有”source“:”back“ 進行放行 ? ? ? ? if (null == request.getHeader("source") || !request.getHeader("source").equals("back")) { ? ? ? ? ? ? return false; ? ? ? ? } ? ? ? ? return true; ? ? } ? ? @Override ? ? public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { ? ? ? ? System.out.println("攔截器執(zhí)行結束"); ? ? ? ? HandlerInterceptor.super.postHandle(request, response, handler, modelAndView); ? ? } ? ? @Override ? ? public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { ? ? ? ? System.out.println("數(shù)據(jù)返回,執(zhí)行結束"); ? ? ? ? HandlerInterceptor.super.afterCompletion(request, response, handler, ex); ? ? } }
攔截器的實現(xiàn)比較簡單,比較難的是根據(jù)業(yè)務,做相關處理,如對cook,session的處理,以及各種條件的處理
總結
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Springboot Logback日志多文件輸出方式(按日期和大小分割)
這篇文章主要介紹了Springboot Logback日志多文件輸出方式(按日期和大小分割),具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-05-05flowable動態(tài)創(chuàng)建多級流程模板實現(xiàn)demo
這篇文章主要為大家介紹了flowable動態(tài)創(chuàng)建多級流程模板實現(xiàn)demo,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-05-05SpringBoot創(chuàng)建Docker鏡像的方法步驟
這篇文章主要介紹了SpringBoot創(chuàng)建Docker鏡像的方法步驟,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-11-11Java Spring使用hutool的HttpRequest發(fā)送請求的幾種方式
文章介紹了Hutool庫中用于發(fā)送HTTP請求的工具,包括添加依賴、發(fā)送GET和POST請求的方法,以及GET請求的不同參數(shù)傳遞方式,感興趣的朋友跟隨小編一起看看吧2024-11-11