2013-04-25 76 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

相關問題