异常启动过滤器springSecurityFilterChain - org.springframework.beans.factory.NoSuchBeanDefinitionException

时间:2013-06-04 17:34:09

标签: spring spring-mvc spring-security web.xml

我是春天的新人。我正在尝试使用Spring Security进行身份验证,我收到此错误:

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

通过判断类似的问题,我认为没有加载安全配置文件。

我的配置文件位于src / main / resources / spring-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中的spring-security.xml包含为classpath:spring-security.xml但仍然遇到同样的问题。

这是spring-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>

1 个答案:

答案 0 :(得分:0)

我可能错了,但如果您使用AnnotationConfigWebApplicationContext并仅在contextConfigLocation中指定包名称,则Spring将仅针对具有构造型注释(@Component,{@Controller的类扫描该包。 {1}},@Service等),因此WebAppConfig基本上被忽略了。尝试将WebAppConfig的完全限定名称设为contextConfigLocation