【问题标题】:Letting Spring security pick implementation of class implementing custom authentication provider让 Spring security 选择实现自定义身份验证提供程序的类的实现
【发布时间】:2017-02-14 16:42:21
【问题描述】:

我们有一个通过 AuthenticationProvider 实现自定义身份验证的 webapp。 这现在工作正常。但是我们希望为客户提供一个选项来实现他们自己的实现 AuthenticationProvider 的身份验证类。所以他们将从应用程序中删除我们的 jar 并将他们的 jar 添加到类路径中。

它出现在安全 xml 中,我们只需要指定实现 AuthenticationProvider 的类,但不能告诉 spring 选择任何实现接口 AuthenticationProvider 的类

当前的 XML 和类实现

<authentication-manager alias="authenticationManager">
    <authentication-provider ref="customAuthenticationProvider"/>
</authentication-manager>

<beans:bean id="customAuthenticationProvider" class="w.x.y.z.CustomAuthenticationProvider"></beans:bean



@Component
public class CustomAuthenticationProvider implements AuthenticationProvider {

    @Override
    public Authentication authenticate(Authentication authentication) throws AuthenticationException {
    //Implementation
    }

    @Override
    public boolean supports(Class<?> arg0) {
        return true;
    }
}

我可以告诉 spring 选择任何实现 AuthenticationProvider 的类吗?

【问题讨论】:

    标签: spring spring-security spring-java-config custom-authentication


    【解决方案1】:

    也许你可以通过使用类型自动装配和工厂方法来做到这一点:

    1-CustomAuthenticationProvider 将通过仅在您的客户端添加的 jar 和已删除的 jar 中定义的类型自动装配注入(它必须恰好是 AuthenticationProvider 的一个实例)。

    2-然后使用工厂方法将此提供程序注入到身份验证管理器中。

    第一步

    public class AuthenticationProviderFactory {
    
        @Autowired
        private AuthenticationProvider authProvider;
    
        public AuthenticationProvider getAuthenticationProvider() {
            return authProvider;
        }
    
    }
    

    两秒步

    <bean name="authenticationProviderFactory"
      class="w.x.y.z..AuthenticationProviderFactory"></bean>
    
    <bean name="authenticationProvider" factory-bean="authenticationProviderFactory"
    factory-method="getAuthenticationProvider">
    </bean>
    <authentication-manager alias="authenticationManager">
       <authentication-provider ref="authenticationProvider"/>
    </authentication-manager>
    

    !!!!删除的 jar 和新的 jar 必须具有相同的 applicationContext.xml 名称(其中声明了 AuthenticationProvider)才能使替换工作。

    <import resource="applicationContextAuthProvider.xml"/>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-12-04
      • 2016-07-24
      • 2011-02-09
      • 1970-01-01
      • 1970-01-01
      • 2016-07-19
      • 2016-09-24
      • 2011-01-03
      相关资源
      最近更新 更多