2016-08-31 8 views
3

私は "ハックで"動作しているSPNEGOで春のセキュリティ構成を持っています。これは次のようになります。WebSecurityConfigurerAdapterカスタム認証フィルタ依存関係の問題

@Configuration 
@EnableWebSecurity 
public class SpnegoConfig extends WebSecurityConfigurerAdapter { 

    @Override 
    protected void configure(HttpSecurity http) throws Exception { 
     http 
       ... 
       .addFilterBefore(
         spnegoAuthenticationProcessingFilter(authenticationManagerBean()), 
         BasicAuthenticationFilter.class); // 1 
    } 

    @Override 
    @Autowired // 3 
    protected void configure(AuthenticationManagerBuilder auth) 
      throws Exception { 
     auth 
       .authenticationProvider(kerberosAuthenticationProvider()) 
       .authenticationProvider(kerberosServiceAuthenticationProvider()); 
    } 


    @Bean 
    public SpnegoAuthenticationProcessingFilter spnegoAuthenticationProcessingFilter(
      AuthenticationManager authenticationManager) { // 2 
     SpnegoAuthenticationProcessingFilter filter = 
       new SpnegoAuthenticationProcessingFilter(); 
     filter.setAuthenticationManager(authenticationManager); 
     return filter; 
    } 
    ... 
} 

何が起こっている:

  • 私はspnegoAuthenticationProcessingFilterを追加する必要があります(1)
  • このフィルタはauthenticationManagerに依存している私は、認証を追加する必要があります(2)
  • プロバイダ(3)

このクラスのポイントはWebSecurityConfigurerAdapterです。 2つのメソッドrriding:

  1. configure(HttpSecurity http)を - 私たちはそれを

を構築している場合 - これは明らかに何もまだ建設されてAuthenticationManagerに関連していない - これは、すでにカスタムフィルタ

  • configure(AuthenticationManagerBuilder auth)を通じてAuthenticationManagerを建てに依存しています私は方法(3)に@Autowiredを持っていませんAuthenticationManagerはあまりにも早く構築されており、AuthenticationProviderの私の追加は効果がありません。例外はありませんが、適切なAuthenticationProviderが存在しません。

    @Autowiredが正しく動作していますが、間違っていると思われます。それがなぜ始まったのかは分かりません。

    正しいアプローチについてアドバイスをお願いします。

    編集:実際に@Autowiredなしで動作します。しかし、その要点は受け入れられた答えにある。 AuthenticationManager@Configurationに設定している場合は、authenticationManagerBean()メソッドで公開または参照されていることを確認してください。

  • 答えて

    2

    AuthenticationManagerを間違って使用します。

    あなたが依存性注入とSpnegoConfigからAuthenticationManagerを使用したい場合、あなたはそれを公開する必要があり、JavaDoc次を参照してください。設定します(AuthenticationManagerBuilder)からAuthenticationManagerを公開する

    オーバーライドこのメソッドをビーンとして公開されます。たとえば、次のように

    @Bean(name name="myAuthenticationManager") 
    @Override 
    public AuthenticationManager authenticationManagerBean() throws Exception { 
        return super.authenticationManagerBean(); 
    } 
    

    グローバルAuthenticationManagerを設定したい場合、あなたはグローバル認証(つまりを設定したい場合にのみ必要があり、例えばSpring Security 3.2.0.RC2 Released

    を参照してください、AuthenticationMangerBuilderをautowireする必要が単一のAuthenticationManager)、AuthenticationMangerBuilderをオートワイヤする必要があります。

    @Autowired 
    public void configureGlobal(AuthenticationManagerBuilder auth) { 
        // ... configure it ... 
    }