在配置AuthenticationManager时,避免递归调用。
示例代码:
@Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private UserDetailsService userDetailsService;
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());
}
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
}
在此示例中,UserDetailsService对象被注入到AuthenticationManagerBuilder中。如果UserDetailsService与AuthenticationManagerBuilder互相注入,就会出现递归调用的情况,进而导致StackOverflowError错误。因此,应该避免这种情况的发生。