美文网首页
Spring Security 自定义 Authenticati

Spring Security 自定义 Authenticati

作者: Epiphanic | 来源:发表于2018-10-10 12:25 被阅读0次

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了

相关文章

网友评论

      本文标题:Spring Security 自定义 Authenticati

      本文链接:https://www.haomeiwen.com/subject/xvjpaftx.html