Question

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);
Was it helpful?

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());
}

OTHER TIPS

I think that a more straight forward approach is:

SecurityUtils.getSubject().login(new UsernamePasswordToken(username, password, rememberMe));
WebUtils.redirectToSavedRequest(request, response, "login.xhtml");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top