全面解析Spring Security 過濾器鏈的機制和特性
1. 前言
過濾器作為 Spring Security 的重中之重,我們需要了解其中的機制。這樣我們才能根據(jù)業(yè)務(wù)需求的變化進(jìn)行定制。今天來探討一下 Spring Security 中的過濾器鏈機制。
2. Spring Security 過濾器鏈
客戶端(APP 和后臺管理客戶端)向應(yīng)用程序發(fā)送請求,然后應(yīng)用根據(jù)請求的 URI 的路徑來確定該請求的過濾器鏈(Filter)以及最終的具體 Servlet 控制器(Controller)。
從上圖我們可以看出 Spring Security 以一個單 Filter(FilterChainProxy) 存在于整個過濾器鏈中,而這個 FilterChainProxy 實際內(nèi)部代理著眾多的 Spring Security Filter 。這簡直就是套娃啊!
2.1 過濾器鏈的形成過程
再多說一點 Filter 們的初始化過程,首先 Filter 們按照一定的順序被 SecurityBuilder 的實現(xiàn)來組裝為 SecurityFilterChain ,然后通過 WebSecurity 注入到 FilterChainProxy 中去,接著 FilterChainProxy 又在 WebSecurityConfiguration 中以 springSecurityFilterChain 的名稱注冊為 Spring Bean 。實際上還有一個隱藏層 DelegatingFilterProxy 代理了 springSecurityFilterChain 注入到最后整個 Servlet 過濾器鏈中。簡單畫了個圖;
事實上 Spring Security 的內(nèi)置 Filter 對于 Spring IoC 容器來說都是不可見的。
”Spring Security 允許有多 條過濾器鏈并行,Spring Security 的 FilterChainProxy 可以代理多條過濾器鏈并根據(jù)不同的 URI 匹配策略進(jìn)行分發(fā)。但是每個請求每次只能被分發(fā)到一條過濾器鏈。如下圖所示:
關(guān)于 Filter 的其它細(xì)節(jié)可以通過 相關(guān)文章[1] 了解。
★實際每條過濾鏈 就是一個 SecurityFilterChain
”4. 總結(jié)
今天我們通過對 Spring Security 中 過濾器鏈機制,對于深入學(xué)習(xí) Spring Security 有著至關(guān)重要的意義。有什么問題和心得請留言反饋。
參考資料
相關(guān)文章: https://www.felord.cn/spring-security-filters.html
到此這篇關(guān)于Spring Security 過濾器鏈的機制和特性的文章就介紹到這了,更多相關(guān)Spring Security 過濾器鏈內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. Python PyQt5-圖形界面的美化操作2. python 編寫輸出到csv的操作3. JAMon(Java Application Monitor)備忘記4. PHP swoole的process模塊創(chuàng)建和使用子進(jìn)程操作示例5. 利用原生JS實現(xiàn)歡樂水果機小游戲6. js的一些潛在規(guī)則使用分析7. 讓chatgpt將html中的圖片轉(zhuǎn)為base64方法示例8. 詳解python第三方庫的安裝、PyInstaller庫、random庫9. Python PyQt5中彈出子窗口解決子窗口一閃而過的問題10. 手把手教你用python發(fā)送短消息(基于阿里云平臺)
