Requested bean is currently in creation: Is there an unresolvable circular reference? while using PasswordEncoder

huangapple go评论154阅读模式
英文:

Requested bean is currently in creation: Is there an unresolvable circular reference? while using PasswordEncoder

问题

@Configuration
@EnableWebSecurity
@AllArgsConstructor
public class SecurityConfig {
    private final UserDetailsService userDetailsService;

    @Bean
    public SecurityFilterChain filterChain(HttpSecurity httpSecurity) throws Exception {
        return httpSecurity
                .csrf(csrf -> csrf.disable())
                .authorizeHttpRequests(auth -> auth
                        .requestMatchers("/api/auth/**").permitAll()
                        .anyRequest().authenticated()
                )
                .build();
    }

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder authenticationManagerBuilder) throws Exception {
        authenticationManagerBuilder.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());
    }

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

    @Bean
    public AuthenticationManager authenticationManager(AuthenticationConfiguration authenticationConfiguration)
            throws Exception {
        return authenticationConfiguration.getAuthenticationManager();
    }
}

在运行上述代码时,我收到了错误消息:“Requested bean is currently in creation: Is there an unresolvable circular reference? using PasswordEncoder Bean why it i got this error how to solve this error.”

英文:
@Configuration
@EnableWebSecurity
@AllArgsConstructor
public class SecurityConfig  {


    private final UserDetailsService userDetailsService;



    @Bean
    public SecurityFilterChain filterChain(HttpSecurity httpSecurity) throws Exception {
        return httpSecurity
                .csrf(csrf -> csrf.disable())
                .authorizeHttpRequests(auth -> auth
                        .requestMatchers("/api/auth/**").permitAll()
                        .anyRequest().authenticated()
                )
                .build();
    }
    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder authenticationManagerBuilder) throws Exception {
        authenticationManagerBuilder.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());
    }
    @Bean
    PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Bean
    public AuthenticationManager authenticationManager(AuthenticationConfiguration authenticationConfiguration)
            throws Exception {
        return authenticationConfiguration.getAuthenticationManager();
    }
}

while running above code i got error Requested bean is currently in creation: Is there an unresolvable circular reference? using PasswordEncoder Bean why it i got this error how to solve this error.

答案1

得分: -1

循环依赖问题发生如下:

  1. SecurityConfig 类依赖于 UserDetailsService bean,这个 bean 很可能由 Spring Security 提供。
  2. UserDetailsService bean 需要 PasswordEncoder bean 来对密码进行编码和解码以进行身份验证。
  3. PasswordEncoder bean 反过来依赖于 SecurityConfig 类来创建 AuthenticationManager

这在依赖图中创建了一个循环,导致循环引用错误。

要解决这个错误,你可以通过对代码进行一些更改来打破循环依赖:

  1. PasswordEncoder bean 的创建移到一个单独的 @Configuration 类中。这样,它就不会参与到循环引用中。
@Configuration
public class PasswordEncoderConfig {
    @Bean
    PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }
}
  1. SecurityConfig 类的 configureGlobal 方法中移除 @Autowired 注解。Spring 将自动将 UserDetailsService 注入到 AuthenticationManagerBuilder 中,无需显式使用 @Autowired
@Configuration
@EnableWebSecurity
@AllArgsConstructor
public class SecurityConfig  {

    private final UserDetailsService userDetailsService;

    // ... (other methods)

    public void configureGlobal(AuthenticationManagerBuilder authenticationManagerBuilder) throws Exception {
        authenticationManagerBuilder.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());
    }

    // ... (other methods)
}

通过将 PasswordEncoder bean 分离到其配置类,并从 configureGlobal 方法中移除 @Autowired,你应该能够解决循环依赖问题,避免"Requested bean is currently in creation"错误。请记住检查代码库的其他部分,确保没有其他可能导致类似问题的循环依赖。

英文:

The circular dependency occurs as follows:

  1. The SecurityConfig class has a dependency on the UserDetailsService bean, which is likely provided by Spring Security.
  2. The UserDetailsService bean requires the PasswordEncoder bean to encode and decode passwords for authentication.
  3. The PasswordEncoder bean, in turn, depends on the SecurityConfig class to create the AuthenticationManager.

This creates a cycle in the dependency graph, leading to the circular reference error.

To solve this error, you can break the circular dependency by making some changes to the code:

  1. Move the PasswordEncoder bean creation to a separate @Configuration class. This way, it won't be involved in the circular reference.

    @Configuration
    public class PasswordEncoderConfig {
    @Bean
    PasswordEncoder passwordEncoder() {
    return new BCryptPasswordEncoder();
    }
    }

  2. Remove the @Autowired annotation from the configureGlobal method in the SecurityConfig class. Spring will automatically inject the UserDetailsService into the AuthenticationManagerBuilder without the need for explicit @Autowired.

    @Configuration
    @EnableWebSecurity
    @AllArgsConstructor
    public class SecurityConfig {

     private final UserDetailsService userDetailsService;
    
     // ... (other methods)
    
     public void configureGlobal(AuthenticationManagerBuilder authenticationManagerBuilder) throws Exception {
         authenticationManagerBuilder.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());
     }
    
     // ... (other methods)
    

    }

By separating the PasswordEncoder bean into its configuration class and removing the @Autowired from the configureGlobal method, you should be able to resolve the circular dependency issue and avoid the "Requested bean is currently in creation" error. Remember to check other parts of your codebase to ensure there are no other circular dependencies that could lead to similar issues.

huangapple
  • 本文由 发表于 2023年7月20日 17:06:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76728297.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定