Spring SecurityカスタムAuthenticationProvider不可@Autowired問題

930 ワード

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を自由に使用できるようになりました