原因一:
authenticationManager()和WebSecurityConfigurerAdapter的authenticationManagerBean()是两个不同的方法,并且您正在调用超类的 authenticationManagerBean() 方法,据我所知,这取决于 authenticationManager() 方法.这反过来会创建方法的循环调用,最终导致 StackOverflowError 异常
@Bean
@Override
public AuthenticationManager authenticationManager() throws Exception {
return super.authenticationManagerBean();
}
改为以下
@Bean(name = BeanIds.AUTHENTICATION_MANAGER)
@Override
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}
原因二:
实现UserDetailsService接口并且重写loadUserByUsername方法并注入到bean
注意:一定一定要在类上加上@Service否则还是会报错,然后再写自己的认证规则
@Service
public class UserDetailsServiceImpl implements UserDetailsService {
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
return new UserDetails();
}
}