Spring Security 自定义 AuthenticationProvider无法@Autowired问题
在AuthenticationProvider中使用@Autowired注入时始终报Null问题
找了半天发现应该在SecurityConfig配置类中
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter{
在设置AuthenticationProvider时应该使用@Bean的方式设置
@Bean
CustomAuthenticationProvider customAuthenticationProvider() {
return new CustomAuthenticationProvider();
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.authenticationProvider(customAuthenticationProvider());
}
之前的错误的设置方式是:
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.authenticationProvider(new CustomAuthenticationProvider());
}
以上可以在实现AuthenticationProvider时自由的使用@Autowired了
作者:Epiphanic
链接:https://www.jianshu.com/p/1fdf50175935
來源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。