如何解决Spring Security配置错误:不正确的请求匹配器类型问题
Spring Security 配置Fortify报错「不正确的请求匹配器类型」解决方案
报错原因
这个Fortify扫描报错的核心原因是你的XML配置中所有<http>标签没有显式指定请求匹配器类型,Spring Security虽然默认使用Ant风格路径匹配,但静态扫描规则认为隐式的匹配策略存在歧义风险,可能出现路径匹配不符合预期、权限绕过等安全问题,因此触发了规则告警。
你报错的第9、10、11、15行对应4个放行Swagger资源的<http>标签,以及匹配所有/api/**路径的鉴权<http>标签,都属于未显式声明匹配器的场景。
修复方案
给所有<http>标签显式添加request-matcher="ant"属性即可,你当前使用的/**、固定路径都属于Ant风格的路径匹配规则,和默认逻辑完全一致,不会改动原有权限逻辑。
修改后的核心配置片段如下:
<!-- 放行Swagger相关资源 --> <http pattern="/api/swagger-ui.html" security="none" request-matcher="ant" xmlns="http://www.springframework.org/schema/security"/> <http pattern="/api/webjars/**" security="none" request-matcher="ant" xmlns="http://www.springframework.org/schema/security"/> <http pattern="/api/swagger-resources/**" security="none" request-matcher="ant" xmlns="http://www.springframework.org/schema/security"/> <http pattern="/api/v2/api-docs" security="none" request-matcher="ant" xmlns="http://www.springframework.org/schema/security"/> <!-- API接口鉴权配置 --> <http pattern="/api/**" create-session="never" entry-point-ref="oauthAuthenticationEntryPoint" access-decision-manager-ref="accessDecisionManager" request-matcher="ant" xmlns="http://www.springframework.org/schema/security"> <anonymous enabled="false"/> <intercept-url pattern="/api/**" access="IS_AUTHENTICATED_FULLY"/> <custom-filter ref="resourceServerFilter" before="PRE_AUTH_FILTER"/> <access-denied-handler ref="oauthAccessDeniedHandler"/> </http>
验证逻辑
修改配置后按以下步骤验证功能正常:
- 启动服务访问Swagger相关路径,确认无需认证即可正常访问
- 调用其他/api/前缀的业务接口,未携带有效OAuth Token时返回401,携带有效Token时正常返回,确认原有鉴权逻辑未受影响
- 重新执行Fortify扫描,确认该告警已消除
内容的提问来源于stack exchange,提问作者Petr Kostroun
相关产品推荐
相关产品推荐

