Keycloak openid 单点注销与弹簧启动



我正在尝试使用keycloak和openid在我的spring启动应用程序中实现单次注销。

我已经使用冲刺启动 1.5.3 和弹簧安全适配器设置了 keycloak 3.4.3(文档在这里(,使用 tomcat 适配器的算法(文档在这里(。一切正常,直到我尝试退出所有会话。

我试过:

1( 在 keycloak 管理控制台 UI 上:注销所有会话 结果:它确实清除了KeyCloak中的所有会话,但不会清除我的客户端应用程序中的浏览器会话。所以我一直保持登录状态,直到我手动删除它们。

2( 使用HttpServletRequest.logout()http://auth-server/auth/realms/{realm-name}/protocol/openid-connect/logout?redirect_uri=encodedRedirectUri(此处的文档(,仅从当前客户端注销,而不是作为 SLO 注销

我不确定OpenID是否支持SLO,我找不到任何关于它的可靠文档。

¿有没有办法使用 OpenID 和 Spring 引导实现单点注销?

您可以执行以下操作注销

@GetMapping(path = "/logout")
public String logout(HttpServletRequest request) throws ServletException {
request.logout();
return "/";
}

http

<a href="/logout">Logout</a>

根据官方文档HttpServletRequest.logout()应该可以工作,但它不起作用。由于错误或其他一些原因,这种方式使注销仅适用于 Web 容器会话,而不是 keycloak 会话。它需要一些额外的工作。

对于钥匙斗篷弹簧启动器:17.0.1
/**
* Makes SSO Logout.
* This endpoint has to be private. Otherwise there will be no token to send logout to KeyCloak.
*
* @param request the request
* @return redirect to logout page
* @throws ServletException if tomcat session logout throws exception
*/
@GetMapping(path = "/logout")
public String logout(HttpServletRequest request) throws ServletException {
keycloakSessionLogout(request);
tomcatSessionLogout(request);
return "redirect:/public/logout.html";
}
private void keycloakSessionLogout(HttpServletRequest request){
RefreshableKeycloakSecurityContext c = getKeycloakSecurityContext(request);
KeycloakDeployment d = c.getDeployment();
c.logout(d);
}
private void tomcatSessionLogout(HttpServletRequest request) throws ServletException {
request.logout();
}
private RefreshableKeycloakSecurityContext getKeycloakSecurityContext(HttpServletRequest request){
return (RefreshableKeycloakSecurityContext) request.getAttribute(KeycloakSecurityContext.class.getName());
}

相关内容

最新更新