【问题标题】:HTTP 404 : conflict between spring security url filter and dispatcher servlet url mappingHTTP 404:spring security url filter和dispatcher servlet url映射之间的冲突
【发布时间】:2018-02-08 00:00:56
【问题描述】:

我有两个问题:

  • 第一个:我得到一个 HTTP 404。我认为 spring 安全过滤器 url 映射和调度程序 servlet 映射之间存在冲突。我这么说是因为当我从web.xml 中删除 spring 安全配置时,请求映射过程可以正常工作。

  • 第二个:我收到“臭名昭著”的 HTTP 500 无法评估表达式“ROLE_ADMIN”

如果我首先得到 404,我怎么可能遇到问题 2?我对此一无所知。昨天问题 1 一切正常,所以我得到了 HTTP 500。今天,我无法通过 HTTP 404。至少我已经确定了我的配置中的罪魁祸首。

请帮我解决问题 1

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 配置中看到的那样,我已尝试按照建议的here 添加hasRole('ROLE_ADMIN') 来解决问题2

【问题讨论】:

  • 尝试将ROLE_ADMIN 替换为ADMIN。据我所知,它应该自动添加前缀ROLE_
  • @DmitrySenkovich 但是 HTTP 404 呢?由于 HTTP 404,我无法到达将 ROLE_ADMIN 更改为 ADMIN 的部分
  • 您确定这是正确的位置:location="/ressources/theme1/"?我的意思是ressources中有两个ss
  • @DmitrySenkovich 是的。不用担心静态资源。一切正常。我打了一些错别字,但基本上,/src/main/webapp/ressources/theme1 是我的静态资源的基本目录
  • @DmitrySenkovich 问题已解决。请参阅下面的答案。感谢您的帮助队友

标签: spring spring-mvc


【解决方案1】:

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

发件人:http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd

收件人:http://www.springframework.org/schema/mvc/spring-mvc.xsd

等等..也许可以做到。

【讨论】:

  • 对于所有xsi 显示版本或仅对于MVC 的版本?
  • 为所有人。我遇到了与版本相关的问题。删除它们并且它起作用了
  • 是的!!!!删除除http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"&lt;web-app version="3.0" 之外的所有版本解决了这个问题。你知道为什么阻止我吗?现在我有另一个问题,比如this one,但这是另一回事。非常感谢杰克。 ...ps:投票支持我的问题 ;-)
  • @Bloomberg58 确定您需要保留 servlet 版本。其余的,看看这个:stackoverflow.com/a/20900801/3959856
猜你喜欢
  • 2012-04-30
  • 2016-01-02
  • 2013-02-08
  • 2013-06-25
  • 2016-02-16
  • 2016-12-31
  • 2020-03-22
相关资源
最近更新 更多