2011-02-28 45 views
4

正如預期的那樣,當請求保護/安全資源的登錄頁面加載:Servlet 3.0之後重定向到受保護資源或原始/保存的請求HttpServletRequest#login()身份驗證?

<login-config> 
    <auth-method>FORM</auth-method> 
    <realm-name>jdbc</realm-name> 
    <form-login-config> 
     <form-login-page>/login.xhtml</form-login-page> 
     <form-error-page>/login.xhtml</form-error-page> 
    </form-login-config> 
</login-config> 

我明白j_security_check將自動轉發到受保護/安全資源,如果認證成功:

<form method="post" action="j_security_check"> 
    <input type="text" name="j_username"> 
    <input type="password" name= "j_password"> 
</form> 

然而,我想讓用戶註冊(或登錄)繼續,所以我用JSF 2.0: <h:form...,EL: #{loginBean.register()}...,等等...我用Servlet 3.0編程驗證:

public void register() { 
    HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest(); 

    try { 
     // Register... 

     request.login(this.username, this.password); 

     // Redirect to the protected/secure resource... 

    } catch (ServletException e) { 
     ... 
    } 
} 

如何找出最初請求的資源是什麼?可能:

  • 得到會話(特定容器) 「保存的請求」?
  • 嘗試以某種方式訪問​​「原始請求」(其中)
  • 任何與請求調度程序相關的內容(狂猜)
  • 使用「referer」標題(壞主意)
  • 創建服務器驗證模塊(SAM)(不簡單)

任何意見將非常感激!

回答

5

登錄頁面位於由forward打開的封面之下,原始請求URI可用作請求屬性,名稱爲javax.servlet.forward.request_uri

所以:

String uri = request.getAttribute("javax.servlet.forward.request_uri"); 

,或者更JSF-ISH:

String uri = externalContext.getRequestMap().get("javax.servlet.forward.request_uri"); 
+0

感謝** **很遠! – 2011-03-01 13:32:02

+0

不客氣。 – BalusC 2011-03-01 13:32:27

+1

你好,在我的情況下,日誌通知顯示重定向。我不知道如何強制它是一個前鋒,所以request_ui的作品。作爲一個重定向(我的意思是,在瀏覽器中顯示loginform.xhtml,而不是原始頁面)我得到一個request_ui的空值。任何想法如何解決這個問題?謝謝。 – icordoba 2015-01-16 11:48:09

相關問題