Session 如何使JSF2.0中的会话无效?
在JSF2.0应用程序中,使会话无效的最佳方法是什么?我知道JSF本身不处理会话。到目前为止我能找到Session 如何使JSF2.0中的会话无效?,session,jsf-2,httpsession,managed-bean,session-scope,Session,Jsf 2,Httpsession,Managed Bean,Session Scope,在JSF2.0应用程序中,使会话无效的最佳方法是什么?我知道JSF本身不处理会话。到目前为止我能找到 private void reset() { HttpSession session = (HttpSession) FacesContext.getCurrentInstance() .getExternalContext().getSession(false); session.invalidate(); } 这个方法正确吗?有没有一种方法不去碰地板
private void reset() {
HttpSession session = (HttpSession) FacesContext.getCurrentInstance()
.getExternalContext().getSession(false);
session.invalidate();
}
@SessionScoped
UserBean处理
用户的登录和注销。我在同一个bean中有这个方法。现在
完成必要的DB后调用reset()
方法时
更新后,我的当前会话范围bean将发生什么变化?自从
甚至bean本身也存储在HttpSession
@ManagedBean
@SessionScoped
public class UserManager {
private User current;
public String logout() {
FacesContext.getCurrentInstance().getExternalContext().invalidateSession();
return "/home.xhtml?faces-redirect=true";
}
// ...
}
我的当前会话作用域bean将发生什么?因为连bean本身都存储在HttpSession中 它在当前响应中仍然可以访问,但在下一个请求中不再存在。因此,在invalidate之后触发重定向(新请求)是很重要的,否则您仍然显示来自旧会话的数据。重定向可以通过向结果中添加
faces redirect=true
来完成,就像我在上面的示例中所做的那样。发送重定向的另一种方法是使用
但是,在这种情况下,它的使用是有问题的,因为使用导航结果更简单。前端代码是:
public void logout() {
FacesContext.getCurrentInstance().getExternalContext().invalidateSession();
}
<h:form>
<h:commandLink action="#{userManager.logout()}">
<span>Close your session</span>
</h:commandLink>
</h:form>
public String logout() {
HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
if (session != null) {
session.invalidate();
}
return "/login.xhtml?faces-redirect=true";
}
@BalusC,
ExternalContext#invalidateSession()
和HttpSession#invalidate()
之间有什么区别?@Patrick:从功能上讲,没有什么区别。他们两个做的完全一样ExternalContext#invalidateSession()
在封面下调用HttpSession#invalidate()
(另请参见我答案中的javadoc链接)。从技术上来说,ExternalContext
方法更好。基本上,您应该努力在任何与JSF相关的工件中实现零javax.servlet.*
导入。SSL会话呢?有可能使它无效吗?因为我尝试了建议的方法,但没有成功
public String logout() {
HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
if (session != null) {
session.invalidate();
}
return "/login.xhtml?faces-redirect=true";
}