【问题标题】:Invalidate Http Session in JSF2 on Logout注销时 JSF2 中的 Http 会话无效
【发布时间】:2015-01-04 05:29:45
【问题描述】:

版本:

Apache MyFaces 2.1.14

Rich Faces 4.3.5

问题:

当用户单击注销页面时,我想使 JSF 会话无效。 我决定使用PreRenderViewEvent 来做同样的事情。 以下是相同的设计:

在渲染注销页面时,通过PreRenderViewEvent在后端bean中调用一个监听器方法。 使 Java 方法中的 JSF 会话无效,例如:FacesContext.getCurrentInstance().getExternalContext().invalidateSession();

我的问题是:这是使会话无效的正确方法吗?或者有没有更好的方法可用?

代码:

<!-- call this in Logout.xhtml page -->
<f:event listener="#{bean.invalidateSession}" type="preRenderView" />

编辑 1:

让我详细说明问题的背景。 我的系统中有两个 Web 应用程序。 当用户单击注销按钮时,它总是转到 webApp1。 要求是使 webApp1 中的会话无效,然后重定向到 webApp2 再次使会话无效。 在呈现 webapp1 LogOut 页面时将使用 preRenderView 事件。那里的侦听器将使会话无效并重定向到 webapp2 LogOut 页面,如下代码:

public void logOut(ComponentSystemEvent event) throws IOException{

            //logoutAction param will be read from request 
            String redirectUrl = "/webapp2/Logout.xhtml?action="+logoutAction;
            FacesContext.getCurrentInstance().getExternalContext().invalidateSession();
            FacesContext.getCurrentInstance().getExternalContext().redirect(redirectUrl);

}

【问题讨论】:

    标签: session jsf-2


    【解决方案1】:

    我通常只是在我的模板中添加一个按钮:

    <h:commandLink action="#{loginBean.logout()}" value="Logout" rendered="#{!empty loginBean.account}" />
    

    这个按钮调用我的“登录”管理器bean,它包含一个属性Account account,填充了用户的参数。它也仅在用户连接时才呈现。

    public String logout()
    {
        setAccount(null);
        FacesContext.getCurrentInstance().getExternalContext().getSessionMap().clear();
        FacesContext.getCurrentInstance().getExternalContext().invalidateSession();
    
        NavigationUtils.redirect("/admin/login");
        return "";
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-01-21
      • 1970-01-01
      • 2013-03-14
      • 2021-03-09
      • 2012-01-24
      • 1970-01-01
      • 2015-08-14
      • 1970-01-01
      相关资源
      最近更新 更多