【问题标题】:How to set-up and configure a ProviderManager using Spring Security namespace?如何使用 Spring Security 命名空间设置和配置 ProviderManager?
【发布时间】:2012-07-19 19:56:44
【问题描述】:

Spring 文档说ProviderManager 是AuthenticationManager 的默认实现,但ProviderManager 的实例是由安全命名空间自动创建和连接的吗?

也就是说,这样的配置会不会自动创建ProviderManager的实例:

<authentication-manager>
    <authentication-provider>
       <password-encoder hash="md5"/>
       <jdbc-user-service data-source-ref="dataSource"/>
    </authentication-provider>
</authentication-manager>

否则,我需要做什么(或声明)?

假设我想插入自己的 AuthenticationManager 实现,我将如何使用命名空间进行配置?

我还想指定应该在ProviderManager 中注册哪个AuthenticationProvider。我找到了以下一段配置代码:

<bean id="authenticationManager"
    class="org.springframework.security.authentication.ProviderManager">
    <property name="providers">
        <list>
            <ref local="daoAuthenticationProvider"/>
            <ref local="anonymousAuthenticationProvider"/>
        </list> 
    </property>
</bean>

但这足够了吗?声明AuthenticationProvider列表的正确方法是什么?关于这个问题的文档不是很清楚和完整。

【问题讨论】:

  • 我已经发布了我认为对我的问题的正确答案。如有必要,我会给专家一些时间来提供他们的意见/反馈。

标签: java configuration spring-security


【解决方案1】:

我将以下配置与自定义身份验证提供程序一起使用;

    <authentication-manager>

        <authentication-provider user-service-ref="myUserDetailsService">
            <password-encoder hash="md5">
                <salt-source user-property="username" />
            </password-encoder>
        </authentication-provider>
    </authentication-manager>

    <beans:bean id="myUserDetailsService" autowire="byType" class="my.user.UserDetailsServiceImpl">
        <beans:property name="userManagementService" ref="userManagementService"></beans:property>
    </beans:bean>

【讨论】:

    【解决方案2】:

    换句话说,这样的配置会自动创建一个 ProviderManager 实例:

    根据附录B2部分,答案是肯定的。

    假设我想插入我自己的实现 AuthenticationManager,我将如何使用命名空间进行配置?

    根据 B.3.1 节:

    <global-method-security authentication-manager-ref="..." >
    

    声明AuthenticationProvider列表的正确方法是什么?

    从blog post,而不是使用&lt;authentication-manager&gt; ... &lt;/authentication-manager&gt;,应该使用类似这样的东西:

    <bean id="authenticationManager" class="org.springframework.security.authentication.ProviderManager">
        <property name="providers">
            <list>
                <ref bean="authenticationProvider" />
                <ref bean="anonymousProvider" />
            </list>
        </property>
    </bean>
    
    <bean id="authenticationProvider" class="org.springframework.security.authentication.dao.DaoAuthenticationProvider">
        <property name="passwordEncoder">
            <bean class="org.springframework.security.authentication.encoding.Md5PasswordEncoder" />
        </property>
        <property name="userDetailsService" ref="userService" />
    </bean>
    
    <bean id="anonymousProvider" class="org.springframework.security.authentication.AnonymousAuthenticationProvider">
        <property name="key" value="SomeUniqueKeyForThisApplication" />
    </bean>
    

    【讨论】:

      猜你喜欢
      • 2011-01-24
      • 1970-01-01
      • 1970-01-01
      • 2014-03-03
      • 2022-07-10
      • 2014-10-11
      • 2018-01-31
      • 1970-01-01
      相关资源
      最近更新 更多