簡體   English   中英

我的網絡應用程序的簡單登錄和注銷功能(JSF 2.0)

[英]Simple login and logout capabilities for my web app(JSF 2.0)

今天早上我騎了JEE6教程的第39,40和41章。 但我非常非常困惑。 我沒有關於JEE6的web-app安全性的背景知識,我在理解和實施方面遇到了很大的困難。

我需要為我的網絡應用程序創建一個授權機制,對於像我這樣的JEE6中的初學者,我的場景也不是很簡單,所以我決定嘗試找到最簡單的方法。

我想解釋一下我的想法,所以你可以糾正我並給我一些建議,告訴我這是最簡單的方法。

理念:

我的網絡應用程序使用名為dock的primefaces組件,當用戶點擊最后一項時,它會彈出登錄對話框。 此導航工具也位於JSF模板中,該模板由應用程序中的所有其他頁面使用。

    <h:body>
<p:dock position="top"> 
    <p:menuitem value="Naslovna" icon="unsecuredimages/naslovna.png" url="main.xhtml"
        alt="The image could not be found." />
    <p:menuitem value="Register" icon="unsecuredimages/register.png"
        url="registration.xhtml" alt="The image could not be found." />
    <p:menuitem value="Cesta pitanja" icon="unsecuredimages/faq.png"
        url="faq.xhtml" alt="The image could not be found." />
        <!-- The login will not have a page, it will pop up a login dialog -->
    <p:menuitem value="Login" icon="unsecuredimages/login.png" url="#" onclick="dlg.show()"/>       
</p:dock>
<p:dialog header="Prijavite se" widgetVar="dlg"  modal="true" draggable="false" resizable="false" effect="SLIDE">  
     <h:outputText value="Em@il:" /><h:inputText id="email" value=""/>
     <br/>  
     <h:outputText value="Lozinka:" /><h:inputText id="password" value=""/>
     <br/> 
     <h:commandButton value="Prijavi se" />
</p:dialog>     
    <br/><br/><br/><br/><br/><br/>  
<ui:insert name="mainForm" />
<ui:insert name="registrationForm" />
<ui:insert name="registrationBuyerForm" />
<ui:insert name="registrationSellerForm" />
<ui:insert name="faqForm" />
<ui:insert name="registrationSuccessForm" />
  </h:body>

我認為JSF應該有一個支持bean來處理電子郵件和密碼到EJB。

import javax.ejb.EJB;
import javax.enterprise.context.SessionScoped;
import javax.faces.bean.ManagedBean;
import ejbinterfaces.IAuthentificationEJB;
@ManagedBean
@SessionScoped
public class SecurityController {
@EJB
private IAuthentificationEJB authentificationEJB;
private String email;
private String password;

public void logIn() {
    authentificationEJB.saveUserState(email, password);
}

public String getEmail() {
    return email;
}

public String getPassword() {
    return password;
}

public void setEmail(String email) {
    this.email = email;
}

public void setPassword(String password) {
    this.password = password;
}   

}

然后EJB應該登錄並注銷(這是我非常困惑的地方):

@Stateful(name = "ejbs/AuthentificationEJB")
public class AuthentificationEJB implements IAuthentificationEJB {      
//Login
public boolean saveUserState(String email,String password) {
    //1-Send query to database to see if that user exist
    //2-If the query returns the user object, store it somewhere in the session(HOW?)
    //3-return true if the user state was saved
    //4-return false otherwise
    return false;
}


//Logout
public void releaseUserState() {
    //1-Check if there is something saved in the session(or wherever the state is saved)
    //2-If 1 then flush it
}

//Check if user is logged in
public boolean checkAuthentificationStatus() {
    //1-Check if there is something saved in the session(This means the user is logged in)
    //2-If there is not a user already loged, then return false
    return false;
}

}

我決定不使用jdbc領域或JEE6教程中解釋的其他身份驗證機制,因為我真的很困惑,所以我認為現在我更容易手動完成。 這是我對我的方法的一些疑問:

  • 這種方法是否正確(可以這樣做)嗎?
  • 在這種情況下,EJB應該是@Stateless還是@Statefull(從數據庫中重新獲取的用戶是否有2個String字段)?
  • 我應該從數據庫中存儲檢索到的用戶的ID,直到用戶決定退出為止?

  • 如果我必須在會話中存儲用戶狀態,直到他/她決定退出,我該怎么辦?

  • 使用這種方法,當關閉瀏覽器而不注銷時,用戶的會話將被降級(如果沒有,如果沒有活動,我怎么能在一段時間后自動使他/她的會話到期?)

我非常感謝你的幫助。

拼圖的一些部分:

這種方法是否正確(可以這樣做)嗎?

是的,它可以。 您可以選擇容器管理的安全性或應用程序管理。

在這種情況下,EJB應該是@Stateless還是@Statefull(從數據庫中重新獲取的用戶是否有2個String字段)?

如果您將當前登錄用戶的id存儲在會話上下文中(見下文),我認為您可以使用無狀態bean(來自理論)來實現。

我應該從數據庫中存儲檢索到的用戶的ID,直到用戶決定退出為止?

您可以將其存儲在會話上下文中:

FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("userID", email);

使用getSessionMap()#get("userID")來檢查存儲的userID。

使用這種方法,當關閉瀏覽器而不注銷時,用戶的會話將被降級(如果沒有,如果沒有活動,我怎么能在一段時間后自動使他/她的會話到期?)

不,會話將在達到超時時自動過期。 可以在web.xml中設置超時:

<session-config>
   <session-timeout>60</session-timeout>
</session-config>

此設置意味着會話將在服務器不活動60分鍾后超時。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM