Spring Security 6 相比旧版有哪些重要变化?(WebSecurityConfigurerAdapter 移除、Lambda DSL、授权规则匹配顺序)
- 标签:Spring Security、认证授权、后端
🏛 业务架构师
Spring Security 6 相比旧版(5.x)的重要变化,我按"迁移影响"排序:
- WebSecurityConfigurerAdapter 移除(最大变更):5.7 起废弃、6.0 彻底删除——继承 Adapter 重写 configure() 的老写法全部失效。新姿势是声明 SecurityFilterChain Bean(组件式配置),每条链一个 Bean,配合 Lambda DSL。这是升级 Spring Boot 3 时安全模块的主要改造量;
java
@Bean
SecurityFilterChain chain(HttpSecurity http) throws Exception {
http.authorizeHttpRequests(a -> a.requestMatchers("/api/**").authenticated())
.formLogin(Customizer.withDefaults());
return http.build();
}- Lambda DSL 成为主流(5.2 引入、6 全面推行):
.formLogin(form -> form.loginPage("/login"))链式 Lambda 替代.and()串联——可读性提升(一眼看清配置块),旧.and()写法已移除; - 授权规则与匹配顺序:antMatchers → requestMatchers 统一(同时兼容 Ant/ServletPath/MVC 匹配,MVC 场景下与 @RequestMapping 对齐更准);授权检查模型重构——
authorizeRequests换成authorizeHttpRequests(基于 AuthorizationManager 的新架构,拒绝时抛 AccessDeniedException 语义更清晰);多条规则的匹配是先声明先生效(first-match-wins),集中显式声明避免顺序歧义; - 其他:
@EnableMethodSecurity(prePostEnabled 默认开启)替代 @EnableGlobalMethodSecurity;OAuth2 登录/资源服务器配置简化;jakarta 命名空间适配(配合 Spring Boot 3)。
业务视角的迁移建议:这次重构的本质是"配置从继承式改为组件式"——迁移是机械性的(官方有迁移对照),典型应用半天到两天;真正的价值在于借迁移做一次安全配置审计:默认拒绝、CSRF 策略(JWT 链关、Cookie 链开)、会话策略逐项确认——升级是混沌窗口也是治理窗口,别只求编译通过,把安全基线一起校准。2026 年 Spring Boot 2.x 已 EOL,这次升级不是选择题。
🏗 系统架构师
Spring Security 6(配 Spring Boot 3)的核心变化,按"迁移痛度"排序:
- WebSecurityConfigurerAdapter 移除(最大迁移项):继承式配置 → 组件式声明:
java
// 5.x 老写法(已删)
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override protected void configure(HttpSecurity http) { ... }
}
// 6.x 新写法:直接声明 SecurityFilterChain Bean
@Bean
SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http.authorizeHttpRequests(a -> a
.requestMatchers("/api/**").authenticated()
.anyRequest().permitAll());
return http.build();
}一个应用声明多个 SecurityFilterChain Bean 即多链——配置从"框架回调"变为"依赖注入",更直观可测;
- Lambda DSL 全面推行:
http.csrf().disable()(方法链+返回 null 的隐患)→http.csrf(csrf -> csrf.disable())——lambda 形态在 6.x 成为文档标准(链式调用已废弃),意图更明确、编译器可查; - authorizeHttpRequests 替代 authorizeRequests:新 API 基于 AuthorizationManager,匹配顺序语义更严格(规则按声明顺序首个匹配生效——
anyRequest()必须最后);requestMatchers的 Ant 通配与 MVC 匹配器统一,尾部/**匹配语义变化(多 segment**只能尾部)是升级时的隐性坑; - 其他重要变化:
@EnableGlobalMethodSecurity→@EnableGlobalMethodSecurity移除,改@EnableMethodSecurity(默认启用 prePostEnabled);OAuth2 支持 OIDC Logout/认证细节强化;WebSecurityCustomizer替代 web.ignoring()(静态资源放行建议直接走 filter chain permitAll 而非 ignoring——安全链路一致性);jakarta 命名空间整体迁移(Servlet 5+)。
架构师视角:6.x 的主题是**"配置即组件 + 表达式现代化"**——声明式 Bean 化让安全配置可组合可测试(多链、按条件装配);迁移策略:逐项目机械重构(老 API 编译期就报错,风险可控),同时借升级审视默认值变化(Spring Security 历史上多次"默认更安全"导致行为变化——升级后必须过安全回归用例:匿名访问、越权、CSRF 行为矩阵)。
💻 全栈开发专家
Spring Security 6 相比 5.x 的重要变化(迁移必知):
1. WebSecurityConfigurerAdapter 移除(最大破坏性变更): 继承式配置全部改成组件式——@Bean SecurityFilterChain:
java
// 5.x 老写法(已死)
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override protected void configure(HttpSecurity http) { ... }
}
// 6.x 新写法
@Bean
SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http.authorizeHttpRequests(a -> a
.requestMatchers("/public/**").permitAll()
.anyRequest().authenticated())
.formLogin(withDefaults());
return http.build();
}2. Lambda DSL 成为一等公民(非 Lambda 重载在 7.x 已删除):
java
http.csrf(csrf -> csrf.disable())
.cors(cors -> cors.configurationSource(source))
.authorizeHttpRequests(auth -> auth.anyRequest().authenticated());好处:配置即代码、可读、防「误调用了重载导致没生效」。
3. 授权模型重构:authorizeRequests() → authorizeHttpRequests()(新 AuthorizationManager 体系,基于路径的 requestMatchers 替代 antMatchers/mvcMatchers);FilterSecurityInterceptor 退场、AuthorizationFilter 上位(6.x 起默认,7.x 唯一)。
4. 授权规则匹配顺序(要点也是坑):规则按声明顺序首个匹配生效(first-match-wins)——具体规则在前、宽泛规则在后:
java
http.authorizeHttpRequests(a -> a
.requestMatchers("/api/admin/**").hasRole("ADMIN") // 先具体
.requestMatchers("/api/**").authenticated()
.anyRequest().denyAll()); // 兜底建议 denyAll顺序写反 = 权限形同虚设(anyRequest().authenticated() 挡前面,ADMIN 规则永远不命中)。6.x 对无序/冲突的 matcher 会有告警,7.x 更严格。
5. 其他迁移项:@EnableGlobalMethodSecurity → @EnableMethodSecurity(默认启用 prePostEnabled);AntPathRequestMatcher → PathPatternRequestMatcher(性能更好);trailingWildcard 行为差异;SecurityContextPersistenceFilter → SecurityContextHolderFilter(不再自动写 session)。
迁移实操:先升 Spring Boot 3(带 Security 6)跑测试,修编译错误(IDE 全部能定位),重点回归「授权规则顺序」和「匿名访问路径」——这两处行为差异最隐蔽。