2013-04-25 71 views
0

我面对这个错误在春季安全的集成:错误春:org.springframework.beans.factory.NoSuchBeanDefinitionException:

org.springframework.beans.factory.NoSuchBeanDefinitionException:没有 豆命名为“springSecurityFilterChain “被定义

这是我的pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 
    <modelVersion>4.0.0</modelVersion> 

    <groupId>com.server.rest</groupId> 
    <artifactId>SpringRestWebServices</artifactId> 
    <packaging>war</packaging> 
    <version>0.0.1-SNAPSHOT</version> 

    <name>SpringRestWebServices</name> 
    <url>http://maven.apache.org</url> 

    <!-- Shared version number properties --> 
    <properties> 
     <org.springframework.version>3.1.3.RELEASE</org.springframework.version> 
    </properties> 

    <dependencies> 

     <!-- Jersey dependencies --> 
     <dependency> 
      <groupId>com.sun.jersey</groupId> 
      <artifactId>jersey-server</artifactId> 
      <version>1.2</version> 
     </dependency> 
     <dependency> 
      <groupId>com.sun.jersey</groupId> 
      <artifactId>jersey-json</artifactId> 
      <version>1.2</version> 
     </dependency> 

     <!-- Spring 3 dependencies -->  <!-- Web application development utilities applicable to both Servlet and 
      Portlet Environments (depends on spring-core, spring-beans, spring-context) 
      Define this if you use Spring MVC, or wish to use Struts, JSF, or another 
      web framework with Spring (org.springframework.web.*) --> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-web</artifactId> 
      <version>${org.springframework.version}</version> 
     </dependency>  <!-- Spring 3 security --> 
     <dependency> 
      <groupId>org.springframework.security</groupId> 
      <artifactId>spring-security-web</artifactId> 
      <version>${org.springframework.version}</version> 
     </dependency> 

     <dependency> 
      <groupId>org.springframework.security</groupId> 
      <artifactId>spring-security-config</artifactId> 
      <version>${org.springframework.version}</version> 
     </dependency> 

     <!-- Jersey + Spring dependencies --> 
     <dependency> 
      <groupId>com.sun.jersey.contribs</groupId> 
      <artifactId>jersey-spring</artifactId> 
      <version>1.2</version> 
      <exclusions> 
       <exclusion> 
        <groupId>org.springframework</groupId> 
        <artifactId>spring</artifactId> 
       </exclusion> 
       <exclusion> 
        <groupId>org.springframework</groupId> 
        <artifactId>spring-core</artifactId> 
       </exclusion> 
       <exclusion> 
        <groupId>org.springframework</groupId> 
        <artifactId>spring-web</artifactId> 
       </exclusion> 
       <exclusion> 
        <groupId>org.springframework</groupId> 
        <artifactId>spring-beans</artifactId> 
       </exclusion> 
       <exclusion> 
        <groupId>org.springframework</groupId> 
        <artifactId>spring-context</artifactId> 
       </exclusion> 
      </exclusions> 
     </dependency> 



     <dependency> 
      <groupId>javax.servlet</groupId> 
      <artifactId>servlet-api</artifactId> 
      <version>2.5</version> 
      <scope>provided</scope> 
     </dependency> 
     <dependency> 
      <groupId>javax.servlet.jsp</groupId> 
      <artifactId>jsp-api</artifactId> 
      <version>2.1</version> 
      <scope>provided</scope> 
     </dependency> 
     <dependency> 
      <groupId>cglib</groupId> 
      <artifactId>cglib</artifactId> 
      <version>2.2.2</version> 
      <scope>provided</scope> 
     </dependency> 

    </dependencies> 

    <build> 
     <plugins> 
      <plugin> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-compiler-plugin</artifactId> 
       <version>2.0.2</version> 
       <configuration> 
        <source>1.5</source> 
        <target>1.5</target> 
       </configuration> 
      </plugin> 
     </plugins> 
    </build> 
</project> 

,这是我applicationConte xt.xml:

<?xml version="1.0" encoding="UTF-8"?> 
<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" 
    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"> 

    <context:annotation-config></context:annotation-config> 
    <context:component-scan base-package="com.server.rest.resource" /> 
</beans> 

,这是的ApplicationContext类:

​​

,这是我的弹簧security.xml文件

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

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

    <security:authentication-manager> 
     <security:authentication-provider> 
      <security:user-service> 
       <security:user name="user" password="user" authorities="ROLE_USER" /> 
      </security:user-service> 
     </security:authentication-provider> 
    </security:authentication-manager> 

</beans> 

和我的web.xml

 <!--?xml version="1.0" encoding="UTF-8"?? --> 
<web-app id="WebApp_ID" version="3.0" 
    xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
    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_3_0.xsd"> 
    <display-name>SpringJerseyRestServer</display-name> 
    <welcome-file-list> 
     <welcome-file>index.html</welcome-file> 
    </welcome-file-list> <!-- Configure ContextLoaderListener to use AnnotationConfigWebApplicationContext 
     instead of the default XmlWebApplicationContext --> 
    <context-param> 
     <param-name>contextClass</param-name> 
     <param-value>   org.springframework.web.context.support.AnnotationConfigWebApplicationContext  </param-value> 
    </context-param> 

    <!-- Configuration locations must consist of one or more comma- or space-delimited 
     fully-qualified @Configuration classes. Fully-qualified packages may also 
     be specified for component-scanning --> 
    <context-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>   com.server.rest.resource.ApplicationContext,   /WEB-INF/spring-security.xml  </param-value> 
    </context-param> 


    <!-- Bootstrap the root application context as usual using ContextLoaderListener --> 
    <listener> 
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
    </listener> 

    <!-- Declare a Spring MVC DispatcherServlet as usual --> 
    <servlet> 
     <servlet-name>dispatcher</servlet-name> 
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  <!-- Configure DispatcherServlet to use AnnotationConfigWebApplicationContext 
      instead of the default XmlWebApplicationContext --> 
     <init-param> 
      <param-name>contextClass</param-name> 
      <param-value> 
        org.springframework.web.context.support.AnnotationConfigWebApplicationContext   </param-value> 
     </init-param>  <!-- Again, config locations must consist of one or more comma- or space-delimited 
      and fully-qualified @Configuration classes --> 
     <init-param> 
      <param-name>contextConfigLocation</param-name> 
      <param-value>com.server.rest.resource.ApplicationContext, 
        /WEB-INF/spring-security.xml 

       </param-value> 
     </init-param> 
    </servlet> 


    <servlet> 
     <servlet-name>Jersey Spring Web Application</servlet-name> 
     <servlet-class>com.sun.jersey.spi.spring.container.servlet.SpringServlet</servlet-class> 
     <init-param> 
      <param-name>com.sun.jersey.config.property.packages</param-name> 
      <param-value>com.server.rest.resource</param-value> 
     </init-param> 

     <load-on-startup>1</load-on-startup> 
    </servlet> 
    <servlet-mapping> 
     <servlet-name>Jersey Spring Web Application</servlet-name> 
     <url-pattern>/rest/*</url-pattern> 
    </servlet-mapping> 

    <!-- Spring Security --> 
    <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>/rest/*</url-pattern> 
    </filter-mapping> 


</web-app> 

我将不胜感激如果你能帮我解决问题。在此先感谢

+0

我更新了我的答案,并解释了一下你的web.xml文件的外观。尝试一下,然后阅读Spring配置/ bean上下文。 – 2013-04-26 15:54:41

+0

非常感谢@SotiriosDelimanolis,我非常感谢你的帮助。我通过组合两个xml文件解决了我的问题:spring-security.xml和applicationContext.xml。非常感谢你,我真的很感激。 – Wijden 2013-04-27 12:56:59

回答

0

Spring的DelegatingFilterProxy预计在<filter-name>中声明名称的bean。在你的情况下,这是springSecurityFilterChain,但它似乎并不像你的应用程序上下文中有这样一个bean。

看看this线程中的最后一篇文章。

看起来你需要明确地使用security命名空间来生成一个springSecurityFilterChain。因此,将命名空间添加到所有的spring-security.xml元素中。 (请确保将默认的命名空间更改为beans或别的东西。)

替换

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

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

    <security:authentication-manager> 
     <security:authentication-provider> 
      <security:user-service> 
       <security:user name="user" password="user" authorities="ROLE_USER" /> 
      </security:user-service> 
     </security:authentication-provider> 
    </security:authentication-manager> 

</beans> 

还有其他的事情错了/在你的web.xml没有必要的。它应该是这样的:

<!--?xml version="1.0" encoding="UTF-8"?? --> 
<web-app id="WebApp_ID" version="3.0" 
    xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
    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_3_0.xsd"> 
    <display-name>SpringJerseyRestServer</display-name> 
    <welcome-file-list> 
     <welcome-file>index.html</welcome-file> 
    </welcome-file-list> <!-- Configure ContextLoaderListener to use AnnotationConfigWebApplicationContext 
     instead of the default XmlWebApplicationContext --> 
    <context-param> 
     <param-name>contextClass</param-name> 
     <param-value> 
      org.springframework.web.context.support.AnnotationConfigWebApplicationContext 
     </param-value> 
    </context-param> 

    <!-- Configuration locations must consist of one or more comma- or space-delimited 
     fully-qualified @Configuration classes. Fully-qualified packages may also 
     be specified for component-scanning --> 
    <context-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>   
      com.server.rest.resource.ApplicationContext 
     </param-value> 
    </context-param> 


    <!-- Bootstrap the root application context as usual using ContextLoaderListener --> 
    <listener> 
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
    </listener> 

    <!-- Declare a Spring MVC DispatcherServlet as usual --> 
    <servlet> 
     <servlet-name>dispatcher</servlet-name> 
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  <!-- Configure DispatcherServlet to use AnnotationConfigWebApplicationContext 
      instead of the default XmlWebApplicationContext --> 
     <init-param> 
      <param-name>contextClass</param-name> 
      <param-value> 
        org.springframework.web.context.support.XmlWebApplicationContext 
      </param-value> 
     </init-param>  <!-- Again, config locations must consist of one or more comma- or space-delimited 
      and fully-qualified @Configuration classes --> 
     <init-param> 
      <param-name>contextConfigLocation</param-name> 
      <param-value> 
       /WEB-INF/spring-security.xml 
      </param-value> 
     </init-param> 
    </servlet> 


    <servlet> 
     <servlet-name>Jersey Spring Web Application</servlet-name> 
     <servlet-class>com.sun.jersey.spi.spring.container.servlet.SpringServlet</servlet-class> 
     <init-param> 
      <param-name>com.sun.jersey.config.property.packages</param-name> 
      <param-value>com.server.rest.resource</param-value> 
     </init-param> 

     <load-on-startup>1</load-on-startup> 
    </servlet> 
    <servlet-mapping> 
     <servlet-name>Jersey Spring Web Application</servlet-name> 
     <url-pattern>/rest/*</url-pattern> 
    </servlet-mapping> 

    <!-- Spring Security --> 
    <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>/rest/*</url-pattern> 
    </filter-mapping> 
</web-app> 

应用程序的根上下文由ContextLoaderListener加载。你只能把ApplicationContext上下文(你的@Configuration类)加载它。由DispatcherServlet加载的所有其他上下文都是根上下文的子节点,因此可以在其中引用bean。在那里您的DispatcherServlet只需要加载/WEB-INF/spring-security.xml上下文(可能还有一个与MVC相关的bean的上下文,但在这里看起来并不像这样。)由于这是一个xml文件,因此您需要一个XmlWebApplicationContext

+0

谢谢你的回复,但我不明白我必须添加什么 – Wijden 2013-04-25 19:21:52

+0

我仍然无法解决我的问题,请我需要帮助。 – Wijden 2013-04-25 20:12:37

+0

你知道xml名称空间是如何工作的吗?在你的'spring-security.xml'文件中,所有安全元素的前缀都是'security:',例如''变为''。但是你也需要改变你的名字空间声明。 – 2013-04-25 20:14:36

相关问题