feat: add shared Keycloak compose baseline

This commit is contained in:
donghyeon-ka
2026-07-25 13:14:58 +09:00
parent 1eeff83d0b
commit f47dea3e24
17 changed files with 532 additions and 1 deletions
@@ -0,0 +1,30 @@
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<String, String> publicEndpoint() {
return Map.of("status", "ok", "service", "keycloak-pattern-api");
}
@GetMapping("/me")
public Map<String, Object> currentUser(@AuthenticationPrincipal Jwt jwt) {
Map<String, Object> 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;
}
}
@@ -0,0 +1,12 @@
package com.example.keycloakpattern;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class KeycloakPatternApplication {
public static void main(String[] args) {
SpringApplication.run(KeycloakPatternApplication.class, args);
}
}
@@ -0,0 +1,27 @@
package com.example.keycloakpattern;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.Customizer;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.web.SecurityFilterChain;
@Configuration
public class SecurityConfig {
@Bean
SecurityFilterChain apiSecurity(HttpSecurity http) throws Exception {
return http
.csrf(csrf -> csrf.disable())
.sessionManagement(session ->
session.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
.authorizeHttpRequests(authorize -> authorize
.requestMatchers("/actuator/health", "/actuator/health/**", "/api/public")
.permitAll()
.anyRequest()
.authenticated())
.oauth2ResourceServer(oauth2 -> oauth2.jwt(Customizer.withDefaults()))
.build();
}
}
@@ -0,0 +1,22 @@
server:
port: ${SERVER_PORT:8081}
spring:
application:
name: keycloak-pattern-api
security:
oauth2:
resourceserver:
jwt:
issuer-uri: ${SPRING_SECURITY_OAUTH2_RESOURCESERVER_JWT_ISSUER_URI:http://localhost:8080/realms/keycloak-patterns}
jwk-set-uri: ${SPRING_SECURITY_OAUTH2_RESOURCESERVER_JWT_JWK_SET_URI:http://localhost:8080/realms/keycloak-patterns/protocol/openid-connect/certs}
management:
endpoint:
health:
probes:
enabled: true
endpoints:
web:
exposure:
include: health,info
@@ -0,0 +1,43 @@
package com.example.keycloakpattern;
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.jwt;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.web.servlet.MockMvc;
@SpringBootTest
@AutoConfigureMockMvc
class ApiSecurityTest {
@Autowired
private MockMvc mockMvc;
@Test
void publicEndpointDoesNotRequireAuthentication() throws Exception {
mockMvc.perform(get("/api/public"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.status").value("ok"));
}
@Test
void protectedEndpointRejectsAnonymousRequests() throws Exception {
mockMvc.perform(get("/api/me"))
.andExpect(status().isUnauthorized());
}
@Test
void protectedEndpointAcceptsJwtAuthentication() throws Exception {
mockMvc.perform(get("/api/me").with(jwt().jwt(token -> token
.subject("test-subject")
.claim("preferred_username", "regular-user"))))
.andExpect(status().isOk())
.andExpect(jsonPath("$.subject").value("test-subject"))
.andExpect(jsonPath("$.username").value("regular-user"));
}
}