从支持 bean 导航到外部 URL?
我正在尝试为我的 Java EE / JSF2 应用程序实现正确的注销。
它需要两件事:
- 我需要从 JAAS 注销并使会话无效 然后
- 我必须导航到外部 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:
- I need to logout from JAAS and invalidate the session
- 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您也可以只使用
ExternalContext#redirect()
。不需要带有元刷新的中间页面。
You can also just use
ExternalContext#redirect()
.No need for an intermediating page with a meta refresh.
您可以创建一个页面 logout.xhtml,因此代码将如下所示:
并在页面中添加:
You can create a page logout.xhtml, so the code will look like this:
and in the page add: