JSF 2.0 redirect to index.jsf after login

mbulau picture mbulau · Feb 28, 2013 · Viewed 10.8k times · Source

We have a JSF (2.0) based web application, running on JBoss 6.1. We are using the FORM based authentication with JAAS.

Some of the users adding links like this "admin/editUser.jsf" to their bookmarks. This page don't work correctly if the user access this page directly (without using the navigation within the application).

The question is: is there any way to redirect the user to the index.jsf page after login, independent from the requested url?

Answer

BalusC picture BalusC · Feb 28, 2013

That's not possible.

If you're on Servlet 3.0 (Tomcat 7 / Glassfish 3 / JBoss 6 / etc), then your best bet is to use programmatic login with HttpServletRequest#login() instead of a JAAS form.

So, instead of

<form action="j_security_check" method="post">
    ...
    <input type="submit" />
</form>

use

<h:form>
    ...
    <h:commandButton value="Login" action="#{bean.login}" />
</h:form>

with

public String login() {
    // ...

    request.login(username, password);

    // ...

    return "index.jsf?faces-redirect=true";
}

See also: