从支持 bean 导航到外部 URL?

发布于 2024-09-11 05:45:17 字数 1855 浏览 12 评论 0原文

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

它需要两件事:

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

Siteminder 注销 URL(在策略服务器上配置 -> 我无法更改它)位于我的应用程序之外语境。例如。如果我的 web 应用程序 URL 为 https://localhost:8080/sm/MyWebApp 则注销 URL 为 < a href="https://localhost:8080/anotherwebapp/logout.html" rel="nofollow noreferrer">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;
}

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

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

那么 onclick 似乎不会被调用。

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

任何帮助将不胜感激。

I'm trying to implement proper logout for my Java EE / JSF2 application.

It requires two things:

  1. I need to logout from JAAS and invalidate the session
  2. I then have to navigate to an external URL to fire Siteminder logout

The Siteminder logout URL (configured on the Policy server -> I cannot change it) is outside my applications context. Eg. if my webapp URL is https://localhost:8080/sm/MyWebApp then the logout URL is https://localhost:8080/anotherwebapp/logout.html.

This is the current local logout code:

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

And here is the property that produces the logout 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;
}

However, I cannot find a JSF2 / Primefaces component that can call logout() then open the external URL. For example, if I have:

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

then onclick does not seem to be called.

Another way I tried was putting the external URL to the end of the logout function to have it returned as a navigation string but it is not recognized (also tried with "?faces-redirect=true"...).

Any help would be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我为君王 2024-09-18 05:45:17

您也可以只使用 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");
}

不需要带有元刷新的中间页面。

You can also just use 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");
}

No need for an intermediating page with a meta refresh.

陌若浮生 2024-09-18 05:45:17

您可以创建一个页面 logout.xhtml,因此代码将如下所示:

public String getLogoutUrl() {
    return "/logout.jsf";
}

并在页面中添加:

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

You can create a page logout.xhtml, so the code will look like this:

public String getLogoutUrl() {
    return "/logout.jsf";
}

and in the page add:

<META HTTP-EQUIV="Refresh" CONTENT="0;URL=https://localhost:8080/anotherwebapp/logout.html">
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文