从后端Bean导航到外部URL?

4
我正在尝试为我的Java EE / JSF2应用程序实现适当的注销。
它需要两件事:
1.我需要从JAAS注销并使会话无效
2.然后,我必须导航到外部URL以触发Siteminder注销。
Siteminder注销URL(在策略服务器上配置 ->我无法更改它)在我的应用程序上下文之外。例如,如果我的Web应用程序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();
    }
}

这里是产生注销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;
}

然而,我找不到一个JSF2 / Primefaces组件可以调用logout()然后打开外部URL。例如,如果我有:

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

然后onclick似乎没有被调用。

我尝试的另一种方法是将外部URL放置到logout函数的末尾,以使其作为导航字符串返回,但它没有被识别(也尝试使用"?faces-redirect=true"…)。

任何帮助都将不胜感激。

2个回答

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

不需要使用 meta refresh 页面进行中介操作。


3
您可以创建一个名为logout.xhtml的页面,代码将如下所示:
public String getLogoutUrl() {
    return "/logout.jsf";
}

在页面中添加:

<META HTTP-EQUIV="Refresh" CONTENT="0;URL=https://localhost:8080/anotherwebapp/logout.html">

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接