简体   繁体   English

在 Spring 处注销授权服务器不会终止 Keycloak 中的用户 session

[英]Logout at Spring Authorization Server doesn’t terminate user session in Keycloak

I am trying to implement an end-user initiated logout mechanism.我正在尝试实施最终用户启动的logout机制。

I have 4 main entities involved.我有4 个主要实体参与。

  • Client: an Angular app registered as a public OIDC client on Keycloak客户端:Angular上注册为公共OIDC客户端的 Angular 应用程序
  • Keycloak: behaves as an identity broker Keycloak:充当身份代理
  • Spring Authorization Server: identity provider registered on Keycloak Spring 授权服务器:在 Keycloak 上注册的身份提供者
  • Resource Server: a Spring Boot application with a secure REST endpoint资源服务器:具有安全端点RESTSpring Boot应用程序

When the end-user initiates a logout , a call is made by the Angular application to Keycloak's end_session_endpoint .当最终用户发起logout时, Angular应用程序将调用 Keycloak 的end_session_endpoint I have configured the logout URL for my identity provider ( Spring Authorization Server ) in Keycloak as http://localhost:9000/logout which is the default Spring Security logout endpoint.我在 Keycloak 中为我的身份提供者( Spring Authorization Server )配置了logout URL 作为http://localhost:9000/logout这是默认的Spring Security注销端点。

FYI: I haven't enabled BackChannel or FrontChannel logout.仅供参考:我没有启用BackChannelFrontChannel注销。

在此处输入图像描述

In the Network tab of Developer Console , the sequence of calls happen as below:Developer ConsoleNetwork选项卡中,调用顺序如下:

While inspecting the DEBUG logs in the Spring Authorization Server , I am able to see the logout happen for that particular end-user including invalidating the JSESSIONID , however the session doesn't terminate in Keycloak which causes the user to stay logged in and access the secure REST endpoint.在检查Spring Authorization Server中的DEBUG日志时,我能够看到该特定最终用户发生logout ,包括使JSESSIONID无效,但是 session 不会在 Keycloak 中终止,这会导致用户保持登录状态并访问安全REST端点。

Is the Spring Authorization Server expected to return a specific response back to Keycloak to convey that the logout process is complete at it's end and that Keycloak can end the session now? Spring Authorization Server是否期望将特定响应返回给 Keycloak,以传达logout过程在其结束时完成并且 Keycloak 现在可以结束 session?

This is my logic for logout at Spring Authorization Server .这是我在Spring Authorization Server logout的逻辑。

@Bean
@Order(Ordered.LOWEST_PRECEDENCE)
public SecurityFilterChain defaultSecurityFilterChain(HttpSecurity http) throws Exception {

    http.csrf(csrf -> csrf.disable())
                .authorizeHttpRequests(
                        authorize -> authorize.mvcMatchers("/hello/**").permitAll().anyRequest().authenticated())
                .formLogin(form -> form.loginPage("/login").permitAll())
                .addFilterAfter(cookieFilter, ChannelProcessingFilter.class)
                .logout().logoutSuccessUrl("http://localhost:4200");
        
    return http.build();
}

I am totally clueless about what I am missing to make Keycloak end the session at it's end.我完全不知道让 Keycloak 在结束时结束 session 所缺少的东西。 Any leads will be greatly appreciated.任何线索将不胜感激。

Thank you!谢谢!

Update: A post_logout_redirect_uri parameter is sent in the logout request to the Spring Authorization Server .更新:logout请求post_logout_redirect_uri参数发送到Spring Authorization Server I am not able to see a redirection happen to this URI which may be the reason that Keycloak session doesn't terminate.无法看到此 URI 发生重定向,这可能是 Keycloak session 未终止的原因。

post_logout_redirect_uri=http://127.0.0.1:8080/auth/realms/SpringBootKeycloak/broker/oidc/endpoint/logout_response

Got this to work finally!终于让这个工作了!

The logoutSuccessUrl("http://localhost:4200") is incorrect. logoutSuccessUrl("http://localhost:4200")不正确。

As I mentioned in the Update part of my question that Keycloak sends a post_logout_redirect_uri parameter (inspected the logout endpoint in the Network calls in Chrome Developer Console ), the logoutSuccessUrl or the redirectUri should be set to正如我在问题的更新部分提到的那样,Keycloak 发送了一个post_logout_redirect_uri参数(在 Chrome Developer ConsoleNetwork调用中检查了logout端点), logoutSuccessUrlredirectUri应该设置为

http://127.0.0.1:8080/auth/realms/SpringBootKeycloak/broker/oidc/endpoint/logout_response http://127.0.0.1:8080/auth/realms/SpringBootKeycloak/broker/oidc/endpoint/logout_response

Also, note that the above URL takes in state as a request parameter.另外,请注意上面的 URL 将state作为请求参数。 Initially, it gave me a 400 Bad Request as I didn't send the state .最初,它给了我一个400 Bad Request因为我没有发送state

Since I was required to intercept the state parameter when logout is initiated at the Spring Authorization Server , I added a custom logout handler to do the job.由于在Spring Authorization Server启动logout时我需要拦截state参数,因此我添加了一个自定义注销处理程序来完成这项工作。

@Component
public class IdpLogoutHandler implements LogoutHandler {

    private static final String STATE = "state";

    @Override
    public void logout(HttpServletRequest request, HttpServletResponse response, Authentication authentication) {
        String state = request.getParameter(STATE);
        try {
            response.sendRedirect(
                    "http://127.0.0.1:8080/auth/realms/SpringBootKeycloak/broker/oidc/endpoint/logout_response?state="
                            + state);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Updated WebSecurityConfig :更新WebSecurityConfig

http.csrf(csrf -> csrf.disable())
                .authorizeHttpRequests(
                        authorize -> authorize.mvcMatchers("/hello/**").permitAll().anyRequest().authenticated())
                .formLogin(form -> form.loginPage("/login").permitAll())
                .addFilterAfter(cookieFilter, ChannelProcessingFilter.class).logout()
                .addLogoutHandler(customLogoutHandler);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Keycloak Spring Boot 从会话中注销 - Keycloak Spring boot logout from session 密钥斗篷反向通道注销 spring session 使用 redis 作为 Z21D6F40CFB511982E544 - keycloak backchannel logout spring session that use redis as session store Spring启动安全性,Oauth2注销,使会话无效,以便在授权服务器中完成身份验证和批准周期 - Spring boot Security, Oauth2 logout, invalidate session in order to go through the authentication and approval cycle in the authorization server 在 Spring 授权服务器中是否有 OIDC Session 管理和注销机制的实现,用于实现 Single Sing On? - Is there any implementation of OIDC Session Management and Logout mechanism in Spring Authorization Server for implementing Single Sing On? 用户注销时会话无效(春季) - Invalid a session when user makes logout (Spring) 如何将 spring 授权服务器作为 OIDC 身份提供者集成到密钥斗篷中 - How to integrate spring authorization server into keycloak as OIDC an identity proivider Spring oauth2 授权服务器:无法注销用户 - Spring oauth2 authorization server: unable to logout users 使用Keycloak的Spring Boot应用程序,单点登录在Apache Web服务器之后不起作用 - Spring Boot Application using Keycloak, single sign on doesn't work behind an Apache Web Server spring 授权服务器,为用户使用退出模型 - spring authorization server, use exit model for user 静默更新不适用于 spring-authorization-server 和 react 客户端 - silent renew doesn't work on spring-authorization-server and react client
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM