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

Spring Boot社交登录教程运行失败:找不到RegisteredClientRepository Bean

问题原因及解决办法

这个错误的核心原因是你的项目引入了Spring Authorization Server相关依赖,但未配置它必需的RegisteredClientRepository Bean——但你实际要实现的是普通OAuth2社交登录(GitHub登录),根本不需要搭建自己的授权服务器。

具体问题拆解:

  • 依赖错配:你大概率在pom.xml或build.gradle中添加了spring-security-oauth2-authorization-server依赖,这个依赖是用来搭建独立OAuth2授权服务器的,而实现社交登录只需要spring-boot-starter-oauth2-client就足够。
  • 配置冗余:你的application.yml里配置了Okta的provider信息,但你实际用的是GitHub登录,这部分配置完全多余;且只有授权服务器才需要管理RegisteredClient,普通客户端不需要这层配置。
  • Security配置错误:如果你的SecurityConfig继承了授权服务器相关配置类(比如旧版的AuthorizationServerConfigurerAdapter),或错误启用了授权服务器的配置逻辑,会触发框架对RegisteredClientRepository Bean的加载要求。

快速修复步骤

  1. 清理依赖:移除spring-security-oauth2-authorization-server依赖,确保仅保留spring-boot-starter-oauth2-client。
  2. 简化配置:删除application.yml里的okta相关配置和oauth2.provider.okta部分,只保留GitHub的注册配置(注意GitHub不支持openid scope,需要移除):
spring:
  application:
    name: myapp
  security:
    oauth2:
      client:
        registration:
          github:
            clientId: ${GITHUB_CLIENT_ID}
            clientSecret: ${GITHUB_CLIENT_SECRET}
            scope:
              - profile
              - email
  1. 修正SecurityConfig:确保配置是针对OAuth2 Client的,比如Spring Security 5.7+的写法:
@Configuration
@EnableWebSecurity
public class SecurityConfig {

    @Bean
    public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
        http
            .authorizeHttpRequests(auth -> auth
                .anyRequest().authenticated()
            )
            .oauth2Login(oauth2 -> oauth2
                .loginPage("/login") // 可选,自定义登录页路径
            );
        return http.build();
    }
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.17 03:22:37