1
votes

Version :

Apache MyFaces 2.1.14

Rich Faces 4.3.5

Issue :

I want to invalidate JSF session when user clicks on Logout page. I have decided to use PreRenderViewEvent for the same. Below is the design for the same :

When logout page is being rendered , call a listener method in back end bean through PreRenderViewEvent. Invalidate JSF session in java method like : FacesContext.getCurrentInstance().getExternalContext().invalidateSession();

My question is : is this a right approach in invalidating session ? Or is there any better approach available ?

Code :

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

EDIT 1:

Let me elaborate the context of the question. I have two web applications in my system. When a user clicks on Logout button it always goes to webApp1. The requirement is to invalidate session in webApp1 and then redirect to webApp2 where again invalidate the session. The preRenderView event will be used when webapp1 LogOut page is being rendered. The listener there will invalidate the session and redirect to webapp2 LogOut page like below code :

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);

}
1

1 Answers

0
votes

I usually just add a button to my template :

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

This button calls my "login" manager bean, which contains an attribute Account account, populated with the user's parameters. It's also only rendered if the user is connected.

public String logout()
{
    setAccount(null);
    FacesContext.getCurrentInstance().getExternalContext().getSessionMap().clear();
    FacesContext.getCurrentInstance().getExternalContext().invalidateSession();

    NavigationUtils.redirect("/admin/login");
    return "";
}