2012-08-07 41 views
1

我正在使用Spring Security 3.1,並且我想在用戶登錄之前獲取來自URL的參數。所以,我期待着訪問我的登錄頁面的用戶向我發送一個redir參數(包含URL他想在他認證之後去)。當我請求的頁面,當我嘗試不提交登錄表單如何在Spring Security中獲取GET參數?

例如:?本地主機/對myApp導向= ​​my.app.com /定製

如何從獲得再導向PARAM網址是什麼?我試過幾件事,包括重寫SimpleUrlAuthenticationSuccessHandler和調用request.getParameter(「redir」),但它返回null。我也嘗試實現我自己的過濾器UsernamePasswordAuthenticationFilter並在attemptAuthentication上調用getParameter(),但返回null。

UPDATE更多信息: 我的登錄頁面相當簡單,基本上是:通過POST方法調用/ j_spring_security_check的表單(使用j_username和j_password參數)。

我的身份驗證提供程序實現AuthenticationManager,因此在用戶通過身份驗證後,身份驗證對象將與授權列表,用戶,密碼一起返回。


我還實現了SimpleUrlAuthenticationSuccessHandler,並且方法onAuthenticationSuccess()將檢查他的權限,如果通過身份驗證並獲得許可,用戶將被重定向到他在URL上通知的頁面(現在,當我嘗試調用request.getParemeter(「redir」 )這種方法,我得到空。

public class GetURLFilter extends UsernamePasswordAuthenticationFilter{ 
@Override 
public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) 
     throws AuthenticationException { 

    String paginaRedirecionar = request.getParameter("redir"); 
    request.getSession().setAttribute("redirecionamento", paginaRedirecionar); 

    return super.attemptAuthentication(request, response); 
} 
} 

<bean id="authenticationFilter" 
    class="com.uolinc.adm.security.GetURLFilter" 
    p:authenticationManager-ref="radiusAuthenticationManager" 
    p:authenticationFailureHandler-ref="radiusAuthenticationFailureHandler" 
    p:authenticationSuccessHandler-ref="radiusAuthenticationSuccessHandler" /> 

<security:http auto-config="false" 
    use-expressions="true" 
    access-denied-page="/auth/denied" entry-point-ref="authenticationEntryPoint" 
    disable-url-rewriting="true" 
    access-decision-manager-ref="accessDecisionManager"> 

    <security:logout logout-url="/auth/logout" logout-success-url="/auth/login" /> 

    <security:custom-filter position="FORM_LOGIN_FILTER" ref="authenticationFilter" /> 
</security:http> 

謝謝

+0

你所描述的是適當的方式來做到這一點,所以無論你嘗試過你做錯了什麼。如果你爲你的問題添加了一些信息(比如你的彈簧安全配置和你的登錄頁面/控制器),也許我可以幫你弄明白。 – pap 2012-08-08 12:44:35

+0

在請求登錄頁面或提交期間發送參數嗎? – 2012-08-08 13:37:45

+0

感謝回答pap,我添加了一些信息(這是很多,所以我嘗試了什麼是最重要的) – thevoyager 2012-08-08 13:46:53

回答

3

UsernamePasswordAuthenticationFilterSimpleUrlAuthenticationSuccessHandler登錄表單,這是從呈現形式的一個不同的請求的提交過程中被調用。所以這是一個參數,目前durin g當您提交表格時,表格的請求將不會出現。您需要在會話中緩存參數,或者將其作爲隱藏參數添加到登錄表單中,使用正常的登錄參數重新提交(以便您的AuthenticationSuccessHandler可用)。

請注意,以這種方式使用客戶端提供的重定向數據是有風險的,除非您有適當的驗證(例如檢查URL是否在您的應用程序中)。否則,攻擊者可以提供鏈接到惡意站點的URL,並且可能在用戶不知情的情況下劫持已認證的會話或在受保護的站點上執行操作。

+1

我必須做的是實現UsernamePasswordAuthenticationFilter,並將邏輯放在doFilter()方法中獲取請求參數,而不是attemptAuthentication()。在驗證之前和之後調用doFilter()方法,正如名字所暗示的那樣,僅在嘗試驗證之後調用驗證方法。這就是訣竅,我得到了參數並插入了會話中。謝謝你們! – thevoyager 2012-08-08 16:43:18

相關問題