HTTP 404:Spring安全性URL过滤器和调度程序Servlet URL映射之间的冲突


问题内容

我在这里有2个问题:

  • 1st:我得到一个HTTP404。我认为spring安全过滤器URL映射和分派器servlet映射之间存在冲突。我说这是因为,当我从中删除spring安全配置时web.xml,请求映射过程可以正常工作。

  • 2nd:我得到“臭名昭著”的HTTP 500无法评估表达式“ ROLE_ADMIN”

现在,如果我首先得到404,我怎么可能会遇到问题2?我对此一无所知。昨天,问题1的一切工作都很好,所以我得到了HTTP500。今天,我无法通过HTTP404。至少我在配置中确定了罪魁祸首。

请帮助我解决问题1 <-这是该问题的主要内容(如果您也对问题2感兴趣)…以下是我的xml配置文件。

Web.xml

<web-app version="3.0" 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_3_0.xsd">

    <!-- For front controller i.e. dispatcher servlet -->
    <servlet>
        <servlet-name>DispatcherServlet</servlet-name>
        <servlet-class> org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>
                /WEB-INF/spring/webcontext/DispatcherServlet-context.xml
            </param-value>
        </init-param>
    </servlet>

    <servlet-mapping>
        <servlet-name>DispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <!-- For Spring security --> 
    <!-- When I remove all items below, my app works fine -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/spring/webcontext/security-context.xml
        </param-value>
    </context-param>

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

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

DispatcherServlet-context.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"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-4.0.xsd
    http://www.springframework.org/schema/mvc 
    http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">

    <!-- To enable spring annotations @RequestMapping, @Controller, @Repository, etc... -->
    <mvc:annotation-driven />

    <!-- To enable @MatrixVariable -->
    <mvc:annotation-driven enable-matrix-variables="true"/>

    <!-- To set the package where dispatcher servlet looks for controllers
    Some other scanning for other purpose may also occur in that package
    For instance for @Autowired to look for interfaces-->

    <context:component-scan base-package="com.packt.webstore" />

    <!-- Sets where ViewResolver looks for views -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/" />
        <property name="suffix" value=".jsp" />
    </bean>

    <!-- For static resources. Example : CSS, JS, etc... -->    
    <mvc:resources mapping="/resources/**" location="/ressources/theme1/" />

    <!-- For externalizing messages -->
    <bean id= "messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
        <property name="basename" value="messages"/>
    </bean>
</beans>

security-context.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"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:security="http://www.springframework.org/schema/security"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
    http://www.springframework.org/schema/security
    http://www.springframework.org/schema/security/spring-security-4.1.xsd
    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-4.2.xsd">

    <security:http auto-config="true">
        <security:intercept-url pattern="/products/add" access="hasRole('ROLE_ADMIN')" />
        <security:form-login login-page="/login"
                             default-target-url="/products/add"
                             authentication-failure-url="/loginfailed"/>
        <security:logout logout-success-url="/logout" />
    </security:http>

    <security:authentication-manager>
        <security:authentication-provider>
            <security:user-service>
                <security:user name="Admin" 
                               password="Admin123" 
                               authorities="ROLE_ADMIN" />
            </security:user-service>
        </security:authentication-provider>
    </security:authentication-manager>
</beans>

正如您在security-cntext.xml配置中看到的那样,我尝试通过hasRole('ROLE_ADMIN')按此处的建议添加来解决问题2


问题答案:

也许版本?检查xsi:schemaLocation两个文件中的属性。他们不同。您可以删除版本。

从: http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd

至: http://www.springframework.org/schema/mvc/spring-mvc.xsd

等等。也许那样做。