英文:
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
循环依赖问题发生如下:
SecurityConfig类依赖于UserDetailsServicebean,这个 bean 很可能由 Spring Security 提供。UserDetailsServicebean 需要PasswordEncoderbean 来对密码进行编码和解码以进行身份验证。PasswordEncoderbean 反过来依赖于SecurityConfig类来创建AuthenticationManager。
这在依赖图中创建了一个循环,导致循环引用错误。
要解决这个错误,你可以通过对代码进行一些更改来打破循环依赖:
- 将
PasswordEncoderbean 的创建移到一个单独的@Configuration类中。这样,它就不会参与到循环引用中。
@Configuration
public class PasswordEncoderConfig {
@Bean
PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
}
- 从
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:
- The
SecurityConfigclass has a dependency on theUserDetailsServicebean, which is likely provided by Spring Security. - The
UserDetailsServicebean requires thePasswordEncoderbean to encode and decode passwords for authentication. - The
PasswordEncoderbean, in turn, depends on theSecurityConfigclass to create theAuthenticationManager.
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:
-
Move the
PasswordEncoderbean creation to a separate@Configurationclass. This way, it won't be involved in the circular reference.@Configuration
public class PasswordEncoderConfig {
@Bean
PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
} -
Remove the
@Autowiredannotation from theconfigureGlobalmethod in theSecurityConfigclass. Spring will automatically inject theUserDetailsServiceinto theAuthenticationManagerBuilderwithout 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论