Spring Security问题出现404错误?


问题内容

问候大家,我使用的是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>

问题是,如果我不是登录用户,并且键入了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>

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


问题答案:

你说过:

我希望,如果用户输入了错误的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的模式。