Spring项目找不到springSecurityFilterChain

时间:2014-11-05 13:54:12

标签: java spring security

尝试在springowego Spring Security中实现你的项目,但我遇到了问题。当我尝试运行项目时,我崩溃了错误:

  
    

lis 05,2014 2:54:51 PM org.apache.catalina.core.StandardContext filterStart SEVERE:异常启动过滤器     springSecurityFilterChain     org.springframework.beans.factory.NoSuchBeanDefinitionException:没有     bean命名为springSecurityFilterChain'定义于     org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:529)       在     org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1095)       在     org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:277)       在     org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)       在     org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1097)       在     org.springframework.web.filter.DelegatingFilterProxy.initDelegate(DelegatingFilterProxy.java:326)       在     org.springframework.web.filter.DelegatingFilterProxy.initFilterBean(DelegatingFilterProxy.java:236)       在     org.springframework.web.filter.GenericFilterBean.init(GenericFilterBean.java:194)       在     org.apache.catalina.core.ApplicationFilterConfig.initFilter(ApplicationFilterConfig.java:281)       在     org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:262)       在     org.apache.catalina.core.ApplicationFilterConfig。(ApplicationFilterConfig.java:107)       在     org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:4775)       在     org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5452)       在     org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)       在     org.apache.catalina.core.ContainerBase $ StartChild.call(ContainerBase.java:1559)       在     org.apache.catalina.core.ContainerBase $ StartChild.call(ContainerBase.java:1549)       在java.util.concurrent.FutureTask.run(未知来源)at     java.util.concurrent.ThreadPoolExecutor.runWorker(未知来源)at     java.util.concurrent.ThreadPoolExecutor $ Worker.run(未知来源)at     java.lang.Thread.run(未知来源)

  
     

lis 05,2014 2:54:51 PM org.apache.catalina.core.StandardContext   startInternal SEVERE:错误filterStart lis 05,2014 2:54:51 PM   org.apache.catalina.core.StandardContext startInternal SEVERE:Context   [/ ibank]启动由于之前的错误而失败

我的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">

    <!-- The definition of the Root Spring Container shared by all Servlets 
        and Filters -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/root-context.xml</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>/WEB-INF/spring/appServlet/servlet-context.xml</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>

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

</web-app>

安全-config.xml中:

    <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.2.xsd">

    <http auto-config="true">
        <access-denied-handler error-page="/403page" />
        <intercept-url pattern="/client**" access="ROLE_CLIENT" />
        <intercept-url pattern="/admin**" access="ROLE_ADMIN" />
        <form-login login-page='/login' username-parameter="username"
            password-parameter="password" default-target-url="/user"
            authentication-failure-url="/login?authfailed" />
        <logout logout-success-url="/login?logout" />
    </http>

    <!-- <authentication-manager> <authentication-provider> <user-service> <user 
        name="user" password="user@123" authorities="ROLE_ADMIN" /> </user-service> 
        </authentication-provider> </authentication-manager> -->

    <authentication-manager>
        <authentication-provider>
            <jdbc-user-service data-source-ref="dataSource"
                users-by-username-query="select username,password, enabled from users where username=?"
                authorities-by-username-query="select username, role from user_roles where username =?  " />
        </authentication-provider>
    </authentication-manager>

</beans:beans>

我找到了这个主题,但我现在不知道他是如何解决Spring : Exception starting filter springSecurityFilterChain

我试试

<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
                 /WEB-INF/spring/root-context.xml
                 /WEB-INF/spring/security-config.xml
    </param-value>
</context-param>

但是应用程序仍无法正常工作,我有这样的错误:

  

错误:org.springframework.web.context.ContextLoader - 上下文初始化失败   org.springframework.beans.factory.parsing.BeanDefinitionParsingException:配置问题:无法找到XML架构命名空间的Spring NamespaceHandler [http://www.springframework.org/schema/security]   违规资源:ServletContext资源[/WB-INF/spring/security-config.xml]

     

lis 05,2014 3:28:37 PM org.apache.catalina.core.StandardContext listenerStart   SEVERE:将上下文初始化事件发送到类org.springframework.web.context.ContextLoaderListener的侦听器实例的异常   org.springframework.beans.factory.parsing.BeanDefinitionParsingException:配置问题:无法找到XML架构命名空间的Spring NamespaceHandler [http://www.springframework.org/schema/security]   违规资源:ServletContext资源[/WB-INF/spring/security-config.xml]

     
    

lis 05,2014 3:28:37 PM org.apache.catalina.core.StandardContext startInternal SEVERE:Error listenerStart lis 05,2014 3:28:37 PM     org.apache.catalina.core.StandardContext startInternal SEVERE:Context     [/ ibank]启动因先前的错误而失败lis 05,2014 3:28:37 PM     org.apache.catalina.core.ApplicationContext log INFO:Closing Spring     root WebApplicationContext lis 05,2014 3:28:37 PM     org.apache.catalina.core.StandardContext listenerStop SEVERE:     将上下文销毁事件发送到侦听器实例的异常     class org.springframework.web.context.ContextLoaderListener     java.lang.IllegalStateException:BeanFactory未初始化或     已关闭 - 请致电&#39;刷新&#39;在通过之前访问bean之前     ApplicationContext的

  
     

警告:org.springframework.web.context.support.XmlWebApplicationContext - 从ApplicationListener处理ContextClosedEvent抛出的异常   java.lang.IllegalStateException:ApplicationEventMulticaster未初始化 - 调用&#39;刷新&#39;在通过上下文组播事件之前:Root WebApplicationContext:启动日期[Wed Nov 05 15:28:35 CET 2014];上下文层次结构的根

     

警告:org.springframework.web.context.support.XmlWebApplicationContext - 在上下文关闭时从LifecycleProcessor抛出的异常   java.lang.IllegalStateException:LifecycleProcessor未初始化 - 调用&#39;刷新&#39;在通过上下文调用生命周期方法之前:Root WebApplicationContext:启动日期[Wed Nov 05 15:28:35 CET 2014];上下文层次结构的根

2 个答案:

答案 0 :(得分:1)

您的web.xml说

   <!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/root-context.xml</param-value>
    </context-param>

除非你的root-context.xml包含security-config.xml,否则你必须在那里列出

   <!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
                     /WEB-INF/spring/root-context.xml
                     /WEB-INF/spring/security-config.xml
        </param-value>
    </context-param>

使用您的最新更新我相信您的问题是双重的,一个,不包括安全配置,第二个缺少pom属性尝试添加

 <spring.security.version>3.1.0.RELEASE</spring.security.version>

在您的属性部分中。版本可能对您有误,但问题应该修复

答案 1 :(得分:1)

将security-config.xml添加到应用程序上下文中,即在web.xml中:

 <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/spring/root-context.xml /WEB-INF/spring/security-config.xml</param-value>    
 </context-param>

使用空格字符分隔条目。

或者在root-context.xml中导入它,即

<import resource="security-config.xml" />