springboot 攔截器執(zhí)行兩次的解決方案
springboot攔截器執(zhí)行兩次
原因是:
org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.error
也是一個controller路徑為/error
@Configuration public class MVCConfig extends WebMvcConfigurationSupport { //自定義的攔截器 @Bean public SecurityInterceptor getSecurityInterceptor() { return new SecurityInterceptor(); } @Override public void addInterceptors(InterceptorRegistry registry) { //添加攔截器 InterceptorRegistration registration = registry.addInterceptor(getSecurityInterceptor()); //排除的路徑 registration.excludePathPatterns("/login"); registration.excludePathPatterns("/logout"); //將這個controller放行 registration.excludePathPatterns("/error"); //攔截全部 registration.addPathPatterns("/**"); } }
Springboot攔截器原理
根據(jù)當(dāng)前請求,找到**HandlerExecutionChain**** 【可以處理請求的handler以及handler的所有 攔截器】
先來順序執(zhí)行 所有攔截器的 preHandle方法
如果當(dāng)前攔截器prehandler返回為true。則執(zhí)行下一個攔截器的preHandle
如果當(dāng)前攔截器返回為false。直接 倒序執(zhí)行所有已經(jīng)執(zhí)行了的攔截器的 afterCompletion;
如果任何一個攔截器返回false。直接跳出不執(zhí)行目標(biāo)方法
所有攔截器都返回True。執(zhí)行目標(biāo)方法
倒序執(zhí)行所有攔截器的postHandle方法。
前面的步驟有任何異常都會直接倒序觸發(fā) afterCompletion
頁面成功渲染完成以后,也會倒序觸發(fā) afterCompletion
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章

SpringBoot Admin 如何實現(xiàn)Actuator端點可視化監(jiān)控

SpringBoot?把PageHelper分頁信息返回給前端的方法步驟

SpringCloud組件之Eureka Server詳細(xì)啟動過程及說明

SpringBoot+LayIM+t-io 實現(xiàn)好友申請通知流程