This question is related to my other question "How to redirect to Login page when Session is expired in Java web application?". Below is what I'm trying to do:
- I've a JSF web application running on JBoss AS 5
- When the user is inactive for, say 15 minutes, I need to log out the user and redirect him to the login page, if he is trying to use the application after the session has expired.
- So, as suggested in 'JSF Logout and Redirect', I've implemented a filter which checks for the session expired condition and redirects the user to a session-timed-out.jsp page, if the session has expired.
- I've added SessionExpiryCheckFilter on top of all other filter definitions in web.xml, so that my session expiry check will get the first hit always.
Now comes the challenge I'm facing. Since I'm using JBoss AS, when the session expired, JBoss automatically redirects me to the login page (note that the session expiry check filter is not invoked). So, after I log-in, my SessionExpiryCheckFilter intercepts the request, and it sees a session is available. But, it throws the exception javax.faces.application.ViewExpiredException: viewId:/mypage.faces - View /mypage.faces could not be restored.
Have anyone faced this issue before? Any ideas to solve this issue?
Implement javax.faces.event.PhaseListener for Restore view
register in faces-config.xml
I would suggest writing a session listener in conjunction with the filter.
When the session expires you can create a new session object and set a timeout value on the new object.
Just check for the timeout value in the filter and redirect the browser.
See http://www.java2s.com/Code/Java/Servlets/Servletsessionlistener.htm
If you are using Mojarra/Sun RI you might want to try to add this to your web.xml:
However be aware that this isn't always the perfect solution. It hides the fact that the user has lost its session.
I tried to write a filter for it but some how it was not working for me, so I made an alternate for it.
I did it like this in every page that I don't want the user to access without Login:
This will call the function
validuser()
which is in my session managed bean.Now this is my function. During login I already insert the user object into the session.
If there is a session but no one had logged in, then it will take you to a redirect page.
The following approach works for me. Note that you have to use the JSTL core taglib redirect and not the jsp redirect in order for this to work (as the jsp also expires).
In your FacesConfig.xml you put the following:
sessionExpired.jsp:
You can also use this approach for other error types or exceptions. For example the element contains a mapping between an error code or exception type and the path of a resource in the web application.:
or element contains a fully qualified class name of a Java exception type.