注销时的Java JSF重定向

时间:2013-06-06 09:48:01

标签: java jsf-2

我有一个ViewScoped JSF页面。用户正在使用ace:对话框。如果用户没有点击两个小时,他的会话将被tomcat自动销毁。

如果用户在两小时后发送请求,我会重定向到登录页面(因为用户已注销)。问题是我成了一个错误:

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

如果用户退出登录,是否有办法将每个请求重定向到登录页面? 如果会话被破坏,我的Backing Beans会发生什么?

如果用户请求子网站但未登录,那就是重定向的方式:

@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]);
}

这是我的代码的方式,如果用户发送ajax请求,例如使用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]);
}

谢谢!

1 个答案:

答案 0 :(得分:-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) {

        }
    }
相关问题