我正在尝试为我的 Java EE/JSF2 应用程序实现正确的注销。
它需要两件事:
- 我需要从 JAAS 注销并使 session 无效
- 然后我必须导航到外部 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();
}
}
这是生成注销 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”...)。
如有任何帮助,我们将不胜感激。
最佳答案
您也可以只使用 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");
}
不需要带有元刷新的中间页面。
关于java - 从支持 bean 导航到外部 URL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3297579/