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

Spring Security 6.2中如何替换已废弃的http.apply(new OAuth2AuthorizationServerConfigurer())?

Spring Security 6.2 中替代 http.apply() 配置授权服务器的方案

在Spring Security 6.2版本中,http.apply()方法已被废弃,针对OAuth2授权服务器的配置,现在可以直接通过HttpSecurity的链式API中的oauth2AuthorizationServer()方法来替代,无需手动创建OAuth2AuthorizationServerConfigurer实例再调用apply。

替代后的代码示例如下:

@Bean
public SecurityFilterChain authorizationServerSecurityFilterChain(HttpSecurity http) throws Exception {
    http
        // 配置请求授权规则
        .authorizeHttpRequests(authorize -> authorize
            .anyRequest().authenticated()
        )
        // 直接通过oauth2AuthorizationServer方法获取配置器进行配置
        .oauth2AuthorizationServer(oauth2 -> oauth2
            .registeredClientRepository(registeredClientRepository)
            // 可在此继续添加其他授权服务器相关配置,例如token设置、授权设置等
        );
    
    return http.build();
}

这种方式是新版本Spring Security推荐的配置方式,将授权服务器的配置直接融入HttpSecurity的链式调用中,代码结构更简洁,也符合框架的API演进方向。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.21 16:43:09