日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關咨詢
選擇下列產品馬上在線溝通
服務時間:8:30-17:00
你可能遇到了下面的問題
關閉右側工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
Spring Security過濾器鏈如何匹配到特定的請求

如何攔截特定的請求

只有滿足了SecurityFilterChain的match方法的請求才能被該SecurityFilterChain處理,那如何配置才能讓一個SecurityFilterChain處理特定的路徑呢?

RequestMatcher

HttpSecurity內置了RequestMatcher屬性來處理路徑匹配問題。RequestMatcher可總結為以下幾大類:

使用Ant路徑:

 httpSecurity.antMatcher("/foo/**");

如果你配置了全局的Servlet Path的話,例如/v1,配置ant路徑的話就要/v1/foo/**,使用MVC風格可以保持一致:

 httpSecurity.mvcMatcher("/foo/**");

另外MVC風格可以自動匹配后綴,例如/foo/hello可以匹配/foo/hello.do、/foo/hello.action 等等。另外你也可以使用正則表達式來進行路徑匹配:

 httpSecurity.regexMatcher("/foo/.+");

如果上面的都滿足不了需要的話,你可以通過HttpSecurity.requestMatcher方法自定義匹配規(guī)則;如果你想匹配多個規(guī)則的話可以借助于HttpSecurity.requestMatchers方法來自由組合匹配規(guī)則,就像這樣:

    httpSecurity.requestMatchers(requestMatchers ->
requestMatchers.mvcMatchers("/foo/**")
.antMatchers("/admin/*get"));

一旦你配置了路徑匹配規(guī)則的話,你會發(fā)現(xiàn)默認的表單登錄404了,因為默認是/login,你加了前綴后當然訪問不到了。

使用場景

比如你后臺管理系統(tǒng)和前端應用各自走不同的過濾器鏈,你可以根據(jù)訪問路徑來配置各自的過濾器鏈。例如:

    /**
* Admin 過濾器鏈.
*
* @param http the http
* @return the security filter chain
* @throws Exception the exception
*/
@Bean
SecurityFilterChain adminSecurityFilterChain(HttpSecurity http) throws Exception {
http.requestMatchers(requestMatchers -> requestMatchers.mvcMatchers("/admin/**"))
//todo 其它配置
return http.build();
}

/**
* App 過濾器鏈.
*
* @param http the http
* @return the security filter chain
* @throws Exception the exception
*/
@Bean
SecurityFilterChain appSecurityFilterChain(HttpSecurity http) throws Exception {
http.requestMatchers(requestMatchers -> requestMatchers.mvcMatchers("/app/**"));
//todo 其它配置
return http.build();
}

另外也可以使用該特性降低不同規(guī)則URI之間的耦合性。

思考一下HttpSecurity這個Spring Bean為什么能夠重復使用。


文章名稱:Spring Security過濾器鏈如何匹配到特定的請求
分享網(wǎng)址:http://www.dlmjj.cn/article/cdoggso.html