2013-02-09 32 views
1

我正在使用Spring Security來處理我的Web應用程序的所有身份驗證。這裏的情況,比方說我有三個JSP頁面:如何在用Spring Security成功登錄後將用戶轉發回所需的受保護頁面

1)登錄頁面
2)默認主頁(需要身份驗證)
3)用戶帳號頁面(需驗證)

標準流程的工作原理就好:

1)用戶成功地從登錄頁面登錄
2)用戶被引導到默認主頁

這裏的用例我想不通出

1)用戶試圖訪問他們的帳戶頁面(但密碼保護) - 工程
2)用戶被重定向到輸入憑據 - 工程
3)用戶成功提供了憑據 - 工程
4 )用戶被重定向到他們的帳戶頁面 - 不起作用(它將他們重定向到默認主頁)

我使用的是自定義AuthenticationSuccessHandler,但麻煩的是,每當我查看請求時,從它總是從/j_spring_security_check

是否有一些簡單的方法來隔離它們來自哪個頁面?

下面是在我的applicationContext.xml中,涉及到我的春季安全設置:

<bean id="successfulLoginService" class="com.service.SuccessfulLoginService"> 
    <property name="defaultTargetUrl" value="/listings/add_listing.htm"/> 
    </bean> 

    <security:http pattern="/**.html" security="none" /> 
    <security:http pattern="/login/*.htm" security="none" /> 
    <security:http pattern="/listings/listing.htm" security="none" /> 
    <security:http pattern="/listings/viewListing.htm" security="none" /> 
    <security:http pattern="/search/keyword/*.htm" security="none" /> 

    <security:http auto-config="true"> 
    <security:intercept-url pattern="/**.htm" access="ROLE_USER" /> 
    <security:intercept-url pattern="/**/**.htm" access="ROLE_USER" /> 
    <security:form-login login-page="/login/login.htm" authentication-failure-handler-ref="failedLoginService" authentication-success-handler-ref="successfulLoginService"/> 
    </security:http> 

    <security:authentication-manager> 
    <security:authentication-provider 
     user-service-ref="userDetailsService" /> 
    </security:authentication-manager> 

這裏是我的SuccessfulLoginService.java:

@Service("successfulLoginService") 
public class SuccessfulLoginService extends SimpleUrlAuthenticationSuccessHandler 
{ 
    @Autowired 
    UserDao userDao; 

    @Override 
    public void onAuthenticationSuccess(HttpServletRequest request, 
     HttpServletResponse response, Authentication authentication) 
     throws IOException, ServletException 
    { 
    Users user = null; 
    String username = ((SpringSecurityUser) authentication.getPrincipal()).getUsername(); 
    try 
    { 
     user = userDao.getUserByEmail(username); 
    } catch (Exception e) 
    { 
     e.printStackTrace(); 
     throw new ServletException("Failed to login", e); 
    } 
    request.getSession().setAttribute("user", user); 

    response.sendRedirect("/MyApplication" + determineTargetUrl(request, response)); 
    } 
} 

回答

3

您可以從org.springframework.security.web.savedrequest.RequestCache得到保存的請求。

內,您的自定義成功處理程序的:

RequestCache requestCache = new HttpSessionRequestCache(); 
SavedRequest savedRequest = requestCache.getRequest(request, response); 
String targetUrl = savedRequest.getRedirectUrl(); 

看看默認SavedRequestAwareAuthenticationSuccessHandler瞭解更多詳情。

+1

我不知道爲什麼我從來不知道如何做到這一點。我幾個月來一直在尋找這個答案,當我無法弄清楚的時候,我一直把它放在後面的燃燒器上。非常感謝您花時間回答我的問題Aleksandr,您是本週的英雄。 – Trevor 2013-02-11 22:50:58

0

您是否在尋找default-target-url?您可以使用它在成功登錄後將用戶重定向到某個位置。

+0

我不這麼認爲,我只是嘗試插入它,但沒有奏效。我也在使用authentication-success-handler-ref,這可能會在代碼中造成一些混淆。我嘗試在我的authentication-success-handler-ref中連接一個default-target-url,但它總是導航到那個URL(即使這並不是我在一開始就試圖訪問的那個時候) – Trevor 2013-02-09 22:28:11

相關問題