簡體   English   中英

Java JSF在注銷時重定向

[英]Java JSF redirect on logout

我有一個ViewScoped JSF頁面。 用戶正在工作的地方存在一個ace:dialog。如果用戶兩個小時沒有單擊,tomcat將自動破壞其會話。

如果用戶在兩個小時后發送請求,我將重定向到登錄頁面(因為用戶已注銷)。 問題是我變成了錯誤:

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

如果用戶注銷,是否可以將每個請求重定向到登錄頁面? 如果會話被破壞了,我的后備豆怎么了?

如果用戶請求一個子站點並且未登錄,那就是我重定向的方式:

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

謝謝!

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) {

        }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM