没有名为 authenticationManager 的 bean [重复]

Posted

技术标签:

【中文标题】没有名为 authenticationManager 的 bean [重复]【英文标题】:No bean named authenticationManager [duplicate] 【发布时间】:2012-12-13 20:26:19 【问题描述】:

可能重复:Getting error org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named ‘springSecurityFilterChain’ is defined

在我的 Spring 应用程序中,我不断收到此错误:

No bean named 'org.springframework.security.authenticationManager' is defined: Did you forget to add a gobal <authentication-manager> element to your configuration (with child <authentication-provider> elements)? Alternatively you can use the authentication-manager-ref attribute on your <http> and <global-method-security> elements.

在我的 Spring Security 上下文 xml 文件中,我定义了以下内容:

<beans:bean id="myUserDetailsService" class="com.myProject.core.security.MyUserDetailsService" />

<beans:bean id="encoder" class="com.myProject.core.security.HmacPasswordEncoder" />

<authentication-manager id="clientAuthenticationManager" >
    <authentication-provider user-service-ref="myUserDetailsService">
        <password-encoder ref="encoder" />
    </authentication-provider>
</authentication-manager>

当我清楚地定义了我的身份验证管理器和身份验证提供者后,任何想法为什么它会抱怨?

注意:这可能会有所帮助,这是一个更具描述性的错误:

org.springframework.beans.factory.BeanCreationException: Error creating bean with 
name 'org.springframework.security.filterChains': Cannot resolve reference to bean
'org.springframework.security.web.DefaultSecurityFilterChain#2' while setting bean
property 'sourceList' with key [2]; nested exception is org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'org.springframework.security.web.DefaultSecurityFilterChain#2':
Cannot resolve reference to bean 'org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter#0'
while setting constructor argument with key [1]; nested exception is
org.springframework.beans.factory.BeanCreationException: Error creating bean with
name 'org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter#0':
Cannot resolve reference to bean 'org.springframework.security.authentication.ProviderManager#0'
while setting bean property 'authenticationManager'; nested exception is
org.springframework.beans.factory.BeanCreationException: Error creating bean with
name 'org.springframework.security.authentication.ProviderManager#0': Cannot resolve
reference to bean 'org.springframework.security.config.authentication.AuthenticationManagerFactoryBean#0'
while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'org.springframework.security.config.authentication.AuthenticationManagerFactoryBean#0':
FactoryBean threw exception on object creation; nested exception is
org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named
'org.springframework.security.authenticationManager' is defined: Did you forget to
add a gobal <authentication-manager> element to your configuration (with child
<authentication-provider> elements)? Alternatively you can use the authentication-manager-ref
attribute on your <http> and <global-method-security> elements.

【问题讨论】:

【参考方案1】:

authenticationManager 是按名称查找的,所以只需将其更改为以下内容:

<authentication-manager alias="authenticationManager">
    <authentication-provider user-service-ref="myUserDetailsService">
        <password-encoder ref="encoder" />
    </authentication-provider>
</authentication-manager>

【讨论】:

甚至 经理> 只需从第二个建议中删除 id="clientAuthenticationManager" 看看是否可行 - 请参阅更新后的编辑。【参考方案2】:

您需要更改 Spring Security 上下文文件以查找 clientAuthenticationManager。您可以将此行添加到您的 http 设置中

<http use-expressions="true" authentication-manager-ref="clientAuthenticationManger">

【讨论】:

是的,它正在尝试以“authenticationManager”的名称获取身份验证管理器引用,因此请填写您的身份验证管理器引用。【参考方案3】:

看看这个链接:

Getting error org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'springSecurityFilterChain' is defined

请注意,过滤器实际上是 DelegatingFilterProxy,而不是 将实际实现过滤器逻辑的类。什么 DelegatingFilterProxy 所做的是将过滤器的方法委托给 从 Spring 应用程序上下文中获取的 bean。

...

您需要定义一个名为 springSecurityFilterChain 的 bean 在您的应用程序上下文中实现 javax.servlet.Filter。

【讨论】:

以上是关于没有名为 authenticationManager 的 bean [重复]的主要内容,如果未能解决你的问题,请参考以下文章

Spring-boot OAuth2 实现:NoSuchBeanDefinitionException:没有 AuthenticationManager 类型的合格 bean

在应用程序上下文中将 Spring Boot 2.1.9 碰撞到 2.2.0 没有 AuthenticationManager

SpringSecurity:全局AuthenticationManager与局部AuthenticationManager

来自不同 url 时的不同登录

authenticationManager无法注入问题

AuthenticationManager, ProviderManager 和 AuthenticationProvider