Spring Security 弃用问题

Yog*_*Rao 20 java spring spring-security spring-boot

尝试配置 JWT 配置。看来 JWT 已被弃用。OAuth2ResourceServerConfigurer::jwt我现在该如何使用?

我的代码:

@Bean
SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
    http.authorizeHttpRequests((requests) -> requests.anyRequest().authenticated());
    http.sessionManagement((session) -> session.sessionCreationPolicy(SessionCreationPolicy.STATELESS));
    //http.formLogin(withDefaults());
    http.httpBasic(Customizer.withDefaults());
    http.csrf(csrf -> csrf.disable());
    http.headers(headers -> headers.frameOptions(frameOptionsConfig -> frameOptionsConfig.sameOrigin()));
    http.oauth2ResourceServer(OAuth2ResourceServerConfigurer::jwt);
    return http.build();
}
Run Code Online (Sandbox Code Playgroud)

此外,在 Spring Security 6.0 中,antMatchers()以及其他用于保护请求的配置方法(即mvcMatchers()regexMatchers())已从 API 中删除。

Mar*_*gio 32

除了 @schrom 答案以及与 的弃用相关的更多内容之外OAuth2ResourceServerConfigurer#jwt,Spring Security 还弃用了返回其自身配置器的方法,转而使用返回的方法HttpSecurity,并弃用.and()HttpSecurity.

例如,httpBasic()已被弃用,取而代之的是httpBasic(Customizer). 进行这些弃用是为了只有一种方法来配置安全 DSL,即使用 lambda。查看文档

因此,对于 JWT 配置,您必须执行以下操作:

oauth2ResourceServer((oauth2) -> oauth2
    .jwt(Customizer.withDefaults())
)
Run Code Online (Sandbox Code Playgroud)


sch*_*rom 6

Spring 的一般建议是先迁移到 Spring 5.8,然后再迁移到 6.0,以便更平滑地过渡到新功能。

如Spring Security 5.8 文档中所述:

在 Spring Security 5.8 中,已弃用 antMatchers、mvcMatchers 和 regexMatchers 方法,取而代之的是新的 requestMatchers 方法

据我所知http.oauth2ResourceServer(OAuth2ResourceServerConfigurer::jwt)应该仍然有效,甚至在 Spring Security 6.0 文档中提到了有关 JWT 的内容

通常,Spring 类有关于不推荐使用的方法的详细文档,即 JavaDoc 经常给出使用哪个类或方法的提示。