2012-08-17 19 views
4

我有一個返回404響應狀態時顯示的錯誤頁面。 由於模板機制(我使用瓷磚)生成此頁面;在這個模板,我有一個包含類似的東西頭:isAnonymous()和isAuthenticated()在錯誤頁面上返回false

<sec:authorize access="isAnonymous()"> 
     blabla 
    </sec:authorize> 
    <sec:authorize access="isAuthenticated()"> 
     blibli 
    </sec:authorize> 

所以,這取決於如果用戶進行身份驗證,它顯示blibli或布拉布拉。此代碼適用於使用此模板的所有頁面,除了我的404頁面!它什麼都不顯示!

任何想法??

回答

10

我敢打賭,問題在於你如何定義filter-mappingweb.xml。最常見的配置是:

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

這映射過濾器將所有的URL,但只有當他們被REQUEST方法來訪問。所有其他情況(如INCLUDEFORWARDERROR)都未被此篩選器捕獲。因此,要將過濾器綁定到ERROR請求,請將其定義爲

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

     <!-- apply Spring Security authentication to error-pages --> 
     <dispatcher>ERROR</dispatcher> 
</filter-mapping> 

試試吧。如果不起作用,則添加<dispatcher>INCLUDE</dispatcher>,因爲Tiles可能會以這種方式包含頁面。

參見:

+0

的確! '錯誤'工程!我今天學到了一些東西:D – tibo 2012-08-23 06:01:47

0

如果此404頁面超出了您的安全網址,則isAnonymous()和isAuthenticated()將返回false並且不呈現任何內容。

1

對於那些尋找Java配置:

private void registerSpringSecurityFilterChain(ServletContext servletContext) { 
    FilterRegistration.Dynamic springSecurityFilterChain = servletContext.addFilter(
     BeanIds.SPRING_SECURITY_FILTER_CHAIN, new DelegatingFilterProxy()); 
    springSecurityFilterChain.addMappingForUrlPatterns(null, false, "/*"); 
    springSecurityFilterChain.addMappingForUrlPatterns(EnumSet.of(DispatcherType.ERROR), false, "/*"); 

} 
相關問題