Spring security - org.springframework.beans.factory.NoSuchBeanDefinitionException:没有定义名为'springSecurityFilterChain'的bean

时间:2013-02-10 17:33:09

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

我已阅读过已发布的大部分类似问题,但仍无法找到解决方案。 请注意,常规CRUD应用程序正常工作。当我尝试通过spring-security添加登录功能时,我收到此错误。

我的工作环境如下;

1.Tomcat 7.0.35

2.Spring 3.2.0

3.Spring安全性

弹簧安全-CONFIG-3.1.3.RELEASE.jar

弹簧安全核心3.1.3.RELEASE.jar

弹簧安全标签库-3.1.3.RELEASE.jar

弹簧安全web的3.1.3.RELEASE.jar

4.我的web.xml(/src/main/webapp/WEB-INF/web.xml)

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


<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:spring/root-context.xml</param-value>
</context-param>

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


<!-- 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>
            classpath:spring/conf/*-context.xml
        </param-value>
    </init-param>
    <!-- 
    <load-on-startup>1</load-on-startup>
     -->
</servlet>


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

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

5.我的security-context.xml(/src/main/resources/spring/conf/security-context.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" 
     xmlns:p="http://www.springframework.org/schema/p" 
     xmlns:util="http://www.springframework.org/schema/util"
     xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
    http://www.springframework.org/schema/security 
    http://www.springframework.org/schema/security/spring-security-3.1.xsd
    http://www.springframework.org/schema/util 
    http://www.springframework.org/schema/util/spring-util-3.1.xsd">

<http pattern="/resources" security="none" />

<http auto-config="true"  use-expressions="true">
    <intercept-url pattern="/login" access="permitAll"/>
    <intercept-url pattern="/logout" access="permitAll"/>
    <intercept-url pattern="/denied" access="hasRole('ROLE_USER')"/>
    <intercept-url pattern="/" access="hasRole('ROLE_USER')"/>
    <intercept-url pattern="/user" access="hasRole('ROLE_USER')"/>
    <intercept-url pattern="/admin" access="hasRole('ROLE_ADMIN')"/>

    <form-login login-page="/login" 
        authentication-failure-url="/login/failure" 
        default-target-url="/"/>

    <access-denied-handler error-page="/denied"/>

    <logout invalidate-session="true" 
        logout-success-url="/logout/success" 
        logout-url="/logout"/>
</http>

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

6.错误

INFO: Initializing Spring root WebApplicationContext
INFO : org.springframework.web.context.ContextLoader - Root WebApplicationContext: initialization started
INFO : org.springframework.web.context.support.XmlWebApplicationContext - Refreshing Root WebApplicationContext: startup date [Sun Feb 10 22:17:40 IST 2013]; root of context hierarchy
INFO : org.springframework.beans.factory.xml.XmlBeanDefinitionReader - Loading XML bean definitions from class path resource [spring/root-context.xml]
INFO : org.springframework.beans.factory.support.DefaultListableBeanFactory - Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@58d51a54: defining beans []; root of factory hierarchy
INFO : org.springframework.web.context.ContextLoader - Root WebApplicationContext: initialization completed in 353 ms
10-Feb-2013 22:17:40 org.apache.catalina.core.StandardContext filterStart
SEVERE: Exception starting filter springSecurityFilterChain
org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'springSecurityFilterChain' is defined
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:549)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1096)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:278)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:198)
    at  org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1121)
    at org.springframework.web.filter.DelegatingFilterProxy.initDelegate(DelegatingFilterProxy.java:326)
    at org.springframework.web.filter.DelegatingFilterProxy.initFilterBean(DelegatingFilterProxy.java:236)
    at org.springframework.web.filter.GenericFilterBean.init(GenericFilterBean.java:194)
    at org.apache.catalina.core.ApplicationFilterConfig.initFilter(ApplicationFilterConfig.java:281)
    at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:262)
    at org.apache.catalina.core.ApplicationFilterConfig.<init>(ApplicationFilterConfig.java:107)
    at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:4656)
    at  org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5312)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:901)
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877)
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:633)
    at org.apache.catalina.startup.HostConfig.deployDescriptor(HostConfig.java:657)
    at  org.apache.catalina.startup.HostConfig$DeployDescriptor.run(HostConfig.java:1637)
    at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
    at java.util.concurrent.FutureTask$Sync.innerRun(Unknown Source)
    at java.util.concurrent.FutureTask.run(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)

请给我一个帮助......谢谢

3 个答案:

答案 0 :(得分:6)

您已配置了两个contextConfigLocation元素。

您需要从contextConfigLocation元素中删除appServlet定义,并将classpath:spring/conf/security-context.xml添加到基本contextConfigLocation元素

答案 1 :(得分:0)

我遇到了同样的错误。

我在root-context.xml上移动了安全bean(?)的配置:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:security="http://www.springframework.org/schema/security"
    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.xsd
        http://www.springframework.org/schema/security
        http://www.springframework.org/schema/security/spring-security-3.1.xsd">

    <!-- Root Context: defines shared resources visible to all other web components -->

    <security:authentication-manager>
        <security:authentication-provider>
            <security:user-service>
                <security:user name="jimi" password="jimispassword"
                    authorities="ROLE_USER, ROLE_ADMIN" />
                <security:user name="bob" password="bobspassword"
                    authorities="ROLE_USER" />
            </security:user-service>
        </security:authentication-provider>
    </security:authentication-manager>

    <security:http auto-config='true'>
        <security:intercept-url pattern="/**" access="ROLE_USER" />
    </security:http>

-

现在它停止了

  

org.springframework.beans.factory.NoSuchBeanDefinitionException:没有   名为'springSecurityFilterChain'的bean已定义

启动时的

例外。(尚未测试网页:))

答案 2 :(得分:0)

提供最小的弹簧安全配置

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.0.3.xsd“&gt;

<http auto-config='true'>
    <intercept-url pattern="/**" access="ROLE_USER" />
</http>