2012-07-16 12 views
0

我正在開發一個使用Spring Security(3.1)的應用程序,並且遇到了以下問題。當用戶註銷時,我想重定向到某個自定義URL,具體取決於他是否從安全頁面註銷。我寫了一個定製LogoutHandler,看起來如下:Spring安全定製LogoutSuccessHandler獲取奇怪的身份驗證對象

@Override 
public void onLogoutSuccess(HttpServletRequest request, 
     HttpServletResponse response, Authentication authentication) 
     throws IOException, ServletException { 


    String refererUrl = request.getHeader("Referer"); 
    if (requiredAuthentication(refererUrl, authentication)) { 
     response.sendRedirect(request.getContextPath()); 
    } else { 
     response.sendRedirect(refererUrl); 
    } 
} 

private boolean requiredAuthentication(String url, Authentication authentication){ 
    return !getPrivilegeEvaluator().isAllowed(url, authentication); 
} 

因此,當用戶從他被註銷,並被重定向到相同的URL非安全頁面退出,如果他是OUF無法登錄安全頁面,他去索引頁面。 問題是,來到該方法的身份驗證對象始終得到身份驗證(即使該方法在AFTER根據規範註銷用戶後調用)。 我的安全上下文:

<http use-expressions="true" disable-url-rewriting="true" request-matcher-ref="requestMatcher" > 


    <intercept-url pattern="/admin/**" access="hasRole('ROLE_ADMIN')" requires-channel="https" /> 
    <intercept-url pattern="/dashboard/**" access="hasRole('ROLE_OWNER')" requires-channel="https" /> 
    <intercept-url pattern="/**" access="permitAll"/> 

    <form-login login-page="/login" 
       authentication-success-handler-ref="successHandler" 
       authentication-failure-url="/login" 
       login-processing-url="/validate" /> 

    <logout logout-url="/logout" invalidate-session="true" success-handler-ref="logoutSuccessHandler" /> 

    <remember-me services-ref="rememberMeServices" key="KEY" use-secure-cookie="false" /> 

    <session-management session-fixation-protection="migrateSession"> 
     <concurrency-control max-sessions="1" /> 
    </session-management> 

</http> 

你有什麼想法,爲什麼收到的認證仍然有效,gettig到logoutSuccessHandler什麼時候?我不能編輯這個對象,因爲它的字段是最終的(除isAuthenticated,但它不是通過isAllowed()方法檢查的。)

回答

3

看看Spring Security源代碼,LogoutFilter從SecurityContextHolder ,通過SecurityContextLogoutHandler將其保留在本地變量上,並將其從持有者中移除。在所有的LogoutHandler被調用後,它會調用LogoutSuccessHandler並傳遞Authentication對象。

即使它說它是有效的,它不再在SecurityContextHolder中,因此對於Spring,用戶已註銷。

+0

是的,我已經檢查了源代碼,所以LogoutSuccessHandler在註銷之前獲取Authentication對象,這就是爲什麼它具有所有授予等等。我保證,如果您使用LogoutSuccessHandler中的Authentication進行isAllowed()返回true,邏輯上認爲它應該返回false(因爲我們已經註銷了..)。我用其他方式解決了我的問題,但是你的問題是正確的,所以我標記爲已回答:) – Mat 2012-07-17 12:57:35

相關問題