Spring Security中@RequestMapping是否存在禁用路径值列表?
问题:Spring Security中是否存在@RequestMapping的「禁用」值列表?
我有一个简单的REST/Flux控制器:
@RestController @RequestMapping("/deployment") public class MyController{ @PostMapping(consumes = {MediaType.MULTIPART_FORM_DATA_VALUE, "application/zip"}) public Mono<Status> uploadFiles(@RequestPart("file") FilePart file) {} @GetMapping public Mono<Status> list() {} }
原本运行正常,添加如下Spring Security配置后:
@Configuration @EnableWebFluxSecurity @EnableReactiveMethodSecurity @RequiredArgsConstructor class WebSecurityConfiguration { private final CookieServerAuthenticationConverter cookieServerAuthenticationConverter; private final CustomAuthenticationEntryPoint customAuthenticationEntryPoint; private final WebSecurityUnsecuredPaths webSecurityUnsecuredPaths; @Bean public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) { return http .authorizeExchange(exchanges -> exchanges .pathMatchers(HttpMethod.GET, webSecurityUnsecuredPaths.getUnsecuredPathsMatchers()) .permitAll() .anyExchange().authenticated()) .oauth2ResourceServer(oauth2 -> oauth2.jwt(Customizer.withDefaults()).bearerTokenConverter(cookieServerAuthenticationConverter) .authenticationEntryPoint(customAuthenticationEntryPoint)) .exceptionHandling(exceptionHandlingSpec -> exceptionHandlingSpec .authenticationEntryPoint(customAuthenticationEntryPoint)) .build(); } }
现在GET接口list可正常工作,但POST接口uploadFiles失效。对比其他控制器配置无异常,GET接口无需认证即可访问,POST接口需要认证。将@RequestMapping的路径从/deployment改为/uploadFile后,所有接口恢复正常。现咨询:Spring Security中是否存在@RequestMapping的「禁用」值列表?
回答
Spring Security 没有官方维护的@RequestMapping路径「禁用」值列表。你遇到的问题大概率和以下原因有关:
- 路径匹配规则覆盖不全:你的安全配置中仅对
webSecurityUnsecuredPaths.getUnsecuredPathsMatchers()返回路径的GET请求放行,/deployment的POST请求未被包含在放行规则内。修改路径为/uploadFile后,该路径的POST请求恰好被现有规则覆盖(或该路径的所有请求都被配置为无需认证)。 - 自定义认证组件干扰:配置中使用的
cookieServerAuthenticationConverter自定义转换器,可能对/deployment路径的POST请求存在特殊处理逻辑(比如解析认证信息失败),导致触发认证拦截,而其他路径不受影响。 - 隐式的自定义安全规则:项目中可能存在其他扩展或自定义配置,针对包含
deployment这类关键词的路径添加了额外的安全校验,需要排查所有安全相关的配置类、过滤器。
建议排查步骤:
- 打印
webSecurityUnsecuredPaths.getUnsecuredPathsMatchers()的返回值,确认是否包含/deployment的POST请求路径。 - 临时移除
cookieServerAuthenticationConverter配置,测试POST接口是否恢复正常,判断是否是自定义转换器的问题。 - 开启Spring Security调试日志(配置
logging.level.org.springframework.security=DEBUG),查看请求处理过程中的认证授权细节,定位具体的拦截逻辑。
内容的提问来源于stack exchange,提问作者Koziołek
相关产品推荐
相关产品推荐

