问题 Spring错误导致Spring安全问题?


问候所有,我使用的是spring security 3.0.2,urlRewrite 3.1.0 ,我有一个弹簧安全问题,我有一个规则,即应用程序中的所有页面都需要身份验证,除了一些页面,所以我的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>

问题是,如果我不是登录用户,并在app / notFoundUrl中键入了应用程序中不存在的某个URL,则spring security将此页面与需要身份验证的模式/ **匹配,因此用户是未按预期重定向到错误页面,但重定向到登录页面,然后重定向到错误页面

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

我认为这个问题与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>

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


5329
2017-11-11 10:28


起源



答案:


你说过:

我希望如果用户输入了一个坏URL,如果他已登录,他会直接重定向到错误页面

Spring安全性会在知道其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的模式。


6
2017-11-30 13:23



“默认拒绝访问通常是一种好习惯,而不仅仅是保护我们需要的资源。” - 引自 Spring安全教程 - michal.kreuzman


答案:


你说过:

我希望如果用户输入了一个坏URL,如果他已登录,他会直接重定向到错误页面

Spring安全性会在知道其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的模式。


6
2017-11-30 13:23



“默认拒绝访问通常是一种好习惯,而不仅仅是保护我们需要的资源。” - 引自 Spring安全教程 - michal.kreuzman


是的,只需添加:

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

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


3
2017-11-15 20:21



帖子已经编辑,有什么帮助吗? - Mahmoud Saleh


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

为了绕过安全,你可以设置 filters="none" 并跳过访问属性: <intercept-url pattern="/errorpage" filters="none" />

看到 <intercept-url>的文档


3
2017-11-12 10:47





添加/错误到您的列表 <intercept-url/> 元素,以便它不需要身份验证才能访问它。


2
2017-11-11 20:47



是否只是添加此规则? <intercept-url pattern =“/ error”filter =“none”/> - Mahmoud Saleh
我有一个访问被拒绝的异常,因为页面不存在,匹配使用规则access =“isAuthenticated()”的模式“/ **”,对于未登录用户,角色是anonyomus,所以用户是重定向回登录页面 - Mahmoud Saleh
帖子已经编辑,有什么帮助吗? - Mahmoud Saleh