2013-07-08 53 views
5

我想爲Web應用程序實現自定義錯誤頁面。我用下面的方法:Spring MVC:錯誤頁面和主要信息

的web.xml

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

彈簧security.xml文件

<http use-expressions="true"> 
    <form-login ... /> 
    <access-denied-handler error-page="/403/" /> 
    .... 
</http> 

兩個網頁通過適當的控制器處理。但似乎principal在這種情況下是不可接受的,即我無法獲得有關當前登錄用戶的任何信息。

它是默認行爲還是我在代碼中有錯誤?

謝謝

UPD#1: 我的配置:

<listener> 
    <listener-class> 
      org.springframework.web.context.ContextLoaderListener 
     </listener-class> 
    </listener> 
    <context-param> 
    <param-name>contextConfigLocation</param-name> 
    <param-value> 
     /WEB-INF/spring-service.xml 
     /WEB-INF/spring-security.xml 
     /WEB-INF/spring-data.xml 
     /WEB-INF/spring-mail.xml 
    </param-value> 
    </context-param> 
    <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>hibernateFilter</filter-name> 
    <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class> 
    <init-param> 
     <param-name>sessionFactoryBeanName</param-name> 
     <param-value>sessionFactory</param-value> 
    </init-param> 
    </filter> 
    <filter-mapping> 
    <filter-name>hibernateFilter</filter-name> 
    <url-pattern>/*</url-pattern> 
    </filter-mapping> 
    <servlet> 
    <servlet-name>spring</servlet-name> 
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
    <load-on-startup>0</load-on-startup> 
    </servlet> 
    <servlet-mapping> 
    <servlet-name>spring</servlet-name> 
    <url-pattern>/</url-pattern> 
    </servlet-mapping> 
+0

如何你在web.xml文件中映射安全過濾器嗎? – Pastur

+0

@AbelPastur查看更新 – nKognito

回答

6

從錯誤頁面訪問主要數據,你需要映射你的春季安全過濾器:

<filter-mapping> 
    <filter-name>springSecurityFilterChain</filter-name> 
    <url-pattern>/*</url-pattern> 
    <dispatcher>FORWARD</dispatcher> 
    <dispatcher>REQUEST</dispatcher> 
    <dispatcher>ERROR</dispatcher> 
</filter-mapping>