2014-01-15 185 views
0

我正在開發一個Web應用程序,並且我已經集成了用於身份驗證的spring安全性。我正在使用spring的默認登錄頁面。有來自其他應用程序和某些文檔的我的應用程序存在外部鏈接。我已經編寫了這些鏈接的控制器,以便在成功登錄後向用戶顯示所需的數據。我的問題是,當我訪問其他應用程序或文檔中的鏈接時,我將被帶到登錄頁面(如果用戶未登錄,那麼這是預期的),然後我被重定向到以前請求的URL的應用程序主頁。當我從這些鏈接進行訪問並且要求登錄時,即使會話處於活動狀態,然後在隨後將其帶到主頁,也會發生這種情況。就好像該URL被複制並粘貼到同一瀏覽器中一樣,它工作正常。這可能是由什麼原因造成的?春季安全登錄外部鏈接

這裏是我的Spring配置文件

<security:http auto-config="true"> 
     <security:intercept-url pattern="/**" access="ROLE_EDIT,ROLE_VIEW" /> 
     <security:session-management invalid-session-url="/"> 
     </security:session-management> 
    </security:http> 

    <security:authentication-manager> 
     <!--my custom authentication--> 
    </security:authentication-manager> 

誰能幫我在這?

回答

1

您可以創建自己的AuthenticationSuccessHandler。這樣

<http auto-config="true"> 
<http-basic /> 
<form-login authentication-success-handler-ref="customAuthenticationSuccessHandler"/> 
</http> 

裏面customAuthenticationSuccessHandler東西,你可以重寫onAuthenticationSuccess方法和編寫自己的代碼識別URL請求,並相應地重定向到相同的,如果它是來自外部的應用程序。

public class CustomAuthenticationSuccessHandler extends 
    SavedRequestAwareAuthenticationSuccessHandler { 

@Override 
public void onAuthenticationSuccess(HttpServletRequest request, 
     HttpServletResponse response, Authentication authentication) 
     throws ServletException, IOException { 
    UserDetails userDetails = (UserDetails) authentication 
      .getPrincipal(); 
    // CODE TO IDENTIFY THE REQUEST URL AND REDIRECT TO SAME IF IT IS EXTERNAL URL(FROM OTHER APP). 
    super.onAuthenticationSuccess(request, response, targetUrl); 

} 
+0

我以前試過這個,我從請求中得到的url是「app root」/ login,我無法使用這個方法獲取之前請求的url。 – rahul

+0

您可以嘗試在請求中追加一個參數,例如clientId,這個參數對於來自請求來源的應用程序是特定的。同樣在你的應用程序中使用特定的redirectURL維護每個clientId的映射。在處理程序中,然後根據您獲得的clientID設置redirectURL –