Spring中的错误:org.springframework.beans.factory.NoSuchBeanDefinitionException:

时间:2013-04-25 17:50:15

标签: spring spring-security web.xml pom.xml applicationcontext

我在弹簧安全性的集成中遇到了这个错误:

  

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

这是我的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>

这是我的applicationContext.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类:

@Configuration //This is a configuration class:  @Bean methods are intercepted, and it's return //values cached as needed for the bean scope.
@ComponentScan(basePackages = "com.server.rest.resource") //Transaction management 
@ImportResource("/WEB-INF/applicationContext.xml") 
public class ApplicationContext {

}

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

如果你能帮我解决问题,我将不胜感激。提前致谢

1 个答案:

答案 0 :(得分:0)

Spring DelegatingFilterProxy期望在<filter-name>中声明名称的bean。在您的情况下,这是springSecurityFilterChain,但似乎您的应用程序上下文中没有这样的bean。

查看this主题中的最后一篇文章。

您似乎需要明确使用Spring的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