diff --git a/spring-boot-modules/spring-boot-keycloak-adapters/pom.xml b/spring-boot-modules/spring-boot-keycloak-adapters/pom.xml index 0da8d920d1..035c226b6d 100644 --- a/spring-boot-modules/spring-boot-keycloak-adapters/pom.xml +++ b/spring-boot-modules/spring-boot-keycloak-adapters/pom.xml @@ -17,27 +17,12 @@ ../../parent-boot-2 - - - - org.keycloak.bom - keycloak-adapter-bom - ${keycloak-adapter-bom.version} - pom - import - - - - org.springframework.boot spring-boot-starter - - org.keycloak - keycloak-spring-boot-starter - + org.springframework.boot spring-boot-starter-data-jpa @@ -59,6 +44,10 @@ org.springframework.boot spring-boot-starter-web + + org.springframework.boot + spring-boot-starter-oauth2-resource-server + org.hsqldb hsqldb @@ -84,8 +73,4 @@ - - 15.0.2 - - \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-keycloak-adapters/src/main/java/com/baeldung/keycloak/CustomUserAttrController.java b/spring-boot-modules/spring-boot-keycloak-adapters/src/main/java/com/baeldung/keycloak/CustomUserAttrController.java index 5b267ae19e..e6432ce19a 100644 --- a/spring-boot-modules/spring-boot-keycloak-adapters/src/main/java/com/baeldung/keycloak/CustomUserAttrController.java +++ b/spring-boot-modules/spring-boot-keycloak-adapters/src/main/java/com/baeldung/keycloak/CustomUserAttrController.java @@ -1,13 +1,10 @@ package com.baeldung.keycloak; -import java.security.Principal; import java.util.Map; -import org.keycloak.KeycloakPrincipal; -import org.keycloak.KeycloakSecurityContext; -import org.keycloak.adapters.springsecurity.token.KeycloakAuthenticationToken; -import org.keycloak.representations.IDToken; import org.springframework.security.core.context.SecurityContextHolder; +import org.springframework.security.oauth2.core.oidc.OidcIdToken; +import org.springframework.security.oauth2.core.oidc.user.DefaultOidcUser; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; @@ -18,34 +15,27 @@ public class CustomUserAttrController { @GetMapping(path = "/users") public String getUserInfo(Model model) { - KeycloakAuthenticationToken authentication = (KeycloakAuthenticationToken) SecurityContextHolder.getContext() - .getAuthentication(); - - final Principal principal = (Principal) authentication.getPrincipal(); + final DefaultOidcUser user = (DefaultOidcUser) SecurityContextHolder.getContext() + .getAuthentication() + .getPrincipal(); String dob = ""; - String userIdByToken = ""; - String userIdByMapper = ""; + String userId = ""; - if (principal instanceof KeycloakPrincipal) { + OidcIdToken token = user.getIdToken(); - KeycloakPrincipal kPrincipal = (KeycloakPrincipal) principal; - IDToken token = kPrincipal.getKeycloakSecurityContext() - .getIdToken(); + Map customClaims = token.getClaims(); - userIdByToken = token.getSubject(); - userIdByMapper = token.getOtherClaims().get("user_id").toString(); - - Map customClaims = token.getOtherClaims(); - - if (customClaims.containsKey("DOB")) { - dob = String.valueOf(customClaims.get("DOB")); - } + if (customClaims.containsKey("user_id")) { + userId = String.valueOf(customClaims.get("user_id")); } - model.addAttribute("username", principal.getName()); - model.addAttribute("userIDByToken", userIdByToken); - model.addAttribute("userIDByMapper", userIdByMapper); + if (customClaims.containsKey("DOB")) { + dob = String.valueOf(customClaims.get("DOB")); + } + + model.addAttribute("username", user.getName()); + model.addAttribute("userID", userId); model.addAttribute("dob", dob); return "userInfo"; } diff --git a/spring-boot-modules/spring-boot-keycloak-adapters/src/main/java/com/baeldung/keycloak/KeycloakConfig.java b/spring-boot-modules/spring-boot-keycloak-adapters/src/main/java/com/baeldung/keycloak/KeycloakConfig.java deleted file mode 100644 index 6a3dc45717..0000000000 --- a/spring-boot-modules/spring-boot-keycloak-adapters/src/main/java/com/baeldung/keycloak/KeycloakConfig.java +++ /dev/null @@ -1,14 +0,0 @@ -package com.baeldung.keycloak; - -import org.keycloak.adapters.springboot.KeycloakSpringBootConfigResolver; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; - -@Configuration -public class KeycloakConfig { - - @Bean - public KeycloakSpringBootConfigResolver keycloakConfigResolver() { - return new KeycloakSpringBootConfigResolver(); - } -} diff --git a/spring-boot-modules/spring-boot-keycloak-adapters/src/main/java/com/baeldung/keycloak/SecurityConfig.java b/spring-boot-modules/spring-boot-keycloak-adapters/src/main/java/com/baeldung/keycloak/SecurityConfig.java index c39e37cfaa..c85438952a 100644 --- a/spring-boot-modules/spring-boot-keycloak-adapters/src/main/java/com/baeldung/keycloak/SecurityConfig.java +++ b/spring-boot-modules/spring-boot-keycloak-adapters/src/main/java/com/baeldung/keycloak/SecurityConfig.java @@ -2,8 +2,11 @@ package com.baeldung.keycloak; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import org.springframework.security.authentication.AuthenticationManager; +import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; +import org.springframework.security.config.annotation.web.configurers.oauth2.server.resource.OAuth2ResourceServerConfigurer; import org.springframework.security.core.session.SessionRegistryImpl; import org.springframework.security.web.SecurityFilterChain; import org.springframework.security.web.authentication.session.RegisterSessionAuthenticationStrategy; @@ -36,6 +39,13 @@ class SecurityConfig { .logout() .addLogoutHandler(keycloakLogoutHandler) .logoutSuccessUrl("/"); + http.oauth2ResourceServer(OAuth2ResourceServerConfigurer::jwt); return http.build(); } + + @Bean + public AuthenticationManager authenticationManager(HttpSecurity http) throws Exception { + return http.getSharedObject(AuthenticationManagerBuilder.class) + .build(); + } } diff --git a/spring-boot-modules/spring-boot-keycloak-adapters/src/main/resources/application-embedded.properties b/spring-boot-modules/spring-boot-keycloak-adapters/src/main/resources/application-embedded.properties deleted file mode 100644 index 7e1985f0ad..0000000000 --- a/spring-boot-modules/spring-boot-keycloak-adapters/src/main/resources/application-embedded.properties +++ /dev/null @@ -1,9 +0,0 @@ -### server port -server.port=8080 - -#Keycloak Configuration -keycloak.auth-server-url=http://localhost:8083/auth -keycloak.realm=baeldung -keycloak.resource=customerClient -keycloak.public-client=true -keycloak.principal-attribute=preferred_username \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-keycloak-adapters/src/main/resources/application.properties b/spring-boot-modules/spring-boot-keycloak-adapters/src/main/resources/application.properties index 323617e2ef..df2fadabae 100644 --- a/spring-boot-modules/spring-boot-keycloak-adapters/src/main/resources/application.properties +++ b/spring-boot-modules/spring-boot-keycloak-adapters/src/main/resources/application.properties @@ -1,15 +1,10 @@ ### server port server.port=8081 -#Keycloak Configuration -keycloak.auth-server-url=http://localhost:8180/auth -keycloak.realm=SpringBootKeycloak -keycloak.resource=login-app -keycloak.public-client=true -keycloak.principal-attribute=preferred_username - spring.security.oauth2.client.registration.keycloak.client-id=login-app spring.security.oauth2.client.registration.keycloak.authorization-grant-type=authorization_code spring.security.oauth2.client.registration.keycloak.scope=openid -spring.security.oauth2.client.provider.keycloak.issuer-uri=http://localhost:8180/auth/realms/SpringBootKeycloak -spring.security.oauth2.client.provider.keycloak.user-name-attribute=preferred_username \ No newline at end of file +spring.security.oauth2.client.provider.keycloak.issuer-uri=http://localhost:8080/realms/SpringBootKeycloak +spring.security.oauth2.client.provider.keycloak.user-name-attribute=preferred_username + +spring.security.oauth2.resourceserver.jwt.issuer-uri=http://localhost:8080/realms/SpringBootKeycloak \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-keycloak-adapters/src/main/resources/templates/userInfo.html b/spring-boot-modules/spring-boot-keycloak-adapters/src/main/resources/templates/userInfo.html index 7f772398c1..5b615fd914 100644 --- a/spring-boot-modules/spring-boot-keycloak-adapters/src/main/resources/templates/userInfo.html +++ b/spring-boot-modules/spring-boot-keycloak-adapters/src/main/resources/templates/userInfo.html @@ -8,10 +8,7 @@ Hello, --name--.

- User ID By Token: --userID--. -

-

- User ID By Mapper: --userID--. + User ID : --userID--.

Your Date of Birth as per our records is . diff --git a/spring-boot-modules/spring-boot-keycloak-adapters/src/test/java/com/baeldung/keycloak/KeycloakConfigurationIntegrationTest.java b/spring-boot-modules/spring-boot-keycloak-adapters/src/test/java/com/baeldung/keycloak/KeycloakConfigurationIntegrationTest.java new file mode 100644 index 0000000000..c14e006bd9 --- /dev/null +++ b/spring-boot-modules/spring-boot-keycloak-adapters/src/test/java/com/baeldung/keycloak/KeycloakConfigurationIntegrationTest.java @@ -0,0 +1,17 @@ +package com.baeldung.keycloak; + +import org.junit.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit.jupiter.SpringExtension; + +@ExtendWith(SpringExtension.class) +@SpringBootTest(classes = { SpringBoot.class }) +public class KeycloakConfigurationIntegrationTest { + + @Test + public void whenLoadApplication_thenSuccess() { + + } + +} \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-keycloak-adapters/src/test/java/com/baeldung/keycloak/KeycloakConfigurationLiveTest.java b/spring-boot-modules/spring-boot-keycloak-adapters/src/test/java/com/baeldung/keycloak/KeycloakConfigurationLiveTest.java deleted file mode 100644 index 5fc8597252..0000000000 --- a/spring-boot-modules/spring-boot-keycloak-adapters/src/test/java/com/baeldung/keycloak/KeycloakConfigurationLiveTest.java +++ /dev/null @@ -1,51 +0,0 @@ -package com.baeldung.keycloak; - -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.keycloak.KeycloakPrincipal; -import org.keycloak.KeycloakSecurityContext; -import org.keycloak.adapters.springboot.client.KeycloakSecurityContextClientRequestInterceptor; -import org.mockito.Mock; -import org.mockito.MockitoAnnotations; -import org.mockito.Spy; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.mock.web.MockHttpServletRequest; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; -import org.springframework.web.context.request.RequestContextHolder; -import org.springframework.web.context.request.ServletRequestAttributes; - -import static org.junit.Assert.assertNotNull; -import static org.mockito.Mockito.when; - -@RunWith(SpringJUnit4ClassRunner.class) -@SpringBootTest(classes = SpringBoot.class) -//requires running Keycloak server and realm setup as shown in https://www.baeldung.com/spring-boot-keycloak -public class KeycloakConfigurationLiveTest { - - @Spy - private KeycloakSecurityContextClientRequestInterceptor factory; - - private MockHttpServletRequest servletRequest; - - @Mock - public KeycloakSecurityContext keycloakSecurityContext; - - @Mock - private KeycloakPrincipal keycloakPrincipal; - - @Before - public void setUp() { - MockitoAnnotations.initMocks(this); - servletRequest = new MockHttpServletRequest(); - RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(servletRequest)); - servletRequest.setUserPrincipal(keycloakPrincipal); - when(keycloakPrincipal.getKeycloakSecurityContext()).thenReturn(keycloakSecurityContext); - } - - @Test - public void testGetKeycloakSecurityContext() throws Exception { - assertNotNull(keycloakPrincipal.getKeycloakSecurityContext()); - } - -}