2017-12-12 14 views
1

既存のSpring Security(バージョン3.2.10)XML設定をJavaベースの設定に変換中です。私は交換しています XML-構成が設定された認証マネージャを持っていますバネセキュリティで認証マネージャを注入するとデリゲートビルダーがnullになる

<authentication-manager alias="authenticationManager"> 
    <authentication-provider ref="kerberosServiceAuthenticationProvider"/> 
    <authentication-provider ref="samlAuthenticationProvider"/> 
    <authentication-provider ref="pkiAuthenticationProvider"/> 
    <authentication-provider ref="openIdConnectAuthenticationProvider"/> 
</authentication-manager> 

私のJava構成と同等である:

@Configuration 
@EnableWebSecurity 
public class SecurityConfiguration extends WebSecurityConfigurerAdapter 
{ 
    @Autowired 
    public void configure(AuthenticationManagerBuilder auth) throws Exception 
    { 
     auth.authenticationProvider(kerberosServiceAuthenticationProvider()) 
     .authenticationProvider(samlAuthenticationProvider()) 
     .authenticationProvider(pkiAuthenticationProvider()) 
     .authenticationProvider(openIdConnectAuthenticationProvider()); 
    } 
} 

認証マネージャは、他の豆を構築する上での別名で呼ばれているように、私

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

提案されているように、認証マニガービーンをオーバーライドしました。 How To Inject AuthenticationManager using Java Configuration in a Custom Filter にしかし、このBeanの作成については、次の例外がスローされます。

Caused by: java.lang.IllegalArgumentException: delegateBuilder cannot be null 
at org.springframework.util.Assert.notNull(Assert.java:112) 
at org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter$AuthenticationManagerDelegator.<init>(WebSecurityConfigurerAdapter.java:426) 
at org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter.authenticationManagerBean(WebSecurityConfigurerAdapter.java:220) 

デリゲートビルダーは、スニペットは、スーパーの実装である(Beanをオーバーライドするとき、最初の引数として使用されている認証ビルダーです。 authenticationManagerBean())。これはnullです。

public AuthenticationManager authenticationManagerBean() throws Exception { 
     return new AuthenticationManagerDelegator(authenticationBuilder, context); 
} 

このBeanが作成されると、何かが見当たりません。

@Autowired 
public void setObjectPostProcessor(ObjectPostProcessor<Object> objectPostProcessor) 
{...} 

をしかし、それは呼び出されません(と上書きすることを意図していないようです):このデリゲートビルダーだけWebSecurityConfigurerAdapterに、この方法で設定されています。 私はconfigureメソッドがまだ呼び出されていないことに気付きました。 私は明らかに何か不足していますが、それが何であるか分かりません。

+0

すでに 'configure(AuthenticationManagerBuilder auth)'でBeanを公開しています。 Beanとして2つの 'AuthenticationManager'が必要ですか? – dur

+0

いいえ、私はそうではありません:)だから、ビルダーから認証マネージャーをBeanとしてどのように公開すべきですか?私のセキュリティ設定でautowiredにすることはできますか? authオブジェクトの再利用とgetOrBuild()の呼び出しは機能しないため、認証マネージャーBeanを必要とする他のBeanが作成される前にconfigureメソッドが呼び出されないためです。 – DennisV

+0

同様の質問については私の[answer](https://stackoverflow.com/a/39259519/5277820)を参照してください。 – dur

答えて

1

AuthenticationManagerBuilderに問題がある場合は、IllegalArgumentException: delegateBuilder cannot be nullというエラーが発生して解決できない可能性があります。これはAuthenticationManagerBuilderで簡単に実行できます。代わりに、AuthenticationManagerをBeanとして公開することをお勧めします。AuthenticationManagerBuilderはまったく使用しないでください。例えば

@Bean 
public ProviderManager authenticationManager() { 
    return new ProviderManager(Arrays.asList(
     kerberosServiceAuthenticationProvider(), 
     samlAuthenticationProvider(), 
     pkiAuthenticationProvider(), 
     openIdConnectAuthenticationProvider()); 
} 

それでも問題が解決しない場合、あなたはまたに定義することができます春のセキュリティの認証(すなわちAuthenticationManagerAuthenticationProviderPasswordEncoder、など)静的に定義するメソッドを作ってみることができますクラス全体を初期化せずにロードすることができます。問題が解決しない場合は、AuthenticationManagerAuthenticationProviderの設定をすべて別の設定クラスに移行することをおすすめします。

+0

最初の提案は実際に問題を解決しました! – DennisV

0

私はSpring Boot 2に移行する間に全く同じエラーが発生しました(このプロセスでは、Spring Security 4からSpring Security 5.0.0に移行し、XML設定をJavaConfigに変換する必要があります)。 これはあなたに当てはまるかどうかわかりません。春のセキュリティ5では、authenticationBuilderはWebSecurityConfigurerAdapterの方法で初期化されていること:

​​

したがって、コンテキストを注入しようとする必要があります。しかし、AuthenticationManager Beanを定義する@ Beanアノテーション付きのメソッドは、そのメソッドの前に常に呼び出され、同じエラーが発生します。 私は単に他のすべての前にメソッドの呼び出しを強制ApplicationContextAwareインターフェース、実装することにより、という固定:

public class MySecurityConfigurerAdapter extends WebSecurityConfigurerAdapter implements ApplicationContextAware { 
/*...*/ 
@Override 
protected void configure(AuthenticationManagerBuilder auth) throws Exception { 
    auth 
      .authenticationProvider(dbAuthenticationProvider) 
      .authenticationProvider(otherAuthProvider); 
} 

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

を私は背後にある正確な理由を説明することはできません、私は新しい「正しい」方法を見つけることができませんでしたドキュメントでそれを行うには、少なくともそれは私のために働いた。

関連する問題