0

I have a ViewScoped JSF page. There exists an ace:dialog where the user is working in. If the user does not click for two hours, his session becomes automaticly destroyed by tomcat.

If the user send a request after that two hours, I redirect to the login page (because the user is logged out). Problem is that I become an error:

java.lang.IllegalStateException: Cannot call sendRedirect() after the response has been committed

Is there a way to redirect every request to the login page if the user become logged out? What happened to my Backing Beans if the session got destroyed?

Thats the way of my redirect if the user is request a subsite and is not logged in:

@PostConstruct
public void initialize() {

    logger.debug("Start - " + new Throwable().getStackTrace()[0]);

    if (hasReadAccess()) {
        FacesContext.getCurrentInstance().getExternalContext().redirect(pPath);
        return;
    }

    logger.debug("End- " + new Throwable().getStackTrace()[0]);
}

That is the way of my code, if the user sends a ajax request, for example using a rowEditListener:

public void rowEditListener(RowEditEvent ev) {

logger.debug("Start - " + new Throwable().getStackTrace()[0]);

if (hasReadAccess()) {
    FacesContext.getCurrentInstance().getExternalContext().redirect(pPath);
    return;
}

// do something

logger.debug("End - " + new Throwable().getStackTrace()[0]);
}

Thanks!

Nils
  • 314
  • 3
  • 17
  • 1
    See: http://stackoverflow.com/questions/11203195/session-timeout-and-viewexpiredexception-handling-on-jsf-primefaces-ajax-request – zargarf Jun 06 '13 at 09:56
  • 1
    Just a side note. creating an Exception just for the pupose of getting the name of the current method is too expensive, better use `Thread.currentThread().getStackTrace()[1].getMethodName()` – A4L Jun 06 '13 at 10:00
  • Thanks for the hint with the Exception and for the Link. I inspect it. – Nils Jun 06 '13 at 10:13

1 Answers1

-1
you can use spring:
public void logout() {
        try {
            SecurityContextHolder.getContext().setAuthentication(null);
            FacesContext.getCurrentInstance().getExternalContext().invalidateSession();
            FacesContext.getCurrentInstance().getExternalContext()
                    .redirect(FacesContext.getCurrentInstance().getExternalContext().getRequestContextPath() + "/j_spring_security_logout?faces-redirect=true");

        } catch (Exception e) {

        }
    }