You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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这类关键词的路径添加了额外的安全校验,需要排查所有安全相关的配置类、过滤器。

建议排查步骤:

  1. 打印webSecurityUnsecuredPaths.getUnsecuredPathsMatchers()的返回值,确认是否包含/deployment的POST请求路径。
  2. 临时移除cookieServerAuthenticationConverter配置,测试POST接口是否恢复正常,判断是否是自定义转换器的问题。
  3. 开启Spring Security调试日志(配置logging.level.org.springframework.security=DEBUG),查看请求处理过程中的认证授权细节,定位具体的拦截逻辑。

内容的提问来源于stack exchange,提问作者Koziołek

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.08 18:05:17