【问题标题】:spring security bean not found exceptionspring security bean 未找到异常
【发布时间】:2013-10-08 03:50:18
【问题描述】:

我正在尝试为 mvc 项目设置 Spring Security,但我遇到了困难。我正在使用 spring security 3.1.4.Release。我设置了一个 spring-security.xml 文件以及用于配置的 mvc-dispatcher-servlet 文件。现在,我的用户详细信息 Bean 出现 bean not found 异常。

在 intellij 中,我收到 myUserDetailService 的“无法解析 bean”消息。我也无法解析包“控制器”。我运行项目时的根本错误是:

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.security.authentication.dao.DaoAuthenticationProvider#0': Cannot resolve reference to bean 'myUserDetailService' while setting bean property 'userDetailsService'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'myUserDetailService' is defined
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:329) [spring-beans-3.2.0.RELEASE.jar:3.2.0.RELEASE]
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:107) [spring-beans-3.2.0.RELEASE.jar:3.2.0.RELEASE]

这是我的 spring-security.xml 文件:

<beans:beans xmlns="http://www.springframework.org/schema/security"
         xmlns:beans="http://www.springframework.org/schema/beans"
         xmlns:context="http://www.springframework.org/schema/context"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
         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.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">


<!-- Enabling Spring beans auto-discovery -->
   <context:component-scan base-package="controller, com.company.admin" />

<http auto-config="true">
    <intercept-url pattern="/admin/*" access="ROLE_USER"  />
    <form-login login-page="/login" default-target-url="/admin/welcome"   authentication-failure-url="/loginfailed" />
    <logout logout-success-url="/logout" />
</http>

<authentication-manager>
    <authentication-provider user-service-ref="myUserDetailService"/>
</authentication-manager>

</beans:beans>

这是我的 mvc-dispatcher-servlet.xml:

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


<!-- Enabling Spring beans auto-discovery -->
<context:component-scan base-package="com.company.admin" />

<!-- Enabling Spring MVC configuration through annotations -->
<mvc:annotation-driven />

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/pages/"/>
    <property name="suffix" value=".jsp"/>
</bean>
</beans>

这是我的 web.xml:

<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

<display-name>Spring MVC Application</display-name>


<filter>
    <filter-name>springSecurityFilterChain</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
    <filter-name>springSecurityFilterChain</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        /WEB-INF/mvc-dispatcher-servlet.xml,
        /WEB-INF/spring-security.xml
    </param-value>
</context-param>

<servlet>
    <servlet-name>mvc-dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>mvc-dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

<listener>
    <listener-class>
        org.springframework.web.context.ContextLoaderListener
    </listener-class>
</listener>
</web-app>

我基本上是在尝试遵循本教程:http://kh-yiu.blogspot.com/2012/12/spring-mvc-security-custom.html 和 UserDetailServiceImpl 与此非常相似,但我确实将它放在我的 main/java 目录的不同文件夹中。需要注意的是,我使用注解来声明类:

@Service("myUserDetailService")
public class UserDetailsServiceImpl implements UserDetailsService{

有什么想法吗?我很难找到与我正在使用的相同 spring 版本的教程,所以很可能我搞砸了 xsd refs。谢谢。

【问题讨论】:

  • 使您的component-scan 包更具限制性。它们目前正在相互覆盖。
  • 我不知道你的意思是让它们更具限制性。如果它们相互覆盖,我会将其放入其中一个上下文文件中吗?
  • 您的每个上下文都在扫描component-scan 中的同一个包。所以每个人都会创造豆子。每个上下文应该只扫描与其目的相关的包。
  • 啊...您的建议使我深入研究了包名称,发现我在 UserDetails 文件的位置上有问题。我粗心疏忽。我只是通过修复名称解决了这个问题。谢谢大家。

标签: java spring-mvc spring-security


【解决方案1】:

您正在添加对以下内容的引用:

<authentication-provider user-service-ref="myUserDetailService"/>

您需要一个具有该 id 的 bean.. 类似:

<bean id="myUserDetailService" class="the.class.of.your.service"></bean>

打破你的例外:

//Error creating the bean
Caused by: org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name 'org.springframework.security.authentication.dao.DaoAuthenticationProvider#0': 
//The myUserDetailService reference cannot be solved, it cannot be found or it has syntax error or something is wrong there
Cannot resolve reference to bean 'myUserDetailService' while setting bean property 'userDetailsService'; 
//The root cause is an exception deeper.
nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: 
//You don't have a bean with this name:
No bean named 'myUserDetailService' is defined

更新:

现在您添加了@Service myUserDetailService... 它可能与why does annotate class as @Service do not create bean? 重复

【讨论】:

  • 我不是说你错了,但是是什么让你认为没有这样的 bean 是因为 component-scan 而创建的。
  • 服务文件用注解声明。我可以将其包括在问题中。 @Service("myUserDetailService") 公共类 UserDetailsS​​erviceImpl 实现 UserDetailsS​​ervice{
  • 您的建议使我走上了正确的道路,因为我不得不重新评估包路径,这是错误的。我犯了一个愚蠢的错误,但感谢您的帮助。 :)
猜你喜欢
  • 1970-01-01
  • 2015-01-20
  • 2021-03-11
  • 1970-01-01
  • 1970-01-01
  • 2018-01-17
  • 2018-04-05
  • 2016-12-03
  • 2013-03-07
相关资源
最近更新 更多