变量Spring Security身份验证提供程序列表

时间:2014-08-13 23:10:18

标签: java spring authentication spring-security

我想有条件地将身份验证提供程序添加到我的身份验证管理器中,并且无法确定如何执行此操作。

假设我定义了以下身份验证管理器:

<security:authentication-manager id="myAuthenticationManager" erase-credentials="true">
    <security:authentication-provider ref='ldapAuthenticationProvider'/>
    <security:authentication-provider ref='adLdapAuthenticationProvider'/>
    <security:authentication-provider user-service-ref='dbAthenticationService'>
        <security:password-encoder ref="myPasswordDigest">
            <security:salt-source ref="saltSource"/>
        </security:password-encoder>
    </security:authentication-provider>
</security:authentication-manager>

就像这样,Spring Security正在运作;我可以针对LDAP,Active Directory或我的数据库进行身份验证。我也可以选择删除其中一个提供商,但事情仍然可以正常运行,所以这一切都很好。

但是,我希望使用Spring配置文件,我可以动态填充身份验证提供程序列表。因此,例如,在我的应用程序上下文结束时,我有

<beans profile="db">
   <!-- Somehow add the following to myAuthenticationManager
    <security:authentication-provider user-service-ref='dbAthenticationService'>
        <security:password-encoder ref="myPasswordDigest">
            <security:salt-source ref="saltSource"/>
        </security:password-encoder>
    </security:authentication-provider> -->
</beans>
<beans profile="ldap">
    <!-- Somehow add the following to myAuthenticationManager
    <security:authentication-provider ref='ldapAuthenticationProvider'/> -->
</beans>
<beans profile="ad">
    <!-- Somehow add the following to myAuthenticationManager
    <security:authentication-provider ref='adLdapAuthenticationProvider'/> -->
</beans>

我已经有了配置文件切换机制,并且每个这些Prodviders都是正确配置的。我只想动态填充在初始化时使用的列表。这可能吗?

由于

1 个答案:

答案 0 :(得分:0)

一种解决方案可能是注册单个身份验证提供程序。此提供程序(请参阅下文)引用了所有身份验证提供程序并转发给了正确的提供程序。

我没有测试解决方案。

public class CompositeAuthenticationProvider implements AuthenticationProvider {

    private final List<AuthenticationProvider> providers = new ArrayList<>();

    @Override
    public Authentication authenticate(Authentication authentication) throws AuthenticationException {
        final AuthenticationProvider provider = getAuthenticationProvider(authentication.getClass());

        if(provider == null){
            throw new ProviderNotFoundException("The authentication [" +authentication
                  + "] is not supported by any provider");
        }

        return null;
    }

    @Override
    public boolean supports(final Class<?> authentication) {
        return getAuthenticationProvider(authentication) != null;
    }

    private AuthenticationProvider getAuthenticationProvider(Class<?> authentication) {
        for (AuthenticationProvider provider : providers) {
            if (provider.supports(authentication)) {
                return provider;
            }
        }
        return null;
    }

    @Autowired
    public void setProviders(List<AuthenticationProvider> providers) {
        this.providers.addAll(providers); // you could order them with @Order, just use a sorter
    }
}

XML定义:

<bean id="compositeAuthenticationProvider"
      class="CompositeAuthenticationProvider" autowire-candidate="false"/>
相关问题