Search code examples
jsfshiro

Redirect to the last page visited after login using Shiro


What is the better way to login with apache shiro and redirect to the last page visited?

I only have this:

SecurityUtils.getSubject().login(new UsernamePasswordToken(username, password, rememberMe);

Solution

  • You can get the last visited page by WebUtils.getAndClearSavedRequest(). You can redirect to it using standard ExternalContext#redirect() or OmniFaces Faces#redirect() which both supports redirecting JSF ajax requests.

    SecurityUtils.getSubject().login(new UsernamePasswordToken(username, password, rememberMe));
    
    SavedRequest savedRequest = WebUtils.getAndClearSavedRequest(Faces.getRequest());
    
    if (savedRequest == null) {
        Faces.redirect("homepage.xhtml");
    } else {
        Faces.redirect(savedRequest.getRequestUrl());
    }