From d90ff3b7ccefbbe8119263dd9e92bf4d394ba6e6 Mon Sep 17 00:00:00 2001 From: wengkee Date: Fri, 17 Jan 2025 16:40:01 +0800 Subject: [PATCH 1/2] add testing logout steps in readme removed redirection uri as it is not needed and parameterised the keycloak uri --- README.md | 16 ++++++++++++++++ .../com/edw/config/SecurityConfiguration.java | 10 +++++++++- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 5229add..231ab6f 100644 --- a/README.md +++ b/README.md @@ -8,6 +8,22 @@ A sample java code to demonstrate a Spring Boot 3 integration with Keycloak 17. - Keycloak 17 - Red Hat OpenJDK 17 +## Testing Login and Logout Steps +1. Start Keycloak server locally +1. Create a realm +1. Create a client +1. In client, configure: + + root url: http://localhost:8081 + home url: http://localhost:8081/login + valid redirect url: * + valid post logout redirect URIs: * + web origin: + +1. Goto localhost:8081/login +2. Keyin username/password +3. Goto localhost:8081/logout, follow the steps +4. Expectation is the logout will redirect back to localhost:8081/login + ## Screenshots Keycloak User Profile diff --git a/src/main/java/com/edw/config/SecurityConfiguration.java b/src/main/java/com/edw/config/SecurityConfiguration.java index 877c252..a0ba169 100644 --- a/src/main/java/com/edw/config/SecurityConfiguration.java +++ b/src/main/java/com/edw/config/SecurityConfiguration.java @@ -1,5 +1,7 @@ package com.edw.config; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.web.builders.HttpSecurity; @@ -19,6 +21,12 @@ @EnableWebSecurity public class SecurityConfiguration { + @Value( "${spring.security.oauth2.client.provider.external.issuer-uri}" ) + private String keycloakUri; + + @Value( "${spring.security.oauth2.client.registration.external.client-id}" ) + private String keycloakClientId; + @Bean public SecurityFilterChain configure(HttpSecurity http) throws Exception { http @@ -40,7 +48,7 @@ public SecurityFilterChain configure(HttpSecurity http) throws Exception { .fullyAuthenticated() .and() .logout() - .logoutSuccessUrl("http://localhost:8080/realms/external/protocol/openid-connect/logout?redirect_uri=http://localhost:8081/"); + .logoutSuccessUrl(keycloakUri + "/protocol/openid-connect/logout?client_id=" + keycloakClientId); return http.build(); } From f50a49e0a032f050322733b3f6730d9036d7537e Mon Sep 17 00:00:00 2001 From: wengkee Date: Fri, 17 Jan 2025 16:42:25 +0800 Subject: [PATCH 2/2] fix alignment --- README.md | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 231ab6f..15cfb77 100644 --- a/README.md +++ b/README.md @@ -12,17 +12,17 @@ A sample java code to demonstrate a Spring Boot 3 integration with Keycloak 17. 1. Start Keycloak server locally 1. Create a realm 1. Create a client -1. In client, configure: - - root url: http://localhost:8081 - home url: http://localhost:8081/login - valid redirect url: * - valid post logout redirect URIs: * - web origin: + +1. In client, configure: + - root url: http://localhost:8081 + - home url: http://localhost:8081/login + - valid redirect url: * + - valid post logout redirect URIs: * + - web origin: + + 1. Goto localhost:8081/login -2. Keyin username/password -3. Goto localhost:8081/logout, follow the steps -4. Expectation is the logout will redirect back to localhost:8081/login +1. Keyin username/password +1. Goto localhost:8081/logout, follow the steps +1. Expectation is the logout will redirect back to localhost:8081/login ## Screenshots Keycloak User Profile