【问题标题】:Spring security issue with 404 error?带有 404 错误的 Spring 安全问题?
【发布时间】:2011-05-08 09:29:02
【问题描述】:

大家好,我使用的是 spring security 3.0.2,urlRewrite 3.1.0 ,我有一个spring security的问题,我有一个规则,应用程序中的所有页面都需要身份验证,除了某些页面,所以我的security.xml是:

<http use-expressions="true" > 
<intercept-url pattern="/" access="permitAll" />
<intercept-url pattern="/error"  filter="none" />  
<intercept-url pattern="/**" access="isAuthenticated()" />
.
.
.</http>

在 web.xml 中我定义了错误页面

<error-page>
   <error-code>404</error-code>
   <location>/p/error</location>
</error-page>

问题是,如果我不是登录用户,并且键入了一些应用程序中不存在的 URL,例如 app/notFoundUrl,spring security 会将此页面与需要身份验证的模式 /** 匹配,所以用户没有按预期重定向到错误页面,而是重定向到登录页面,然后重定向到错误页面

我希望如果用户在登录与否时输入了错误的 url,他会直接重定向到错误页面。

我认为问题与web.xml有关,这里是:

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app version="2.5" 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_2_5.xsd">

    <!-- Beans in these files will makeup the configuration of the root web application context -->
    <!-- Bootstraps the root web application context before servlet initialization-->
    <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- Deploys the 'projects' dispatcher servlet whose configuration resides in /WEB-INF/servlet-config.xml-->
    <servlet>
        <servlet-name>p</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>
            /WEB-INF/servlet-config.xml         
            </param-value>
        </init-param>
    </servlet>

    <!-- Maps all /p URLs to the 'p' servlet -->
    <servlet-mapping>
        <servlet-name>p</servlet-name>
        <url-pattern>/p/*</url-pattern>
    </servlet-mapping>

   <error-page>
   <error-code>404</error-code>
   <location>/p/error</location>
   </error-page>


   <!-- force encoding on the requests -->
   <filter>
    <filter-name>encoding-filter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
      <param-name>encoding</param-name>
      <param-value>utf-8</param-value>
    </init-param>
    <init-param>
      <param-name>forceEncoding</param-name>
      <param-value>true</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>encoding-filter</filter-name>
    <url-pattern>/*</url-pattern>
    <dispatcher>REQUEST</dispatcher>
    <dispatcher>FORWARD</dispatcher>
  </filter-mapping>

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



   <filter>
    <filter-name>UrlRewriteFilter</filter-name>
    <filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>

  </filter>
  <filter-mapping>
    <filter-name>UrlRewriteFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>





    <!-- Security -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
      /WEB-INF/application-config.xml
      /WEB-INF/app-security.xml
      /WEB-INF/mvc-config.xml
    </param-value>
    </context-param>


    <session-config>
      <session-timeout>1</session-timeout> 
    </session-config>


</web-app>

任何想法如何解决这个问题?

【问题讨论】:

    标签: spring spring-mvc spring-security


    【解决方案1】:

    你说过:

    我希望如果用户在登录或未登录的情况下输入了错误的 url,他会直接重定向到错误页面

    Spring security 会在它知道它的 url 是否有效之前拦截每个请求,因此获取它的一种方法是拦截所有具有某些模式的有效 url,并在最后添加一个任何人都可以访问的通用模式.

    <intercept-url pattern="/" access="permitAll" />
    <intercept-url pattern="/validUrl1Pattern"  access="permitAll" />  
    <intercept-url pattern="/validUrl2Pattern"  access="permitAll" />  
    <intercept-url pattern="/validUrl2Pattern"  access="permitAll" />  
    ...
    <intercept-url pattern="/**" access="ROLE_ANONYMOUS" />
    

    这种配置的问题是,如果您的应用程序很复杂,可能很难找到所有有效 url 的模式。

    【讨论】:

    • “默认情况下拒绝访问通常是一种很好的做法,而不仅仅是保护我们需要的资源。” - 引用自Spring Security tutorial
    【解决方案2】:

    是的,只需添加以下内容:

    <intercept-url pattern="/error/**" access="permitAll" />
    

    这样任何人都可以访问您的所有错误页面。

    【讨论】:

      【解决方案3】:

      当您设置属性access="true" 时,您告诉spring-security 检查用户是否具有名为“true”的安全属性(通常是一个角色)。我认为这不是你的目标?

      要绕过安全性,您可以设置filters="none" 并跳过访问属性: &lt;intercept-url pattern="/errorpage" filters="none" /&gt;

      documentation of <intercept-url>

      【讨论】:

        【解决方案4】:

        将 /error 添加到您的 &lt;intercept-url/&gt; 元素列表中,这样它就不需要身份验证即可访问它。

        【讨论】:

        • 只是添加这条规则吗?
        • 我收到了拒绝访问异常,因为该页面不存在,匹配使用规则 access="isAuthenticated()" 的模式“/**”,并且在未登录用户的情况下该角色是匿名的,因此用户被重定向回登录页面
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-01-30
        • 2014-05-08
        • 2013-12-25
        • 1970-01-01
        • 1970-01-01
        • 2016-08-20
        相关资源
        最近更新 更多