Merge pull request #14456 from anuragkumawat/JAVA-18117
JAVA-18117 Update spring-boot-keycloak-adapters module
This commit is contained in:
commit
24396a71c2
|
@ -17,27 +17,12 @@
|
||||||
<relativePath>../../parent-boot-2</relativePath>
|
<relativePath>../../parent-boot-2</relativePath>
|
||||||
</parent>
|
</parent>
|
||||||
|
|
||||||
<dependencyManagement>
|
|
||||||
<dependencies>
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.keycloak.bom</groupId>
|
|
||||||
<artifactId>keycloak-adapter-bom</artifactId>
|
|
||||||
<version>${keycloak-adapter-bom.version}</version>
|
|
||||||
<type>pom</type>
|
|
||||||
<scope>import</scope>
|
|
||||||
</dependency>
|
|
||||||
</dependencies>
|
|
||||||
</dependencyManagement>
|
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-starter</artifactId>
|
<artifactId>spring-boot-starter</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
|
||||||
<groupId>org.keycloak</groupId>
|
|
||||||
<artifactId>keycloak-spring-boot-starter</artifactId>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-starter-data-jpa</artifactId>
|
<artifactId>spring-boot-starter-data-jpa</artifactId>
|
||||||
|
@ -59,6 +44,10 @@
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-starter-web</artifactId>
|
<artifactId>spring-boot-starter-web</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-oauth2-resource-server</artifactId>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.hsqldb</groupId>
|
<groupId>org.hsqldb</groupId>
|
||||||
<artifactId>hsqldb</artifactId>
|
<artifactId>hsqldb</artifactId>
|
||||||
|
@ -84,8 +73,4 @@
|
||||||
</plugins>
|
</plugins>
|
||||||
</build>
|
</build>
|
||||||
|
|
||||||
<properties>
|
|
||||||
<keycloak-adapter-bom.version>15.0.2</keycloak-adapter-bom.version>
|
|
||||||
</properties>
|
|
||||||
|
|
||||||
</project>
|
</project>
|
|
@ -1,13 +1,10 @@
|
||||||
package com.baeldung.keycloak;
|
package com.baeldung.keycloak;
|
||||||
|
|
||||||
import java.security.Principal;
|
|
||||||
import java.util.Map;
|
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.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.stereotype.Controller;
|
||||||
import org.springframework.ui.Model;
|
import org.springframework.ui.Model;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
@ -18,34 +15,27 @@ public class CustomUserAttrController {
|
||||||
@GetMapping(path = "/users")
|
@GetMapping(path = "/users")
|
||||||
public String getUserInfo(Model model) {
|
public String getUserInfo(Model model) {
|
||||||
|
|
||||||
KeycloakAuthenticationToken authentication = (KeycloakAuthenticationToken) SecurityContextHolder.getContext()
|
final DefaultOidcUser user = (DefaultOidcUser) SecurityContextHolder.getContext()
|
||||||
.getAuthentication();
|
.getAuthentication()
|
||||||
|
.getPrincipal();
|
||||||
final Principal principal = (Principal) authentication.getPrincipal();
|
|
||||||
|
|
||||||
String dob = "";
|
String dob = "";
|
||||||
String userIdByToken = "";
|
String userId = "";
|
||||||
String userIdByMapper = "";
|
|
||||||
|
|
||||||
if (principal instanceof KeycloakPrincipal) {
|
OidcIdToken token = user.getIdToken();
|
||||||
|
|
||||||
KeycloakPrincipal<KeycloakSecurityContext> kPrincipal = (KeycloakPrincipal<KeycloakSecurityContext>) principal;
|
Map<String, Object> customClaims = token.getClaims();
|
||||||
IDToken token = kPrincipal.getKeycloakSecurityContext()
|
|
||||||
.getIdToken();
|
|
||||||
|
|
||||||
userIdByToken = token.getSubject();
|
if (customClaims.containsKey("user_id")) {
|
||||||
userIdByMapper = token.getOtherClaims().get("user_id").toString();
|
userId = String.valueOf(customClaims.get("user_id"));
|
||||||
|
|
||||||
Map<String, Object> customClaims = token.getOtherClaims();
|
|
||||||
|
|
||||||
if (customClaims.containsKey("DOB")) {
|
|
||||||
dob = String.valueOf(customClaims.get("DOB"));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
model.addAttribute("username", principal.getName());
|
if (customClaims.containsKey("DOB")) {
|
||||||
model.addAttribute("userIDByToken", userIdByToken);
|
dob = String.valueOf(customClaims.get("DOB"));
|
||||||
model.addAttribute("userIDByMapper", userIdByMapper);
|
}
|
||||||
|
|
||||||
|
model.addAttribute("username", user.getName());
|
||||||
|
model.addAttribute("userID", userId);
|
||||||
model.addAttribute("dob", dob);
|
model.addAttribute("dob", dob);
|
||||||
return "userInfo";
|
return "userInfo";
|
||||||
}
|
}
|
||||||
|
|
|
@ -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();
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -2,8 +2,11 @@ package com.baeldung.keycloak;
|
||||||
|
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
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.builders.HttpSecurity;
|
||||||
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
|
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.core.session.SessionRegistryImpl;
|
||||||
import org.springframework.security.web.SecurityFilterChain;
|
import org.springframework.security.web.SecurityFilterChain;
|
||||||
import org.springframework.security.web.authentication.session.RegisterSessionAuthenticationStrategy;
|
import org.springframework.security.web.authentication.session.RegisterSessionAuthenticationStrategy;
|
||||||
|
@ -36,6 +39,13 @@ class SecurityConfig {
|
||||||
.logout()
|
.logout()
|
||||||
.addLogoutHandler(keycloakLogoutHandler)
|
.addLogoutHandler(keycloakLogoutHandler)
|
||||||
.logoutSuccessUrl("/");
|
.logoutSuccessUrl("/");
|
||||||
|
http.oauth2ResourceServer(OAuth2ResourceServerConfigurer::jwt);
|
||||||
return http.build();
|
return http.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public AuthenticationManager authenticationManager(HttpSecurity http) throws Exception {
|
||||||
|
return http.getSharedObject(AuthenticationManagerBuilder.class)
|
||||||
|
.build();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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
|
|
|
@ -1,15 +1,10 @@
|
||||||
### server port
|
### server port
|
||||||
server.port=8081
|
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.client-id=login-app
|
||||||
spring.security.oauth2.client.registration.keycloak.authorization-grant-type=authorization_code
|
spring.security.oauth2.client.registration.keycloak.authorization-grant-type=authorization_code
|
||||||
spring.security.oauth2.client.registration.keycloak.scope=openid
|
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.issuer-uri=http://localhost:8080/realms/SpringBootKeycloak
|
||||||
spring.security.oauth2.client.provider.keycloak.user-name-attribute=preferred_username
|
spring.security.oauth2.client.provider.keycloak.user-name-attribute=preferred_username
|
||||||
|
|
||||||
|
spring.security.oauth2.resourceserver.jwt.issuer-uri=http://localhost:8080/realms/SpringBootKeycloak
|
|
@ -8,10 +8,7 @@
|
||||||
Hello, <span th:text="${username}">--name--</span>.
|
Hello, <span th:text="${username}">--name--</span>.
|
||||||
</h1>
|
</h1>
|
||||||
<h1>
|
<h1>
|
||||||
User ID By Token: <span th:text="${userIDByToken}">--userID--</span>.
|
User ID : <span th:text="${userID}">--userID--</span>.
|
||||||
</h1>
|
|
||||||
<h1>
|
|
||||||
User ID By Mapper: <span th:text="${userIDByMapper}">--userID--</span>.
|
|
||||||
</h1>
|
</h1>
|
||||||
<h3>
|
<h3>
|
||||||
Your Date of Birth as per our records is <span th:text="${dob}" />.
|
Your Date of Birth as per our records is <span th:text="${dob}" />.
|
||||||
|
|
|
@ -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() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -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());
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
Loading…
Reference in New Issue