如何使用 Spring Security 命名空间设置和配置 ProviderManager?

Jér*_*nge 7 java configuration spring-security

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

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

<authentication-manager>
    <authentication-provider>
       <password-encoder hash="md5"/>
       <jdbc-user-service data-source-ref="dataSource"/>
    </authentication-provider>
</authentication-manager>
Run Code Online (Sandbox Code Playgroud)

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

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

我还想指定哪些内容AuthenticationProvider应该在ProviderManager. 我找到了以下一段配置代码:

<bean id="authenticationManager"
    class="org.springframework.security.authentication.ProviderManager">
    <property name="providers">
        <list>
            <ref local="daoAuthenticationProvider"/>
            <ref local="anonymousAuthenticationProvider"/>
        </list> 
    </property>
</bean>
Run Code Online (Sandbox Code Playgroud)

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

Jér*_*nge 4

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

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

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

根据 B.3.1 节:

<global-method-security authentication-manager-ref="..." >
Run Code Online (Sandbox Code Playgroud)

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

从一篇博客文章中,不应使用<authentication-manager> ... </authentication-manager>,而是应该使用类似于以下内容的内容:

<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>
Run Code Online (Sandbox Code Playgroud)