2014-03-30 76 views
0

我需要一點理解JSF(2.2.6)中的會話。我試圖找到一些文檔,但仍然缺少一些東西。來自同一瀏覽器的多個會話

我有一個@RequestScoped登錄bean,它保存會話映射中的參數以供其他會話作用域支持bean引用。他們通過PostConstruct方法獲取用戶信息,並且一切正常。

但是,如果使用多個窗口或用戶沒有註銷並直接返回到登錄頁面,邏輯將失敗。 JSF將它視爲同一個會話,並且不會調用@PostConstructs。

我很確定我可以使會話失效,但是這並不能解決來自不同瀏覽器窗口的多個用戶的問題。

任何指導或參考網站,將不勝感激。

在此先感謝! 約翰

+0

我只是不明白你在這裏如何使用後構造。在處理用戶輸入之前是不是應該調用它? – Leo

+0

一個瀏覽器==一個會話!根據定義。 JBoss Seam的'對話'是解決這個問題的辦法,但卻很笨拙。 – EJP

+0

@Leo登錄身份驗證由我的登錄bean處理,然後使用返回字符串導航到另一個頁面。該頁面的呈現發生在SessionScoped bean中。 – GolfAddict

回答

1

會話HAS對於每個瀏覽器窗口都是相同的,唯一的例外是當使用匿名模式時:即,chrome的行爲類似於同時打開兩個瀏覽器。

有多個分會的另一種方式是使用不同的服務器名稱: http://localhost:8080/apphttp://127.0.0.1:8080/app可能不共享單個會話。然而,會話從不重疊。

你的問題,如果我理解正確的,是當一個記錄用戶訪問登錄頁面,重新登錄,保留他的老會議上,這就是爲什麼會話bean不再PostConstructed(獨立使用的窗口)。

一個通用的解決方案是禁止訪問登錄用戶的登錄頁面。 並且一般情況下,當用戶重新登錄而沒有事先註銷時,容器將拋出AlreadyAuthenticatedException或類似錯誤。

削減長話短說,只是一個初步的例子等着你的代碼:

@ManagedBean 
@SessionScoped 
public class UserBean implements Serializable 
{ 
    private static final long serialVersionUID = 1L; 

    private User user; 

    public boolean isLoggedIn() 
    { 
     return user != null; 
    } 

    public void login(String username, String password) 
    { 
     // maybe you want to check isLoggedIn() and either call logout() or throw an exception 

     HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest(); 
     request.login(username, password); 

     user = someDAO.loadFromDatabase(username); 
    } 

    public void logout() 
    { 
     // maybe you want to check isLoggedIn() and either throw an exception or do nothing 

     HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest(); 
     request.logout(); 

     user = null; 

     // suggested for another scenario 
     // request.getSession().invalidate(); 
    } 

    // getters and setters 
} 

@ManagedBean 
@SessionScoped 
public class OperationBean implements Serializable 
{ 
    private static final long serialVersionUID = 1L; 

    @ManagedProperty("#{userBean}") 
    private UserBean userBean; 

    public void execute() 
    { 
     if(!userBean.isLoggedIn()) 
     { 
      FacesContext.getCurrentInstance().getExternalContext().redirect("login.jsf"); 
      return; 
     } 

     User user = userBean.getUser(); 

     // do something 
    } 

    // getters and setters 
} 

與此相結合,而不是使用OperationBean的@PostContruct我用@ManagedProperty,所​​以OperationBean包含對用戶的始終最新的引用,而不必關心多次重新登錄。

相關問題