0

可能是一個新手問題。我想注入CustomAuthenticationProviderInside Spring AuthenticationManager。我在網上發現了很多例子:春季安全自定義注入身份驗證管理器內AuthenticationProvider

<authentication-manager> 

    <authentication-provider ref="CustomAuthenticationProvider"/> 

</authentication-manager> 

我該如何使用Java Config類來做到這一點?

+0

請參考CustomerAuthenticationManager示例。 http://stackoverflow.com/questions/31826233/custom-authentication-manager-with-spring-security-and-java-configuration –

回答

0

Spring提供了一個AuthenticationManager的默認實現,它是ProviderManager。 ProviderManager中有一個構造函數這需要身份驗證提供的陣列通過擴展的ProviderManager

public class MyAuthenticationManager extends ProviderManager implements AuthenticationManager{ 

public MyAuthenticationManager(List<AuthenticationProvider> providers) { 
    super(providers); 
    providers.forEach(e->System.out.println("Registered providers "+e.getClass().getName())); 
    } 
} 

然後我的Java安全配置

public ProviderManager(List<AuthenticationProvider> providers) { 
    this(providers, null); 
} 

如果你想你可以用它玩,你可以添加自定義的認證管理器。

@Override 
protected AuthenticationManager authenticationManager() throws Exception { 
    return new MyAuthenticationManager(Arrays.asList(new CustomAuthenticationProvider())); 
} 
相關問題