BAEL-4441: Custom User Attributes with Keycloak and Spring (#9966)
This commit is contained in:
parent
a82d2bf9e0
commit
473b453a1f
@ -0,0 +1,46 @@
|
|||||||
|
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.stereotype.Controller;
|
||||||
|
import org.springframework.ui.Model;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
|
||||||
|
@Controller
|
||||||
|
public class CustomUserAttrController {
|
||||||
|
|
||||||
|
@GetMapping(path = "/users")
|
||||||
|
public String getUserInfo(Model model) {
|
||||||
|
|
||||||
|
KeycloakAuthenticationToken authentication = (KeycloakAuthenticationToken) SecurityContextHolder.getContext()
|
||||||
|
.getAuthentication();
|
||||||
|
|
||||||
|
final Principal principal = (Principal) authentication.getPrincipal();
|
||||||
|
|
||||||
|
String dob = "";
|
||||||
|
|
||||||
|
if (principal instanceof KeycloakPrincipal) {
|
||||||
|
|
||||||
|
KeycloakPrincipal<KeycloakSecurityContext> kPrincipal = (KeycloakPrincipal<KeycloakSecurityContext>) principal;
|
||||||
|
IDToken token = kPrincipal.getKeycloakSecurityContext()
|
||||||
|
.getIdToken();
|
||||||
|
|
||||||
|
Map<String, Object> customClaims = token.getOtherClaims();
|
||||||
|
|
||||||
|
if (customClaims.containsKey("DOB")) {
|
||||||
|
dob = String.valueOf(customClaims.get("DOB"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
model.addAttribute("username", principal.getName());
|
||||||
|
model.addAttribute("dob", dob);
|
||||||
|
return "userInfo";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -44,7 +44,7 @@ class SecurityConfig extends KeycloakWebSecurityConfigurerAdapter {
|
|||||||
protected void configure(HttpSecurity http) throws Exception {
|
protected void configure(HttpSecurity http) throws Exception {
|
||||||
super.configure(http);
|
super.configure(http);
|
||||||
http.authorizeRequests()
|
http.authorizeRequests()
|
||||||
.antMatchers("/customers*")
|
.antMatchers("/customers*", "/users*")
|
||||||
.hasRole("user")
|
.hasRole("user")
|
||||||
.anyRequest()
|
.anyRequest()
|
||||||
.permitAll();
|
.permitAll();
|
||||||
|
@ -0,0 +1,15 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html xmlns:th="http://www.thymeleaf.org">
|
||||||
|
<head th:include="layout :: headerFragment">
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div id="container">
|
||||||
|
<h1>
|
||||||
|
Hello, <span th:text="${username}">--name--</span>.
|
||||||
|
</h1>
|
||||||
|
<h3>
|
||||||
|
Your Date of Birth as per our records is <span th:text="${dob}" />.
|
||||||
|
</h3>
|
||||||
|
</div>
|
||||||
|
</body>
|
||||||
|
</html>
|
Loading…
x
Reference in New Issue
Block a user