2013-10-01 37 views
1

我試圖爲mvc項目設置彈簧安全,並且我很難。我正在使用spring security 3.1.4.Release。我有一個spring-security.xml文件以及爲配置設置的mvc-dispatcher-servlet文件。現在,我得到一個未發現我的用戶細節Bean異常的bean。沒有發現彈簧安全bean異常

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

這裏是我的彈簧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-調度-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是非常相同的,但我確實有它在我的主/ Java目錄中的不同文件夾中。重要的是要注意我使用註釋來聲明類:

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

任何想法?我很難找到與我使用的是同一個春天版本的教程,所以很可能我搞砸了xsd refs。謝謝。

+1

使您的'component-scan'軟件包更具限制性。他們目前正在相互覆蓋。 –

+0

我不知道你的意思是讓它們更具限制性。如果他們覆蓋對方,我會把它放在其中一個上下文文件中? – gleasonomicon

+0

您的每個上下文都在掃描'component-scan'中的同一個軟件包。所以每個人都會創建bean。每個上下文應只掃描與其目的相關的軟件包。 –

回答

1

你加入一個參考:

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

你需要一個bean與ID ..類似:

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

+0

我並不是說你錯了,但是是什麼讓你覺得沒有這樣一個由於'component-scan'創建的bean。 –

+0

服務文件用註釋聲明。我可以在問題中加入這個。 @Service(「myUserDetailService」) public class UserDetailsS​​erviceImpl實現UserDetailsS​​ervice { – gleasonomicon

+0

由於我不得不重新評估軟件包路徑,這是錯誤的,您的建議讓我走上了正確的道路。愚蠢的錯誤,但我感謝您的幫助。 :) – gleasonomicon