【问题标题】:spring security custom inject AuthenticationProvider inside AuthenticationManagerspring security 自定义在 AuthenticationManager 中注入 AuthenticationProvider
【发布时间】:2016-12-07 17:28:15
【问题描述】:
可能是一个新手问题。我想在 Spring AuthenticationManager 中注入 CustomAuthenticationProviderInside。我在网上找到了很多这样做的例子:
<authentication-manager>
<authentication-provider ref="CustomAuthenticationProvider"/>
</authentication-manager>
如何使用 Java Config 类做到这一点?
【问题讨论】:
标签:
java
spring
authentication
spring-security
spring-java-config
【解决方案1】:
Spring 提供了 AuthenticationManager 的一种默认实现,即 ProviderManager。 ProviderManager 有一个构造函数,它接受一组身份验证提供程序
public ProviderManager(List<AuthenticationProvider> providers) {
this(providers, null);
}
如果你愿意,你可以通过扩展 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 安全配置中添加您的自定义身份验证管理器。
@Override
protected AuthenticationManager authenticationManager() throws Exception {
return new MyAuthenticationManager(Arrays.asList(new CustomAuthenticationProvider()));
}