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

Spring Security客户端Unsatisfied Dependency Exception问题求助

Spring Security客户端Unsatisfied Dependency Exception排查

我正在学习Spring Security,运行客户端应用时遭遇Unsatisfied Dependency Exception,无法定位问题。授权服务运行正常,推测问题出在Spring Security客户端,现附上错误堆栈信息、WebSecurityConfig.java代码及application.yaml配置,请求协助排查解决。

错误堆栈信息

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.security.config.annotation.web.configuration.WebSecurityConfiguration': Unsatisfied dependency expressed through method 'setFilterChains' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'securityFilterChain' defined in class path resource [com/himanshu/spring/security/config/WebSecurityConfig.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.security.web.SecurityFilterChain]: Factory method 'securityFilterChain' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'clientRegistrationRepository' defined in class path resource 

WebSecurityConfig.java代码

package com.himanshu.spring.security.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.Customizer;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.stereotype.Component;

@EnableWebSecurity
@Configuration
public class WebSecurityConfig {

    private static final String[] WHITE_LISTS = {
            "/hello*",
            "/register*",
            "/verifyRegistration*",
            "/resendVerificationToken*"
    };

    @Bean
    public PasswordEncoder passwordEncoder(){
        return new BCryptPasswordEncoder(11);
    };

    @Bean
    SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception{
        http.
                cors()
                .and()
                .csrf()
                .disable()
                .authorizeHttpRequests()
                .antMatchers(WHITE_LISTS).permitAll()
                .antMatchers("/api/**").authenticated()
                .and()
                .oauth2Login(oauth2Login ->
                        oauth2Login.loginPage("/oauth2/authorization/api-client-oidc"))
                .oauth2Client(Customizer.withDefaults());

        return http.build();
    }
}

application.yaml配置

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/usr_reg
    username: root
    password: Hima@63546
    driver-class-name: com.mysql.cj.jdbc.Driver

  jpa:
    show-sql: true
    hibernate:
      ddl-auto: update

  security:
    oauth2:
      client:
        registration:
          api-client-oidc:
            provider: spring
            client-id: api-client
            client-secret: secret
            authorization-grant-type: authorization_code
            redirect-uri: "http://127.0.0.1:8080/login/oauth2/code/{registrationId}"
            scope: openid
            client-name: api-client-oidc
          api-client-authorization-code:
            provider: spring
            client-id: api-client
            client-secret: secret
            authorization-grant-type: authorization_code
            redirect-uri: "http://127.0.0.1:8080/authorized"
            scope: api.read
            client-name: api-client-authorization-code
        provider:
          spring:
            issuer-url: http://auth-server:9090

排查思路与解决方法

1. 补充OAuth2客户端依赖

clientRegistrationRepository由Spring Security OAuth2客户端模块提供,未引入依赖会导致Bean创建失败:

  • Maven项目在pom.xml添加:
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-oauth2-client</artifactId>
</dependency>
  • Gradle项目在build.gradle添加:
implementation 'org.springframework.boot:spring-boot-starter-oauth2-client'

2. 修复授权服务地址可达性

配置中http://auth-server:9090可能无法解析:

  • 若授权服务在本地运行,直接改为http://localhost:9090;
  • 若在远程机器,修改hosts文件添加映射(如Windows系统在C:\Windows\System32\drivers\etc\hosts添加127.0.0.1 auth-server)。

3. 移除冗余注解

WebSecurityConfig类上的@Component注解多余,@Configuration已包含组件扫描功能,移除后避免冲突:

package com.himanshu.spring.security.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.Customizer;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.SecurityFilterChain;

@EnableWebSecurity
@Configuration
public class WebSecurityConfig {

    private static final String[] WHITE_LISTS = {
            "/hello*",
            "/register*",
            "/verifyRegistration*",
            "/resendVerificationToken*"
    };

    @Bean
    public PasswordEncoder passwordEncoder(){
        return new BCryptPasswordEncoder(11);
    }

    @Bean
    SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception{
        http
                .cors(Customizer.withDefaults())
                .csrf(csrf -> csrf.disable())
                .authorizeHttpRequests(auth -> auth
                        .antMatchers(WHITE_LISTS).permitAll()
                        .antMatchers("/api/**").authenticated())
                .oauth2Login(oauth2Login ->
                        oauth2Login.loginPage("/oauth2/authorization/api-client-oidc"))
                .oauth2Client(Customizer.withDefaults());

        return http.build();
    }
}

4. 验证客户端配置一致性

  • 确保client-id和client-secret与授权服务注册信息完全匹配;
  • 确认redirect-uri在授权服务允许的回调地址列表内;
  • 检查授权服务是否正常启动,且能通过issuer-url访问OIDC配置端点(如http://localhost:9090/.well-known/openid-configuration)。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.25 01:54:25