2013-06-04 111 views
0

我是新的春天。我試圖使用Spring Security使用MySQL認證有,我得到這個錯誤:異常開始過濾器springSecurityFilterChain - org.springframework.beans.factory.NoSuchBeanDefinitionException

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

通過從我認爲沒有加載安全配置文件類似的問題判斷。

我的配置文件在src /主/資源/彈簧security.xml文件,我包括在awt.project.init.WebAppConfig:

@Configuration 
@EnableWebMvc 
@EnableTransactionManagement 
@ComponentScan("awt.project") 
@ImportResource("classpath:spring-security.xml") 
public class WebAppConfig { 
.... 
} 

下面是web.xml中:

<?xml version="1.0" encoding="UTF-8"?> 
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 
version="3.0"> 

<!-- Serves static resource content from .jar files such as spring-faces.jar --> 
<servlet> 
    <servlet-name>Resource Servlet</servlet-name> 
    <servlet-class>org.springframework.js.resource.ResourceServlet</servlet-class> 
    <load-on-startup>0</load-on-startup> 
</servlet> 

<!-- Map all /resources requests to the Resource Servlet for handling --> 
<servlet-mapping> 
    <servlet-name>Resource Servlet</servlet-name> 
    <url-pattern>/resources/*</url-pattern> 
</servlet-mapping> 

<!-- Java-based Spring container definition --> 
<context-param> 
    <param-name>contextClass</param-name> 
    <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value> 
</context-param> 

<!-- Location of Java @Configuration classes that configure the components that makeup this application --> 
<context-param> 
    <param-name>contextConfigLocation</param-name> 
    <param-value> 
    awt.project.init 
    </param-value> 
</context-param> 

<!-- Creates the Spring Container shared by all Servlets and Filters --> 
<listener> 
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
</listener> 

<!-- Processes application requests --> 
<servlet> 
    <servlet-name>appServlet</servlet-name> 
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
    <init-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value></param-value> 
    </init-param> 
    <load-on-startup>1</load-on-startup> 
</servlet> 

<servlet-mapping> 
    <servlet-name>appServlet</servlet-name> 
    <url-pattern>/</url-pattern> 
</servlet-mapping> 


<!-- Secures the application --> 
<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> 

我想也包括在contexConfiguration彈簧security.xml文件作爲classpath:spring-security.xml但我仍然得到了同樣的問題。

這裏是彈簧的security.xml

<?xml version="1.0" encoding="UTF-8"?> 
<beans:beans xmlns="http://www.springframework.org/schema/security" 
xmlns:beans="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
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 auto-config="true"> 

    <intercept-url pattern="/sec/moderation.html" access="ROLE_MODERATOR" /> 
    <intercept-url pattern="/admin/*" access="ROLE_ADMIN" /> 

    <form-login login-page="/user-login.html" 
     default-target-url="/success-login.html" authentication-failure-url="/error-login.html" /> 
    <logout logout-success-url="/index.html" /> 

</http> 

<authentication-manager> 
    <authentication-provider user-service-ref="customUserDetailsService"> 
     <password-encoder hash="plaintext" /> 
    </authentication-provider> 
</authentication-manager> 

+0

你的spring-security.xml文件是什麼樣子的? –

+0

我加了spring-security.xml –

+0

好像我有一些依賴不匹配,解決後他們的問題就解決了。 –

回答

0

我可能是錯的,但如果你使用AnnotationConfigWebApplicationContextcontextConfigLocation僅指定一個包名,Spring將只掃描包班與原型註釋(@Component,@Controller,@Service等),所以你的WebAppConfig基本上被忽略。嘗試將WebAppConfig的完全限定名稱作爲contextConfigLocation

+0

我嘗試了contextConfigLocation中的awt.project.init.WebAppConfig,但我仍然得到相同的錯誤 –

+2

檢查spring-security.xml文件是否被加載的一種方法是故意使其無效XML,從而導致錯誤說明XML是無效的。或者,您可以啓用調試日誌記錄以查看它是否正在加載。 –

+0

是的,我試過了,它沒有被加載......我如何正確解決它?它在src/main/resources/spring-security.xml中,是classpath:spring-security.xml是否正確? –

相關問題