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

Spring Security:同一SecurityFilterChain中两个authorizeHttpRequests是否覆盖?

在Spring Security中多次调用authorizeHttpRequests的行为分析

是的,第二个.authorizeHttpRequests调用会完全替换第一个的配置。

Spring Security的HttpSecurity采用配置器替换机制,.authorizeHttpRequests方法的作用是注册或替换负责权限控制的AuthorizeHttpRequestsConfigurer实例。每次调用该方法,都会覆盖之前已配置好的所有权限规则,而非在原有规则基础上追加。

举个实际例子:
假设第一个权限配置是:

.authorizeHttpRequests(authorize -> authorize
    .requestMatchers("/public/**").permitAll()
    .anyRequest().authenticated()
)

后续又调用了第二次:

.authorizeHttpRequests(authorize -> authorize
    .anyRequest().permitAll()
)

最终生效的权限规则会是所有请求允许匿名访问,第一次配置的/public/**规则会被完全覆盖。

如果需要配置多条权限规则,正确的做法是在同一个.authorizeHttpRequests的Lambda表达式中完成所有规则定义,示例如下:

.authorizeHttpRequests(authorize -> authorize
    .requestMatchers("/public/**").permitAll()
    .requestMatchers("/admin/**").hasRole("ADMIN")
    .anyRequest().authenticated()
)

内容的提问来源于stack exchange,提问作者user3084686

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.03 12:02:33