【问题标题】:Spring security authentication-manager has no providersSpring security authentication-manager 没有提供者
【发布时间】:2018-08-23 12:39:25
【问题描述】:

我正在尝试使用 Spring 安全性设置一个简单的项目以启用用户名/密码登录。

在 UsernamePasswordAuthenticationFilter 中指向某个断点后,我注意到 getAuthenticationManager 有 0 个提供者

this.getAuthenticationManager()

但是我确实在 security-context.xml 中添加了这个

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

看起来 authenticationManager 确实得到了正确的自动装配,但由于某种原因,身份验证提供程序没有被注入。

我是否忘记在某处启用某些功能?

【问题讨论】:

    标签: spring authentication spring-security provider


    【解决方案1】:

    默认情况下,SpringSecurity 使用org.springframework.security.authentication.ProviderManager,除非设置了 parent,否则它最初应该至少有一个配置的提供者。否则你会在初始化阶段得到IllegalArgumentException。因此,您绝对应该能够在从过滤器返回的身份验证管理器或其父级之一中找到身份验证提供程序(当然,只要不存在故意删除提供程序的有害代码)。

    【讨论】:

    • 我看错了提供商。基本提供者为空,但“父”中有一个
    【解决方案2】:

    从 Spring security 3 升级到版本 4 时,我们遇到了这个问题。 我们有一个 AuthenticationManager 定义如下

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

    事实证明,仅使用 alias 属性定义的 AuthenticationManager 未被 Spring 使用。 我们需要定义一个id 属性以使其工作。

    <authentication-manager id="authenticationManager">
        ...
    </authentication-manager>
    

    当没有指定 id 时,Spring 在 org.springframework.security.config.authentication.AuthenticationManagerBeanDefinitionParser.parse 中的 bean 解析期间将 id 设置为“org.springframework.security.authenticationManager”,覆盖全局注册的AuthenticationManager。这似乎弄乱了指定的providers

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-04-24
      • 1970-01-01
      • 2016-11-09
      • 2015-08-26
      • 2014-06-08
      • 2017-08-13
      • 2019-05-15
      相关资源
      最近更新 更多