bigywater.tech
Spring · 152

Spring Security 7:旧写法编译不过的清单与替换

Spring Security 7 删掉了哪些旧写法?升级后哪些配置编译不过、替换成什么?这篇用 Security 6.5 与 7.0 把 32 种常见写法逐个编译,再用同一份新配置对比两个版本的路径授权结果。

14 / 32
Security 7.0 上能编译通过的写法数(Security 6.5 上是 27 / 32)
2 / 10
HttpSecurity 的 10 种配置写法里 7.0 只通过两种 lambda DSL
0
同一份 lambda 配置在 6.5 与 7.0 上访问 12 个路径,状态码不同的路径数

HttpSecurity

实测 · 本机执行输出
  Security 6.5(Boot 3.5.16):通过 10 / 10;Security 7.0(Boot 4.0.8):通过 2 / 10
    lambda DSL:http.csrf(c -> c.disable()):6.5→通过;7.0→通过
    lambda DSL:authorizeHttpRequests(a -> a.requestMatchers("/a").permitAll().anyRequest().authenticated()):6.5→通过;7.0→通过
    旧写法:http.csrf().disable():6.5→通过;7.0→失败(method csrf in class HttpSecurity cannot be ap)   ← 不同
    旧写法:http.authorizeHttpRequests().anyRequest().authenticated():6.5→通过;7.0→失败(method authorizeHttpRequests in class HttpSecu)   ← 不同
    旧写法:http.authorizeRequests():6.5→通过;7.0→失败(cannot find symbol)   ← 不同
    旧写法:http.formLogin().and().httpBasic():6.5→通过;7.0→失败(method formLogin in class HttpSecurity cannot )   ← 不同
    旧写法:http.formLogin() 无参:6.5→通过;7.0→失败(method formLogin in class HttpSecurity cannot )   ← 不同
    旧写法:http.httpBasic() 无参:6.5→通过;7.0→失败(method httpBasic in class HttpSecurity cannot )   ← 不同
    旧写法:http.headers().frameOptions().disable():6.5→通过;7.0→失败(method headers in class HttpSecurity cannot be)   ← 不同
    旧写法:http.sessionManagement().sessionCreationPolicy(…):6.5→通过;7.0→失败(method sessionManagement in class HttpSecurity)   ← 不同

无参重载与 .and() 链式在 7.0 里没了,只剩 lambda DSL。

匹配器与拦截器

实测 · 本机执行输出
  Security 6.5(Boot 3.5.16):通过 10 / 11;Security 7.0(Boot 4.0.8):通过 5 / 11
    AntPathRequestMatcher:6.5→通过;7.0→失败(cannot find symbol)   ← 不同
    AntPathRequestMatcher.antMatcher(…):6.5→通过;7.0→失败(cannot find symbol)   ← 不同
    PathPatternRequestMatcher.withDefaults().matcher(…):6.5→通过;7.0→通过
    MvcRequestMatcher:6.5→通过;7.0→失败(cannot find symbol)   ← 不同
    FilterSecurityInterceptor:6.5→通过;7.0→失败(cannot find symbol)   ← 不同
    AccessDecisionManager:6.5→通过;7.0→失败(cannot find symbol)   ← 不同
    WebSecurityCustomizer:6.5→通过;7.0→通过
    http.requestMatchers(…) 链式 securityMatcher:6.5→通过;7.0→通过
    @EnableGlobalMethodSecurity:6.5→通过;7.0→通过
    @EnableMethodSecurity:6.5→通过;7.0→通过
    WebSecurityConfigurerAdapter:6.5→失败(cannot find symbol);7.0→失败(cannot find symbol)

AntPathRequestMatcher、MvcRequestMatcher、FilterSecurityInterceptor、AccessDecisionManager 在 7.0 里没了,PathPatternRequestMatcher 是替代。

认证与编码器

实测 · 本机执行输出
  Security 6.5(Boot 3.5.16):通过 8 / 11;Security 7.0(Boot 4.0.8):通过 7 / 11
    DaoAuthenticationProvider 无参构造:6.5→通过;7.0→失败(constructor DaoAuthenticationProvider in class)   ← 不同
    DaoAuthenticationProvider(UserDetailsService):6.5→通过;7.0→通过
    Pbkdf2PasswordEncoder 无参构造:6.5→失败(no suitable constructor found for Pbkdf2Passwo);7.0→失败(no suitable constructor found for Pbkdf2Passwo)
    Pbkdf2PasswordEncoder.defaultsForSpringSecurity_v5_8():6.5→通过;7.0→通过
    SCryptPasswordEncoder 无参构造:6.5→失败(constructor SCryptPasswordEncoder in class SCr);7.0→失败(constructor SCryptPasswordEncoder in class SCr)
    Argon2PasswordEncoder 无参构造:6.5→失败(constructor Argon2PasswordEncoder in class Arg);7.0→失败(constructor Argon2PasswordEncoder in class Arg)
    BCryptPasswordEncoder 无参构造:6.5→通过;7.0→通过
    StandardPasswordEncoder:6.5→通过;7.0→通过
    Md4PasswordEncoder:6.5→通过;7.0→通过
    NoOpPasswordEncoder.getInstance():6.5→通过;7.0→通过
    User.withDefaultPasswordEncoder():6.5→通过;7.0→通过

DaoAuthenticationProvider 必须传 UserDetailsService;几个编码器的无参构造两个版本都不能用。

行为对比

实测 · 本机执行输出
  路径 → 状态码(401 = 被安全过滤器挡住,200 = 到达了 Controller)
    /admin/x         6.5 → 401;7.0 → 401
    /admin           6.5 → 401;7.0 → 401
    /admin/          6.5 → 401;7.0 → 401
    /admin//x        6.5 → 400;7.0 → 400
    /admin/./x       6.5 → 400;7.0 → 400
    /admin/x/        6.5 → 401;7.0 → 401
    /ADMIN/x         6.5 → 200;7.0 → 200
    /admin;a=b/x     6.5 → 400;7.0 → 400
    /%61dmin/x       6.5 → 401;7.0 → 401
    /admin%2Fx       6.5 → 400;7.0 → 400
    /admin/x.json    6.5 → 401;7.0 → 401
    /public          6.5 → 200;7.0 → 200

新写法配置在两个版本上路径授权结果一致。

分步演示

Security 7
本步的对象

    本文用到的工具与边界

    • 本机 macOS 27 arm64 / OpenJDK 21.0.12(Homebrew) / Spring Boot 4.0.8(Spring Framework 7.0.9、Tomcat 11.0.24) / Spring Cloud 2025.1.3;用 javac 编译、java 直接运行,依赖由 Maven 从 Maven Central 解析;数字每次运行会有小幅抖动(对比版本 Spring Boot 3.5.16 带的 Spring Security 6.5.11 与 Spring Boot 4.0.8 带的 Spring Security 7.0.7;每种写法是一个独立的 .java 文件,在两个版本上分别用 javac 编译;行为部分是同一份 lambda DSL 配置的最小应用,用 http.client 直接发请求)
    • 没有验证:OAuth2 登录、资源服务器、SAML、Spring Authorization Server 相关 API 的变化;WebFlux 版 Security 的写法;PathPatternRequestMatcher 与 AntPathRequestMatcher 在更多路径(尾部斜杠、大小写、后缀)上的匹配差异;@EnableGlobalMethodSecurity 在 7.0 里实际是否还生效(只验证了能编译);7.0 里新增的默认值与安全头变化;写法被删掉的完整清单(只测了 32 种)
    京ICP备2026067502号-1京公网安备11010502063487号