會話HAS對於每個瀏覽器窗口都是相同的,唯一的例外是當使用匿名模式時:即,chrome的行爲類似於同時打開兩個瀏覽器。
有多個分會的另一種方式是使用不同的服務器名稱: http://localhost:8080/app
和http://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包含對用戶的始終最新的引用,而不必關心多次重新登錄。
我只是不明白你在這裏如何使用後構造。在處理用戶輸入之前是不是應該調用它? – Leo
一個瀏覽器==一個會話!根據定義。 JBoss Seam的'對話'是解決這個問題的辦法,但卻很笨拙。 – EJP
@Leo登錄身份驗證由我的登錄bean處理,然後使用返回字符串導航到另一個頁面。該頁面的呈現發生在SessionScoped bean中。 – GolfAddict