【问题标题】: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()));
}

【讨论】:

    猜你喜欢
    • 2011-01-20
    • 2017-05-27
    • 2012-01-28
    • 2014-10-06
    • 2017-01-09
    • 1970-01-01
    • 2015-10-16
    • 2013-10-11
    • 2016-03-17
    相关资源
    最近更新 更多