From 898661f1429e6e9639aba9f43b230260579c3d3a Mon Sep 17 00:00:00 2001 From: h_sharifi Date: Wed, 14 Sep 2022 11:02:02 +0430 Subject: [PATCH] #BAEL-5443: add logout config --- .../java/com/baeldung/keycloak/SecurityConfig.java | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/spring-boot-modules/spring-boot-keycloak/src/main/java/com/baeldung/keycloak/SecurityConfig.java b/spring-boot-modules/spring-boot-keycloak/src/main/java/com/baeldung/keycloak/SecurityConfig.java index c3f87627ff..88f829e567 100644 --- a/spring-boot-modules/spring-boot-keycloak/src/main/java/com/baeldung/keycloak/SecurityConfig.java +++ b/spring-boot-modules/spring-boot-keycloak/src/main/java/com/baeldung/keycloak/SecurityConfig.java @@ -13,6 +13,12 @@ import org.springframework.security.web.authentication.session.SessionAuthentica @EnableWebSecurity class SecurityConfig extends WebSecurityConfigurerAdapter { + private final KeycloakLogoutHandler keycloakLogoutHandler; + + SecurityConfig(KeycloakLogoutHandler keycloakLogoutHandler) { + this.keycloakLogoutHandler = keycloakLogoutHandler; + } + @Bean protected SessionAuthenticationStrategy sessionAuthenticationStrategy() { return new RegisterSessionAuthenticationStrategy(new SessionRegistryImpl()); @@ -25,7 +31,11 @@ class SecurityConfig extends WebSecurityConfigurerAdapter { .hasRole("USER") .anyRequest() .permitAll(); - http.oauth2Login(); + http.oauth2Login() + .and() + .logout() + .addLogoutHandler(keycloakLogoutHandler) + .logoutSuccessUrl("/"); } }