JSF 2.0 redirects homepage if it is already registered

How to redirect the user to the home page if the user is already registered. I use the Filter class for the login page, but it does not work correctly. my code is:

@WebFilter(filterName = "loginFilter", urlPatterns ={"/login.xhtml"}) public class LoginFilter implements Filter{ private FilterConfig filterconfig; @Override public void init(FilterConfig filterConfig) throws ServletException { this.filterconfig = filterconfig; } @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletRequest httprequest =(HttpServletRequest) request; HttpServletResponse httpresponse =(HttpServletResponse) response; try{ if(httprequest.getUserPrincipal() != null){ System.out.printf("User authenticated with " + httprequest.getRemoteUser() + " username conected."); httprequest.getRequestDispatcher("/home.xhtml").forward(request, response); } else{ chain.doFilter(request, response); } }catch(Exception){ //do something } } @Override public void destroy() { System.out.print("Existing from loginFilter"); } } 

Here is my problem: the user registered on the Internet and made navigation and much more, and then returned to the login page using the return button of the browser without logging out and again enter the username, and then click the login button. Then it throws an IndexOutOfBoundsException exception. I just need to check when the user goes to the login page using the link or link back, and redirect to the home page. any suggestion?

+4
source share
1 answer

Just tell the browser not to cache the login page. Add the following filters to the filter immediately after you respond to httpresponse .

 httpresponse.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); // HTTP 1.1. httpresponse.setHeader("Pragma", "no-cache"); // HTTP 1.0. httpresponse.setDateHeader("Expires", 0); // Proxies. 

Otherwise, the browser will display the page from the cache on the back button instead of sending a full request to the server that should run the filter.

+3
source

All Articles