For logging purposes, I am interested in detecting when a session timeout occurs in my JSF application.
I have implemented a PhaseListener that checks if users are logged in and have their session already active. My implementation of afterPhase method is:
var url_accepted (used in the code) contains a list of public pages that users should have access in order to provide a login form.
public void afterPhase(PhaseEvent event) { FacesContext context = event.getFacesContext(); HttpSession session = (HttpSession) context.getExternalContext().getSession(true); AuthenticationBean sessionBean = (AuthenticationBean) session.getAttribute("sessionBean"); String url_req = context.getViewRoot().getViewId(); //1. Check if user has a session and is logged in: if(((sessionBean == null) || (sessionBean != null && !sessionBean.isLoggedIn())) && !url_accepted.contains(url_req)){ context.getApplication().getNavigationHandler().handleNavigation(context,null,"auth_error"); return; } //2. Code continues in order to check if a logged user has permissions to access the requested page(not relevant): }
When an user has been disconnected due to a session timeout, the PhaseListener cannot retrieve my sessionBean from the ExternalContext and assigns null to the sessionBean attribute. At this point I cannot distinguish whether the user hasn't been logged-in before or has been disconnected by timeout.
I've read that one can use errorPages in order to detect a ViewExpiredException exception and redirect the view to a specific page. But i don't know if it's possible to manage this exception in my source code.
My question is: Can I catch this ViewExpiredException inside my PhaseListener implementation in order to handle a session timeout?
Thanks in advance.