【问题标题】:Use of custom authenticationManager and daoAuthenticationProvider beans使用自定义 authenticationManager 和 daoAuthenticationProvider bean
【发布时间】:2014-07-23 21:43:33
【问题描述】:

我正在运行从 github 下载的小型 spring mvc 3 应用程序(Spitter from spring in action 3 book)。在 spring 安全文件中,他们为 authenticationManager 和 daoAuthenticationProvider 编写了这样的 bean

<beans:beans xmlns="http://www.springframework.org/schema/security"
xmlns:beans="http://www.springframework.org/schema/beans" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.0.3.xsd">

<http auto-config="true" use-expressions="true">
    <intercept-url pattern="/home*" access="hasAnyRole('ROLE_USER','ROLE_ADMIN')"/>
    <intercept-url pattern="/spitters/**" access="hasAnyRole('ROLE_USER','ROLE_ADMIN')" />
    <intercept-url pattern="/admin/**" access="hasRole('ROLE_ADMIN')" />
    <form-login login-processing-url="/static/j_spring_security_check"
        login-page="/login" authentication-failure-url="/login?login_error=t" />
    <logout logout-success-url="/home"/>
</http>

<beans:bean id="daoAuthenticationProvider"
    class="org.springframework.security.authentication.dao.DaoAuthenticationProvider">
    <beans:property name="userDetailsService" ref="userDetailsService" />
</beans:bean>

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

<authentication-manager>
    <authentication-provider user-service-ref="userDetailsService">
        <password-encoder hash="md5" />
    </authentication-provider>
</authentication-manager>

或此链接https://github.com/karolgornicki/spitter/blob/master/src/main/webapp/WEB-INF/spring-security.xml

authenticationManager 和 daoAuthenticationProvider 这两个bean有什么用。在评论后这个应用程序也可以完美运行。

【问题讨论】:

    标签: spring spring-mvc spring-security


    【解决方案1】:

    我认为AuthenticationManager 将持久用户信息的获取委托给一个或多个AuthenticationProviders。身份验证提供者(例如DaoAuthenticationProviderJaasAuthenticationProviderLdapAuthenticationProviderOpenIDAuthenticationProvider)专门访问特定的用户信息存储库。参考手册的this part 中提到了其他内容。它说:

    您可能希望使用ProviderManager 注册额外的AuthenticationProvider bean,您可以使用具有 ref 属性的元素来执行此操作,其中属性的值是您要添加的提供程序 bean 的名称。

    换句话说,您可以指定多个AuthenticationProviders,例如一个在LDAP 数据库中查找用户,另一个在SQL 数据库中查找。

    【讨论】:

    • 您是正确的,但在上面的 xml 文件中,为什么他们添加了 authenticationManager 和 daoAuthenticationProvider bean。在不添加此 bean 的情况下,此应用程序也可以完美运行,因为我们在 标记中添加了 user-service-ref="userDetailsS​​ervice"。我认为没有必要再添加这两个豆子了。
    • @NileshKhaire 是的,没错,但提供者只是 Spring 的插件,用于连接和验证 LDAP 和其他数据库。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-05-02
    • 2013-10-11
    • 1970-01-01
    • 2018-08-11
    • 1970-01-01
    • 2016-12-07
    • 2016-03-17
    相关资源
    最近更新 更多