2017-02-09 4 views
0

私は、AuthenticationProvidersがSpringアプリケーションに複数あるとします。スプリングセキュリティが認証プロバイダのリストを実行する方法の順序とロジックを制御する

@Autowired 
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { 
    auth.authenticationProvider(AAuthenticationProvider()); 
    auth.authenticationProvider(BAuthenticationProvider()); 
    auth.authenticationProvider(CAuthenticationProvider());  
} 

デフォルトでは、スプリング・セキュリティは、非ヌル・レスポンスを提供するまで、これらのプロバイダを順番に試します。

質問:リストプロバイダの実行順序とロジックを変更できる場合は、この動作をカスタマイズできますか?

ProviderManagerはどういうわけかこのロジックを実行しているようです。この動作を無効にすることは可能でしょうか?

答えて

0

auth.authenticationProviderでプロバイダを追加する順序の順序を変更するのではなく、順序を操作する便利な方法はないと思います。

ビルダーには、auth.authenticationProviderを呼び出すたびに最後に貼り付けられるArrayListがあります。評価は、これらのプロバイダがリストに追加されるのと同じ順序で行われます。

希望すると便利です。

0

私は春から直接解決策を見つけることができませんでした。私は習慣を作る能力などを見つけることを望んでいたProviderManager。私の回避策は、1つを作成することです親認証プロバイダと1つの親UserDetailsS​​erviceすべてのUserDetailsS​​ervicesのフローを制御できます。

あなたは、設定クラスには、次のものが含まれます:

@Configuration 
@EnableWebSecurity 
public class SecConfig extends WebSecurityConfigurerAdapter { 
@Autowired 
UserDetailsService parentUserDetailsService; 

    @Autowired 
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { 
     auth.authenticationProvider(ParentAuthenticationProvider());  
    } 

    @Bean 
    public DaoAuthenticationProvider ParentAuthenticationProvider() { 
     DaoAuthenticationProvider authenticationProvider = new DaoAuthenticationProvider(); 
    authenticationProvider.setUserDetailsService(parentUserDetailsService); 
    return authenticationProvider; 
} 
} 

親サービスは、すべての子のサービスにアクセスできるようになります。

@Service 
public class ParentUserDetailsService implements UserDetailsService { 

@Autowired 
UserDetailsService aUserDetailsService; 

@Autowired 
UserDetailsService bUserDetailsService; 

@Autowired 
UserDetailsService cUserDetailsService; 

@Override 
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { 

    UserDetails user = null; 
    /* your logic will be here. 
     You iterate through all of the services 
     or have some conditional flow. the sky is your limit! 
    */ 
    // For Example 
    if(cond1) 
     user = aUserDetailsService.loadUserByUsername(username); 
    else(cond2){ 
     try{ 
      user = bUserDetailsService.loadUserByUsername(username); 
     }catch(Exception e){ 
     user = cUserDetailsService.loadUserByUsername(username); 
     } 
    } 

    return user; 

} 

これが最適な解決策であるかどうかはわかりませんが、私の場合はうまくいきました。

関連する問題