使用多個(gè)servlet時(shí)Spring security需要指明路由匹配策略問題
多個(gè)servlet時(shí)Spring security需要指明路由匹配策略
項(xiàng)目原本是 SpringBoot-3.1.3
+ druid-1.2.21
,并且 Druid
開啟了可視化監(jiān)控頁 stat-view-servlet
。
將項(xiàng)目升級到 SpringBoot-3.1.4
后,啟動(dòng)項(xiàng)目時(shí)報(bào)錯(cuò)。
摘取部分內(nèi)容:
Failed to instantiate [org.springframework.security.web.SecurityFilterChain]: Factory method ‘filterChain’ threw exception with message: This method cannot decide whether these patterns are Spring MVC patterns or not. If this endpoint is a Spring MVC endpoint, please use requestMatchers(MvcRequestMatcher); otherwise, please use requestMatchers(AntPathRequestMatcher).
This is because there is more than one mappable servlet in your servlet context: {org.springframework.web.servlet.DispatcherServlet=[/], com.alibaba.druid.support.jakarta.StatViewServlet=[/druid/*]}.
原因分析
錯(cuò)誤信息可以看出來配置 Spring security
的放行策略時(shí)路由匹配的策略選擇不當(dāng),
根據(jù)spring官方在關(guān)于 CVE-2023-34035
的安全報(bào)告中提到
Spring Security versions 5.8 prior to 5.8.5, 6.0 prior to 6.0.5 and 6.1 prior to 6.1.2 could be susceptible to authorization rule misconfiguration if the application uses or and multiple servlets, one of them being Spring MVC’s DispatcherServlet.
如果應(yīng)用程序使用或和多個(gè)servlet(其中一個(gè)是Spring MVC的DispatcherServlet),則Spring Security 5.8.5之前的5.8版本、6.0.5之前的6.0版本和6.1.2之前的6.1版本可能容易受到授權(quán)規(guī)則錯(cuò)誤配置的影響。
即除了 DispatcherServlet
以外,還存在其他 Servlet
時(shí), Spring security
的路由匹配策略需要明確哪些路由模式是Spring MVC的。
修復(fù)問題
- 例如舊版放行路徑使用的是
http.authorizeHttpRequests(authorize -> authorize.requestMatchers("/user/register").permitAll())
- 改為
// MvcRequestMatcher策略 authorize.requestMatchers(new MvcRequestMatcher(new HandlerMappingIntrospector(), "/user/register")).permitAll() // AntPathRequestMatcher策略 authorize.requestMatchers(new AntPathRequestMatcher("/user/register")).permitAll()
其中 MvcRequestMatcher
和 AntPathRequestMatcher
是兩種不同的匹配規(guī)則。
具體概念不再贅述,核心點(diǎn)就是 MvcRequestMatcher
基于 DispatcherServlet
進(jìn)行匹配。
路由策略區(qū)別
調(diào)整放行 Druid
的路徑,使用 MvcRequestMatcher
策略匹配,正常啟動(dòng)。
但是訪問網(wǎng)頁的時(shí)候發(fā)現(xiàn)放行沒生效,被 Spring security
攔截了,需要認(rèn)證才能訪問,調(diào)整為 AntPathRequestMatcher
則是正常放行。
因?yàn)?Druid
提供了自己的監(jiān)控?cái)?shù)據(jù)的 Servlet
,其是作為一個(gè)獨(dú)立的 Servlet
映射到容器中,而并非通過 DispatcherServlet
,所以放行 Druid
就需要使用 AntPathRequestMatcher
方式。
authorize.requestMatchers(new AntPathRequestMatcher("/druid/**")).permitAll()
而對于 Swagger
,通常它的頁面和API文檔都會(huì)被包括在 Spring MVC
的控制器里面,并且其URL路徑會(huì)通過 @RequestMapping
注解映射,所以使用 MvcRequestMatcher
和 AntPathRequestMatcher
都可以正確匹配到。
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- Vue 動(dòng)態(tài)路由的實(shí)現(xiàn)及 Springsecurity 按鈕級別的權(quán)限控制
- Spring?Boot如何接入Security權(quán)限認(rèn)證服務(wù)
- SpringBoot集成Swagger使用SpringSecurity控制訪問權(quán)限問題
- SpringSecurity角色權(quán)限控制(SpringBoot+SpringSecurity+JWT)
- Spring Security基于HttpRequest配置權(quán)限示例詳解
- Spring Security實(shí)現(xiàn)動(dòng)態(tài)路由權(quán)限控制方式
相關(guān)文章
SpringMVC使用MultipartFile實(shí)現(xiàn)文件上傳
這篇文章主要為大家詳細(xì)介紹了SpringMVC使用MultipartFile實(shí)現(xiàn)文件上傳功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-04-04詳解如何Java中實(shí)現(xiàn)Excel的注釋和批注
注釋及批注是?Excel?中比較常用的功能,這篇文章主要為大家詳細(xì)介紹了如何在Java中實(shí)現(xiàn)Excel的注釋和批注,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-12-12thymeleaf實(shí)現(xiàn)前后端數(shù)據(jù)交換的示例詳解
Thymeleaf?是一款用于渲染?XML/XHTML/HTML5?內(nèi)容的模板引擎,當(dāng)通過?Web?應(yīng)用程序訪問時(shí),Thymeleaf?會(huì)動(dòng)態(tài)地替換掉靜態(tài)內(nèi)容,使頁面動(dòng)態(tài)顯示,這篇文章主要介紹了thymeleaf實(shí)現(xiàn)前后端數(shù)據(jù)交換,需要的朋友可以參考下2022-07-07Java 是如何讀取和寫入瀏覽器Cookies的實(shí)例詳解
這篇文章主要介紹了Java 是如何讀取和寫入瀏覽器Cookies的實(shí)例的相關(guān)資料,需要的朋友可以參考下2016-09-09java實(shí)現(xiàn)肯德基收銀系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)肯德基收銀系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-05-05