从支持bean导航到外部URL?

egb*_*kul 4 java jsf siteminder primefaces jsf-2

我正在尝试为我的Java EE/JSF2应用程序实现正确的注销.

它需要两件事:

  1. 我需要从JAAS注销并使会话无效
  2. 然后,我必须导航到外部URL以触发Siteminder注销

Siteminder注销URL(在策略服务器上配置 - >我无法更改它)在我的应用程序上下文之外.例如.如果我的webapp URL是https:// localhost:8080/sm/MyWebApp,那么注销URL是https:// localhost:8080/anotherwebapp/logout.html.

这是当前的本地注销代码:

public void logout() {
    System.out.println("Logging out...");
    HttpServletRequest request = (HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();
    try {
        request.logout();
    } catch (ServletException e) {
        e.printStackTrace();
    }
    HttpSession session = (HttpSession)FacesContext.getCurrentInstance().getExternalContext().getSession(false);
    if (session != null) {
        session.invalidate();
    }
}
Run Code Online (Sandbox Code Playgroud)

以下是生成注销URL的属性:

public String getLogoutUrl() {
    HttpServletRequest request = (HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();
    String requestServer = request.getServerName();
    String requestScheme = request.getScheme();
    int serverPort = request.getServerPort();
    String logoutUrl = requestScheme + "://" + requestServer + ":" + Integer.toString(serverPort) + "/anotherwebapp/logout.html";
    return logoutUrl;
}
Run Code Online (Sandbox Code Playgroud)

但是,我找不到可以调用logout()然后打开外部URL的JSF2/Primefaces组件.例如,如果我有:

<h:outputLink value="#{authBean.logoutUrl}" onclick="#{authBean.logout()}">[Logout]</h:outputLink>
Run Code Online (Sandbox Code Playgroud)

然后onclick似乎没有被调用.

我尝试的另一种方法是将外部URL放在注销函数的末尾,使其作为导航字符串返回,但无法识别(也尝试使用"?faces-redirect = true"...).

任何帮助,将不胜感激.

Bal*_*usC 13

你也可以使用ExternalContext#redirect().

public void logout() throws ServletException, IOException {
    ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext();
    ((HttpServletRequest) ec.getRequest()).logout();
    ec.invalidateSession();
    ec.redirect("http://example.com/anothercontext/logout");
}
Run Code Online (Sandbox Code Playgroud)

无需具有元刷新的中介页面.