通用匹配模式('/ **')在过滤器链中的其他模式之前定义,导致它们被忽略

时间:2013-12-28 08:54:12

标签: spring-security tomcat7 java-7

Spring Security基本示例给出了此异常。

错误:“通用匹配模式('/ **')在过滤器链中的其他模式之前定义,导致它们被忽略。请检查命名空间或FilterChainProxy bean配置中的顺序”

我在同一个问题上查了很多帖子。但找不到任何解决方案。任何人都可以部署一个hello spring security吗?

我正在使用这些版本 - Spring 3.2.0.RELEASE / Spring security 3.2.0.RELEASE / Tomcat 7 / Java 1.7。

这是我的代码:

的web.xml

<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
    <display-name>Spring security sample Web Application</display-name>

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

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

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

    <servlet>
        <servlet-name>spring-dispatcher-servlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring-context.xml</param-value>
        </init-param>
    </servlet>

    <servlet-mapping>
        <servlet-name>spring-dispatcher-servlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>

弹簧context.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.2.xsd 
    http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.2.xsd">

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

    <http auto-config="true">
        <intercept-url pattern="/pages/hello.jsp" access="ROLE_USER" />
    </http>

</beans:beans>

MyController.java

package com.jai.spring.security.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class MyConroller {

    @RequestMapping(value = "/security", method = RequestMethod.GET)
        public String security(Model model) {
            System.out.println("calling ... /security");
            model.addAttribute("name", "Jay");
            return "pages/hello";
        }


    @RequestMapping(value = "/profile", method = RequestMethod.GET)
        public String profile(Model model) {
            System.out.println("calling ... /profile");
            return "user/profile";
        }
}

的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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.jai.spring.security</groupId>
    <artifactId>springsec</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>springsec Maven Webapp</name>
    <url>http://maven.apache.org</url>

    <properties>
        <spring-version>3.2.0.RELEASE</spring-version>
        <spring-security-version>3.2.0.RELEASE</spring-security-version>
    </properties>

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

        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.14</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>


    </dependencies>
    <build>
        <finalName>springsec</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <version>2.2</version>

                <configuration>
                    <url>http://localhost:8080/manager/text</url>
                    <server>localhost</server>
                    <path>/${project.build.finalName}</path>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

文件结构

enter image description here

1 个答案:

答案 0 :(得分:13)

您错误地使用了XML上下文文件,这会导致重复的bean定义(包括安全配置)。没有<http>属性的pattern配置映射到/** =&gt;你的错误。


在这里,您可以为servlet定义根XML上下文文件:

<servlet>
    <servlet-name>spring-dispatcher-servlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring-context.xml</param-value>
    </init-param>
</servlet>

改为创建新的上下文配置(如果使用默认名称{your-servlet-name}-servlet.xml =&gt; spring-dispatcher-servlet-servlet.xml),则可以省略此声明:

    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
    </init-param>

root servlet 上下文之间的区别在StackOverflow上已多次讨论过。基本上,根上下文(在您的情况下为spring-context.xmlsecurity-context.xml)应该包含应用程序范围的bean(服务,DAO,包括安全性配置)和调度程序上下文(在我的示例中为dispatcher-servlet.xml)应该保留调度程序servlet特定的bean,如控制器,视图解析器等。

进一步阅读: