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())); 
} 
相关问题