Spring Boot (Web 篇):整合攔截器Interceptor
阿新 • • 發佈:2018-11-08
目錄
說在前面
Struts2(Interceptor篇):攔截器的實現原理以及程式碼示例:https://blog.csdn.net/Soinice/article/details/83341457
Spring MVC (Interceptor 篇):整合攔截器Interceptor:https://blog.csdn.net/Soinice/article/details/83342954
在pom.xml加入相關依賴
當然,通過idea 建立預設spring boot web 專案,會預設含有這個屬性
定義攔截器
編寫攔截器 Interceptor
建立自己的Interceptor攔截器,並且實現HandlerInterceptor介面。
使用@Component讓Spring 管理其生命週期:
註冊攔截器 Interceptor
編寫攔截器後,我們還需要將其註冊到攔截器鏈中,如下配置:
新建WebConfig繼承WebMvcConfigurerAdapter並且重寫addInrerceptors方法。
頁面訪問(專案啟動)
頁面隨便方位一個Controller
總結
需要注意的是,攔截器是基於spring的,且只有通過DispatcherServlet的請求才能被攔截。
原始碼下載
[相關示例完整程式碼]請獲取 相關分支哦:feature-20180910-web