package com.example.keycloakpattern; import java.util.LinkedHashMap; import java.util.Map; import org.springframework.security.core.annotation.AuthenticationPrincipal; import org.springframework.security.oauth2.jwt.Jwt; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/api") public class ApiController { @GetMapping("/public") public Map publicEndpoint() { return Map.of("status", "ok", "service", "keycloak-pattern-api"); } @GetMapping("/me") public Map currentUser(@AuthenticationPrincipal Jwt jwt) { Map response = new LinkedHashMap<>(); response.put("subject", jwt.getSubject()); response.put("username", jwt.getClaimAsString("preferred_username")); response.put("issuer", jwt.getIssuer()); response.put("audience", jwt.getAudience()); return response; } }