Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/396.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用Spring Security在运行时切换身份验证方法?_Java_Spring_Authentication_Jakarta Ee_Spring Security - Fatal编程技术网

Java 使用Spring Security在运行时切换身份验证方法?

Java 使用Spring Security在运行时切换身份验证方法?,java,spring,authentication,jakarta-ee,spring-security,Java,Spring,Authentication,Jakarta Ee,Spring Security,通常,当您为应用程序声明不同的“”时(在我的例子中是webapp),Spring Security会在出现故障时一个接一个地调用提供者。因此,假设我在配置文件中首先声明了DatabaseAuthenticationProvider和LDAPAuthenticationProvider,在运行时,首先调用DatabaseAuthenticationProvider,如果身份验证失败,则尝试LDAPAuthentication。这很酷-但是,我需要的是一个运行时开关 我想在这两种方法(基于数据库的身

通常,当您为应用程序声明不同的“”时(在我的例子中是webapp),Spring Security会在出现故障时一个接一个地调用提供者。因此,假设我在配置文件中首先声明了DatabaseAuthenticationProvider和LDAPAuthenticationProvider,在运行时,首先调用DatabaseAuthenticationProvider,如果身份验证失败,则尝试LDAPAuthentication。这很酷-但是,我需要的是一个运行时开关

我想在这两种方法(基于数据库的身份验证/基于ldap的身份验证)之间进行选择,并以某种方式切换基于thsi全局设置的实现


我该怎么做?甚至可以使用Spring Security吗?

编写一个授权AuthenticationProvider,它知道如何访问运行时开关和数据库/LDAP AuthenticationProvider的实际实例

我在想这样的事情:

public class SwitchingAuthenticationProvider implements AuthenticationProvider
{
    private List<AuthenticationProvider> delegateList;
    private int selectedProvider;

    @Override
    public Authentication authenticate(Authentication authentication)
        throws AuthenticationException
    {
        AuthenticationProvider delegateTo = delegateList.get(selectedProvider);
        return delegateTo.authenticate(authentication);
    }

    ....
}
公共类切换AuthenticationProvider实现AuthenticationProvider
{
私人名单代表名单;
私有int-selectedProvider;
@凌驾
公共身份验证(身份验证)
抛出AuthenticationException
{
AuthenticationProvider delegateTo=delegateList.get(selectedProvider);
返回delegateTo.authenticate(身份验证);
}
....
}

我将介绍如何将您自己的自定义身份验证提供程序注入到其他无数示例中。看起来它与用xml标记特定bean有关。但希望我能为你补充一些其他细节

因此,您已经定义了类似于上面的类,我将添加Spring所需的更多细节(即,合并上面的内容)

public class SwitchingAuthenticationProvider implements AuthenticationProvider
{
    ....
    public List<AuthenticationProvider> getProviders() { return delegateList; }
    public void setProviders(List<AuthenticationProvider> providers) {
        this.delegateList = providers;
    }
    ....
}
公共类切换AuthenticationProvider实现AuthenticationProvider
{
....
公共列表getProviders(){return delegateList;}
公共无效设置提供程序(列表提供程序){
this.delegateList=提供者;
}
....
}
这将允许您使用spring注入大量提供程序:

<bean id="customAuthProvider1" class=".....CustomProvider1"> ... </bean>
<bean id="customAuthProvider2" class=".....CustomProvider2"> ... </bean>
...
<bean id="customAuthProviderX" class=".....CustomProviderX"> ... </bean>

<bean id="authenticationProvider" class="....SwitchingAuthenticationProvider">
    <security:custom-authentication-provider/>
    <!-- using property injection (get/setProviders) in the bean class -->
    <property name="providers">
        <list>
            <ref local="customAuthProvider1"/> <!-- Ref of 1st authenticator -->
            <ref local="customAuthProvider2"/> <!-- Ref of 2nd authenticator -->
            ...
            <ref local="customAuthProviderX"/> <!-- and so on for more -->
        </list>
    </property>
</bean>
。。。
... 
...
... 
...
最后,您如何填充提供程序可以是让委托人获得提供程序集合的任何方法。它们如何映射到要使用的提供程序集合取决于您。集合可以是命名映射,基于委托人的当前状态。它可以是一个包含多个要尝试的提供程序的列表。它可以是两个属性,“get/setPrimary”和“get/setPrimary”“get/setSecondary”用于类似故障转移的功能。一旦您注入了授权者,可能性就取决于您了


如果这不能回答您的问题,请告诉我。

@Matt这很好。但是,我如何填充身份验证提供程序列表?@Matt我知道这是一个愚蠢的问题,但我只是另一个Spring新手。Jay,我稍后会写更多内容,提供更多细节,但您可以将它们作为另一个spri注入到您的SwitchingAuthenticationProvider中ng bean。在运行时动态交换提供程序怎么样?假设我们有一组从数据库加载的OAuth2提供程序,我们想添加一个新的提供程序或删除一个现有的提供程序。@Matt谢谢。这很有帮助。我会尝试一下,让您知道。