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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11581058/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 05:40:10  来源:igfitidea点击:

How to set-up and configure a ProviderManager using Spring Security namespace?

javaconfigurationspring-security

提问by Jér?me Verstrynge

Spring documentation says that ProviderManageris the default implementation of the AuthenticationManager, but is an instance of ProviderManagerautomatically created and wired by the security namespace?

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

In other words, will such configuration automatically create an instance of ProviderManager:

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

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

Else, what do I need to do (or declare)?

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

Assuming I would want to plug my own implementation of AuthenticationManager, how would I configure this using the namespace?

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

I also want to specify which AuthenticationProvidershould be registered in the ProviderManager. I have found the following piece of configuration code:

我还想指定哪些AuthenticationProvider应该在ProviderManager. 我发现了以下配置代码:

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

But is it enough? What is the right way to declare the list of AuthenticationProvider? Documentation is not very clear and complete regarding this issue.

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

回答by Jér?me Verstrynge

In other words, will such configuration automatically create an instance of ProviderManager:

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

According to section B2 of the appendix, the answer is yes.

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

Assuming I would want to plug my own implementation of AuthenticationManager, how would I configure this using the namespace?

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

According to section B.3.1:

根据第 B.3.1 节:

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

What is the right way to declare the list of AuthenticationProvider?

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

From a blog post, instead of using <authentication-manager> ... </authentication-manager>, one should use something like similar to this:

从一篇博客文章,而不是使用<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>

回答by Mark Bakker

I use the following config with a custom auth providerl;

我将以下配置与自定义身份验证 providerl 一起使用;

    <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>