Search code examples
jsfcookiesauthenticationredirectwithcookies

jsf auto-login according to cookie


I'm having a problem in achieving an auto-login feature for my JSF application which is being proted from a JSP & servlet-based architecture.

I used LoginBean backing the jsf page with a constructor method to check if the cookie containing the user crendtials exists, and if it does to automatically open a session and redirect the user to the main page.

The auto-login never takes place and the user always remains in front of the login form. What should I do to make it work?


Solution

  • Here is an option that should work. You can do something like this once you've ascertained that they are valid:

    final ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
    
    try {
        HttpServletResponse response = (HttpServletResponse) externalContext.getResponse();
    
        if (!response.isCommitted()) {
            externalContext.redirect("loggedin.xhtml");
        }
    } catch (IOException ex) {
        // log etc.
    }