260327_01_ENH: 프로젝트 구조 리펙토링 및 README.md 갱신
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
package com.kri.common.auth.apikey;
|
||||
package com.kri.common.autoconfigure.auth.apikey;
|
||||
|
||||
import com.kri.common.endpoint.auth.apikey.ApiKeyAdminController;
|
||||
import org.springframework.boot.autoconfigure.AutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
@@ -8,7 +9,7 @@ import org.springframework.context.annotation.Import;
|
||||
@AutoConfiguration
|
||||
@ConditionalOnClass(ApiKeyAdminController.class)
|
||||
@ConditionalOnProperty(prefix = "kri.auth.apikey", name = "admin-api-enabled", havingValue = "true")
|
||||
@Import(ApiKeyAdminController.class) // ✅ 컴포넌트 스캔 없이도 Bean 등록
|
||||
@Import(ApiKeyAdminController.class) // 컴포넌트 스캔 없이도 Bean 등록
|
||||
public class ApiKeyAdminAutoConfiguration {
|
||||
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.kri.common.auth.apikey;
|
||||
package com.kri.common.autoconfigure.auth.apikey;
|
||||
|
||||
import com.zaxxer.hikari.HikariDataSource;
|
||||
import jakarta.persistence.EntityManagerFactory;
|
||||
@@ -111,4 +111,4 @@ public class ApiKeyJpaAutoConfiguration {
|
||||
// FactoryBean으로 등록하면 내부에서 EntityManagerFactory들을 찾아 Metamodel을 구성함
|
||||
return new JpaMetamodelMappingContextFactoryBean();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,8 @@
|
||||
package com.kri.common.auth.apikey;
|
||||
package com.kri.common.autoconfigure.auth.apikey;
|
||||
|
||||
import com.kri.common.auth.persistence.repository.ApiKeyRepository;
|
||||
import com.kri.common.feature.auth.apikey.ApiKeyUserClaimsProvider;
|
||||
import com.kri.common.runtime.auth.apikey.PreloadedApiKeyUserClaimsProvider;
|
||||
import com.kri.common.runtime.auth.apikey.persistence.repository.ApiKeyRepository;
|
||||
import org.springframework.boot.autoconfigure.AutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
@@ -14,7 +16,7 @@ import org.springframework.context.annotation.Bean;
|
||||
public class ApiKeyPreloadAutoConfiguration {
|
||||
|
||||
@Bean
|
||||
@ConditionalOnBean(ApiKeyRepository.class) // ✅ apikey JPA 컨텍스트가 repo를 만들었을 때만
|
||||
@ConditionalOnBean(ApiKeyRepository.class)
|
||||
@ConditionalOnMissingBean(ApiKeyUserClaimsProvider.class)
|
||||
public PreloadedApiKeyUserClaimsProvider apiKeyUserClaimsProvider(
|
||||
ApiKeyProperties props,
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.kri.common.auth.apikey;
|
||||
package com.kri.common.autoconfigure.auth.apikey;
|
||||
|
||||
import java.util.List;
|
||||
import lombok.Getter;
|
||||
@@ -1,8 +1,9 @@
|
||||
package com.kri.common.auth.jwt;
|
||||
package com.kri.common.autoconfigure.auth.jwt;
|
||||
|
||||
import com.kri.common.auth.apikey.ApiKeyAuthFilter;
|
||||
import com.kri.common.auth.apikey.ApiKeyProperties;
|
||||
import com.kri.common.auth.apikey.ApiKeyUserClaimsProvider;
|
||||
import com.kri.common.runtime.auth.apikey.ApiKeyAuthFilter;
|
||||
import com.kri.common.autoconfigure.auth.apikey.ApiKeyProperties;
|
||||
import com.kri.common.feature.auth.apikey.ApiKeyUserClaimsProvider;
|
||||
import com.kri.common.feature.auth.jwt.JwtService;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
@@ -19,16 +20,14 @@ public class JwtAutoConfiguration {
|
||||
return new JwtService(properties);
|
||||
}
|
||||
|
||||
/**
|
||||
* ApiKeyUserClaimsProvider 구현체가 존재할 때만 API Key 필터 자동 생성. (실제 필터 체인에 넣는 건 각 서비스의 SecurityConfig 에서
|
||||
* addFilterBefore 로 수행)
|
||||
*/
|
||||
@Bean
|
||||
@ConditionalOnBean(ApiKeyUserClaimsProvider.class)
|
||||
@ConditionalOnMissingBean
|
||||
public ApiKeyAuthFilter apiKeyAuthFilter(ApiKeyProperties apiKeyProperties,
|
||||
public ApiKeyAuthFilter apiKeyAuthFilter(
|
||||
ApiKeyProperties apiKeyProperties,
|
||||
ApiKeyUserClaimsProvider provider,
|
||||
JwtService jwtService) {
|
||||
JwtService jwtService
|
||||
) {
|
||||
return new ApiKeyAuthFilter(apiKeyProperties, provider, jwtService);
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.kri.common.auth.jwt;
|
||||
package com.kri.common.autoconfigure.auth.jwt;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
@@ -32,4 +32,4 @@ public class JwtProperties {
|
||||
* (선택) Refresh Token 유효 시간(초 단위)
|
||||
*/
|
||||
private long refreshTokenValiditySeconds = 7 * 24 * 3600; // 기본 7일
|
||||
}
|
||||
}
|
||||
@@ -1,10 +1,11 @@
|
||||
package com.kri.common.redis;
|
||||
package com.kri.common.autoconfigure.redis;
|
||||
|
||||
import com.kri.common.feature.redis.RedisQueueHelper;
|
||||
import org.springframework.boot.autoconfigure.AutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.data.redis.connection.RedisConnectionFactory;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
@@ -12,7 +13,7 @@ import org.springframework.data.redis.core.StringRedisTemplate;
|
||||
|
||||
@AutoConfiguration
|
||||
@ConditionalOnClass(RedisTemplate.class)
|
||||
@ConditionalOnProperty(prefix = "common.redis", name = "enabled", havingValue = "true", matchIfMissing = false)
|
||||
@ConditionalOnExpression("${kri.redis.enabled:false}")
|
||||
@ConditionalOnBean(RedisConnectionFactory.class)
|
||||
public class CommonRedisAutoConfiguration {
|
||||
|
||||
@@ -28,4 +29,4 @@ public class CommonRedisAutoConfiguration {
|
||||
public RedisQueueHelper redisQueueHelper(StringRedisTemplate rt) {
|
||||
return new RedisQueueHelper(rt);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,12 +1,12 @@
|
||||
package com.kri.common.scheduling;
|
||||
package com.kri.common.autoconfigure.scheduling;
|
||||
|
||||
import org.springframework.boot.autoconfigure.AutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
|
||||
import org.springframework.scheduling.annotation.EnableScheduling;
|
||||
|
||||
@AutoConfiguration
|
||||
@EnableScheduling
|
||||
@ConditionalOnProperty(prefix = "common.scheduling", name = "enabled", havingValue = "true", matchIfMissing = false)
|
||||
@ConditionalOnExpression("${kri.scheduling.enabled:false}")
|
||||
public class CommonSchedulingAutoConfiguration {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,9 @@
|
||||
package com.kri.common.security;
|
||||
package com.kri.common.autoconfigure.security;
|
||||
|
||||
import com.kri.common.auth.apikey.ApiKeyAuthFilter;
|
||||
import com.kri.common.auth.jwt.JwtProperties;
|
||||
import com.kri.common.auth.jwt.JwtService;
|
||||
import com.kri.common.runtime.auth.apikey.ApiKeyAuthFilter;
|
||||
import com.kri.common.autoconfigure.auth.jwt.JwtProperties;
|
||||
import com.kri.common.feature.auth.jwt.JwtService;
|
||||
import com.kri.common.runtime.security.JwtAuthenticationFilter;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.beans.factory.ObjectProvider;
|
||||
import org.springframework.boot.autoconfigure.AutoConfiguration;
|
||||
@@ -83,4 +84,4 @@ public class KriSecurityAutoConfiguration {
|
||||
|
||||
return http.build();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.kri.common.security;
|
||||
package com.kri.common.autoconfigure.security;
|
||||
|
||||
import java.util.List;
|
||||
import lombok.Getter;
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.kri.common.swagger;
|
||||
package com.kri.common.autoconfigure.swagger;
|
||||
|
||||
import com.kri.common.auth.apikey.ApiKeyProperties;
|
||||
import com.kri.common.autoconfigure.auth.apikey.ApiKeyProperties;
|
||||
import com.kri.common.runtime.swagger.SwaggerIpFilter;
|
||||
import io.swagger.v3.oas.models.Components;
|
||||
import io.swagger.v3.oas.models.OpenAPI;
|
||||
import io.swagger.v3.oas.models.info.Info;
|
||||
@@ -24,13 +25,12 @@ import org.springframework.context.annotation.Bean;
|
||||
public class KriSwaggerAutoConfiguration {
|
||||
|
||||
private final KriSwaggerProperties properties;
|
||||
private final ApiKeyProperties apiKeyProperties; // apikey.header-name 읽어오기 위해
|
||||
private final ApiKeyProperties apiKeyProperties; // apikey.header-name 읽기용
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public OpenAPI kriOpenAPI() {
|
||||
|
||||
// 🔐 JWT Bearer + API-Key 스키마 정의
|
||||
// JWT Bearer + API Key 스키마 정의
|
||||
Components components = new Components()
|
||||
.addSecuritySchemes("BearerAuth",
|
||||
new SecurityScheme()
|
||||
@@ -41,9 +41,9 @@ public class KriSwaggerAutoConfiguration {
|
||||
new SecurityScheme()
|
||||
.type(SecurityScheme.Type.APIKEY)
|
||||
.in(SecurityScheme.In.HEADER)
|
||||
.name(apiKeyProperties.getHeaderName())); // 예: X-API-Key
|
||||
.name(apiKeyProperties.getHeaderName())); // 예: X-API-Key
|
||||
|
||||
// 🔐 기본적으로 두 보안 스키마를 전역에 적용
|
||||
// 두 보안 스키마를 기본 보안 요구사항으로 등록
|
||||
SecurityRequirement securityRequirement = new SecurityRequirement()
|
||||
.addList("BearerAuth")
|
||||
.addList("ApiKeyAuth");
|
||||
@@ -54,8 +54,7 @@ public class KriSwaggerAutoConfiguration {
|
||||
.info(new Info()
|
||||
.title(properties.getTitle())
|
||||
.description(properties.getDescription())
|
||||
.version(properties.getVersion())
|
||||
);
|
||||
.version(properties.getVersion()));
|
||||
}
|
||||
|
||||
@Bean(name = "kriGroupedOpenApi")
|
||||
@@ -67,10 +66,7 @@ public class KriSwaggerAutoConfiguration {
|
||||
|
||||
String base = properties.getBasePackage();
|
||||
if (base != null && !base.isBlank()) {
|
||||
builder.packagesToScan(
|
||||
base,
|
||||
"com.kri.common.auth.apikey"
|
||||
);
|
||||
builder.packagesToScan(base, "com.kri.common.feature.auth.apikey");
|
||||
}
|
||||
|
||||
return builder.build();
|
||||
@@ -86,4 +82,4 @@ public class KriSwaggerAutoConfiguration {
|
||||
registrationBean.setOrder(1);
|
||||
return registrationBean;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.kri.common.swagger;
|
||||
package com.kri.common.autoconfigure.swagger;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
@@ -19,12 +19,12 @@ public class KriSwaggerProperties {
|
||||
private String version = "v1";
|
||||
|
||||
/**
|
||||
* 문서화할 base package (null 이면 전체)
|
||||
* 臾몄꽌?뷀븷 base package (null ?대㈃ ?꾩껜)
|
||||
*/
|
||||
private String basePackage;
|
||||
|
||||
/**
|
||||
* Swagger 접근 허용 IP 목록 (127.0.0.1, localhost, 0:0:0:0:0:0:0:1은 기본 허용)
|
||||
* Swagger ?묎렐 ?덉슜 IP 紐⑸줉 (127.0.0.1, localhost, 0:0:0:0:0:0:0:1? 湲곕낯 ?덉슜)
|
||||
*/
|
||||
private List<String> allowedIps = new ArrayList<>();
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package com.kri.common.autoconfigure.web;
|
||||
|
||||
import com.kri.common.runtime.web.ErrorAdvice;
|
||||
import com.kri.common.runtime.web.MdcHandlerConfig;
|
||||
import com.kri.common.runtime.web.ServiceMdcAspect;
|
||||
import com.kri.common.runtime.web.TraceFilter;
|
||||
import org.aspectj.lang.annotation.Aspect;
|
||||
import org.springframework.boot.autoconfigure.AutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
|
||||
import org.springframework.boot.web.servlet.FilterRegistrationBean;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.core.Ordered;
|
||||
|
||||
@AutoConfiguration
|
||||
@ConditionalOnExpression("${kri.web.enabled:true}")
|
||||
@Import(MdcHandlerConfig.class)
|
||||
public class CommonWebAutoConfiguration {
|
||||
|
||||
@Bean
|
||||
public ErrorAdvice errorAdvice() {
|
||||
return new ErrorAdvice();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public FilterRegistrationBean<TraceFilter> traceFilter() {
|
||||
var reg = new FilterRegistrationBean<>(new TraceFilter());
|
||||
reg.setOrder(Ordered.HIGHEST_PRECEDENCE);
|
||||
return reg;
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnClass(Aspect.class)
|
||||
@ConditionalOnExpression("${kri.web.mdc.enabled:true}")
|
||||
public ServiceMdcAspect serviceMdcAspect() {
|
||||
return new ServiceMdcAspect();
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.kri.common.webmvc;
|
||||
package com.kri.common.autoconfigure.webmvc;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
@@ -1,6 +1,9 @@
|
||||
package com.kri.common.webmvc;
|
||||
package com.kri.common.autoconfigure.webmvc;
|
||||
|
||||
import com.kri.common.auth.jwt.JwtService;
|
||||
import com.kri.common.feature.auth.jwt.JwtService;
|
||||
import com.kri.common.runtime.webmvc.IpAccessControlInterceptor;
|
||||
import com.kri.common.runtime.webmvc.IpAccessControlService;
|
||||
import com.kri.common.runtime.webmvc.RequestUserContextArgumentResolver;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.beans.factory.ObjectProvider;
|
||||
import org.springframework.boot.autoconfigure.AutoConfiguration;
|
||||
@@ -15,7 +18,7 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
|
||||
@AutoConfiguration
|
||||
@EnableConfigurationProperties(KriIpAccessProperties.class)
|
||||
@ConditionalOnProperty(name = "common.webmvc.enabled", matchIfMissing = true)
|
||||
@ConditionalOnProperty(name = "kri.webmvc.enabled", matchIfMissing = true)
|
||||
@RequiredArgsConstructor
|
||||
public class KriWebMvcAutoConfiguration {
|
||||
|
||||
@@ -81,8 +84,8 @@ public class KriWebMvcAutoConfiguration {
|
||||
|
||||
@Override
|
||||
public void addFormatters(org.springframework.format.FormatterRegistry registry) {
|
||||
registry.addConverterFactory(new com.kri.common.webmvc.CodeEnumConverter());
|
||||
registry.addConverterFactory(new com.kri.common.runtime.webmvc.CodeEnumConverter());
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,8 @@
|
||||
package com.kri.common.auth.apikey;
|
||||
package com.kri.common.endpoint.auth.apikey;
|
||||
|
||||
import com.kri.common.auth.dto.JwtUserClaimsDto;
|
||||
import com.kri.common.autoconfigure.auth.apikey.ApiKeyProperties;
|
||||
import com.kri.common.feature.auth.dto.JwtUserClaimsDto;
|
||||
import com.kri.common.runtime.auth.apikey.PreloadedApiKeyUserClaimsProvider;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -36,7 +38,7 @@ public class ApiKeyAdminController {
|
||||
}
|
||||
|
||||
/**
|
||||
* 현재 캐시 상태(키는 마스킹)
|
||||
* 현재 메모리에 적재된 API Key 캐시를 확인합니다.
|
||||
*/
|
||||
@GetMapping("/cache")
|
||||
public List<CachedKeyView> cache(HttpServletRequest req) {
|
||||
@@ -1,6 +1,6 @@
|
||||
package com.kri.common.auth.apikey;
|
||||
package com.kri.common.feature.auth.apikey;
|
||||
|
||||
import com.kri.common.auth.dto.JwtUserClaimsDto;
|
||||
import com.kri.common.feature.auth.dto.JwtUserClaimsDto;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.kri.common.auth.dto;
|
||||
package com.kri.common.feature.auth.dto;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.Builder;
|
||||
@@ -26,4 +26,4 @@ public class JwtUserClaimsDto {
|
||||
|
||||
@Schema(description = "권한 코드")
|
||||
Integer userRole;
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.kri.common.auth.jwt;
|
||||
package com.kri.common.feature.auth.jwt;
|
||||
|
||||
import com.kri.common.auth.dto.JwtUserClaimsDto;
|
||||
import com.kri.common.autoconfigure.auth.jwt.JwtProperties;
|
||||
import com.kri.common.feature.auth.dto.JwtUserClaimsDto;
|
||||
import io.jsonwebtoken.Claims;
|
||||
import io.jsonwebtoken.Jws;
|
||||
import io.jsonwebtoken.JwtException;
|
||||
@@ -44,10 +45,6 @@ public class JwtService {
|
||||
this.key = Keys.hmacShaKeyFor(secret.getBytes(StandardCharsets.UTF_8));
|
||||
}
|
||||
|
||||
// ======================================================
|
||||
// 내부 공통 빌더
|
||||
// ======================================================
|
||||
|
||||
private String buildToken(Map<String, Object> claims, String subject, long validitySeconds) {
|
||||
Date now = new Date();
|
||||
Date expiry = new Date(now.getTime() + validitySeconds * 1000);
|
||||
@@ -64,7 +61,6 @@ public class JwtService {
|
||||
|
||||
private Map<String, Object> buildClaims(JwtUserClaimsDto dto) {
|
||||
Map<String, Object> claims = new HashMap<>();
|
||||
// 필요한 항목만 골라서 claim에 넣어줌
|
||||
claims.put("userId", dto.getUserId());
|
||||
claims.put("userCmpn", dto.getUserCmpn());
|
||||
claims.put("userTeam", dto.getUserTeam());
|
||||
@@ -73,41 +69,15 @@ public class JwtService {
|
||||
return claims;
|
||||
}
|
||||
|
||||
// ======================================================
|
||||
// 토큰 생성 (DTO 기반)
|
||||
// ======================================================
|
||||
|
||||
/**
|
||||
* JwtUserClaimsDto 기반 Access Token 생성 - subject = userId - claims: userId, userCmpn, userTeam,
|
||||
* userName, role
|
||||
*/
|
||||
public String generateAccessToken(JwtUserClaimsDto userClaims) {
|
||||
Map<String, Object> claims = buildClaims(userClaims);
|
||||
return buildToken(
|
||||
claims,
|
||||
userClaims.getUserId(),
|
||||
properties.getAccessTokenValiditySeconds()
|
||||
);
|
||||
return buildToken(claims, userClaims.getUserId(), properties.getAccessTokenValiditySeconds());
|
||||
}
|
||||
|
||||
/**
|
||||
* Refresh Token은 보통 userId만 넣고 심플하게 사용
|
||||
*/
|
||||
public String generateRefreshToken(String userId) {
|
||||
return buildToken(
|
||||
Collections.emptyMap(),
|
||||
userId,
|
||||
properties.getRefreshTokenValiditySeconds()
|
||||
);
|
||||
return buildToken(Collections.emptyMap(), userId, properties.getRefreshTokenValiditySeconds());
|
||||
}
|
||||
|
||||
// ======================================================
|
||||
// 토큰 파싱
|
||||
// ======================================================
|
||||
|
||||
/**
|
||||
* 토큰 파싱 + 서명/만료 검증
|
||||
*/
|
||||
public Jws<Claims> parseToken(String token) throws JwtException {
|
||||
return Jwts.parserBuilder()
|
||||
.requireIssuer(properties.getIssuer())
|
||||
@@ -116,18 +86,11 @@ public class JwtService {
|
||||
.parseClaimsJws(token);
|
||||
}
|
||||
|
||||
/**
|
||||
* 토큰에서 Claims만 추출 (검증 포함)
|
||||
*/
|
||||
public Claims getClaims(String token) throws JwtException {
|
||||
return parseToken(token).getBody();
|
||||
}
|
||||
|
||||
/**
|
||||
* Claims → JwtUserClaimsDto 변환
|
||||
*/
|
||||
public JwtUserClaimsDto toUserClaimsDto(Claims claims) {
|
||||
// userId는 subject와 별도 claim 둘 다에서 시도
|
||||
String userId = claims.get("userId", String.class);
|
||||
if (userId == null) {
|
||||
userId = claims.getSubject();
|
||||
@@ -136,8 +99,6 @@ public class JwtService {
|
||||
String userCmpn = claims.get("userCmpn", String.class);
|
||||
String userTeam = claims.get("userTeam", String.class);
|
||||
String userName = claims.get("userName", String.class);
|
||||
|
||||
// 숫자는 Integer / Long 등으로 들어올 수 있으니 Number로 받았다가 intValue()
|
||||
Number roleNumber = claims.get("role", Number.class);
|
||||
Integer userRole = roleNumber != null ? roleNumber.intValue() : null;
|
||||
|
||||
@@ -150,43 +111,26 @@ public class JwtService {
|
||||
.build();
|
||||
}
|
||||
|
||||
/**
|
||||
* 토큰 → JwtUserClaimsDto 바로 추출
|
||||
*/
|
||||
public JwtUserClaimsDto toUserClaimsDto(String token) throws JwtException {
|
||||
Claims claims = getClaims(token);
|
||||
return toUserClaimsDto(claims);
|
||||
}
|
||||
|
||||
// ======================================================
|
||||
// Authentication 변환 (Spring Security 연동용)
|
||||
// ======================================================
|
||||
|
||||
/**
|
||||
* DTO → Spring Security Authentication 변환 여기서는 간단하게 "ROLE_{role값}" 형태로 Authority를 구성. ex) role =
|
||||
* 1 → "ROLE_1" 실제 프로젝트에서 0/1/2 같은 값에 대한 의미 매핑이 명확하다면, 이 메서드 안의 매핑 로직만 교체해서 쓰면 됨.
|
||||
*/
|
||||
public Authentication toAuthentication(JwtUserClaimsDto dto) {
|
||||
String principal = dto.getUserId();
|
||||
|
||||
List<GrantedAuthority> authorities = new ArrayList<>();
|
||||
if (dto.getUserRole() != null) {
|
||||
String authority = "ROLE_" + dto.getUserRole();
|
||||
authorities.add(new SimpleGrantedAuthority(authority));
|
||||
authorities.add(new SimpleGrantedAuthority("ROLE_" + dto.getUserRole()));
|
||||
}
|
||||
|
||||
UsernamePasswordAuthenticationToken auth =
|
||||
new UsernamePasswordAuthenticationToken(principal, null, authorities);
|
||||
|
||||
auth.setDetails(dto); // 핵심: claims를 details에 보관
|
||||
auth.setDetails(dto);
|
||||
return auth;
|
||||
}
|
||||
|
||||
/**
|
||||
* 토큰 → Authentication 변환 (JWT 파싱 + DTO 변환까지 한 번에)
|
||||
*/
|
||||
public Authentication toAuthentication(String token) throws JwtException {
|
||||
JwtUserClaimsDto dto = toUserClaimsDto(token);
|
||||
return toAuthentication(dto);
|
||||
return toAuthentication(toUserClaimsDto(token));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.kri.common.cache;
|
||||
package com.kri.common.feature.cache;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
@@ -81,7 +81,7 @@ public abstract class AbstractListCache<T> {
|
||||
lock.writeLock().lock();
|
||||
try {
|
||||
ArrayList<T> next = new ArrayList<>(this.list);
|
||||
int i = Math.max(0, Math.min(index, next.size())); // 0 ~ size로 보정
|
||||
int i = Math.max(0, Math.min(index, next.size())); // 0 ~ size濡?蹂댁젙
|
||||
next.addAll(i, new ArrayList<>(items));
|
||||
this.list = List.copyOf(next);
|
||||
} finally {
|
||||
@@ -105,7 +105,7 @@ public abstract class AbstractListCache<T> {
|
||||
|
||||
try {
|
||||
ArrayList<T> next = new ArrayList<>(this.list);
|
||||
int i = Math.max(0, Math.min(index, next.size())); // 0 ~ size로 보정
|
||||
int i = Math.max(0, Math.min(index, next.size())); // 0 ~ size濡?蹂댁젙
|
||||
next.add(i, item);
|
||||
this.list = List.copyOf(next);
|
||||
} finally {
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.kri.common.cache;
|
||||
package com.kri.common.feature.cache;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
@@ -116,4 +116,3 @@ public abstract class AbstractMapCache<K, V> {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.kri.common.datetime;
|
||||
package com.kri.common.feature.datetime;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.time.LocalDate;
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.kri.common.datetime;
|
||||
package com.kri.common.feature.datetime;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JacksonAnnotationsInside;
|
||||
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.kri.common.datetime;
|
||||
package com.kri.common.feature.datetime;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonParser;
|
||||
import com.fasterxml.jackson.databind.BeanProperty;
|
||||
@@ -90,4 +90,4 @@ public class LenientLocalDateTimeDeserializer extends JsonDeserializer<LocalDate
|
||||
|
||||
return (ann == null) ? this : new LenientLocalDateTimeDeserializer(ann.patterns());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.kri.common.enums;
|
||||
package com.kri.common.feature.enums;
|
||||
|
||||
public interface CodeEnum<T> {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.kri.common.enums;
|
||||
package com.kri.common.feature.enums;
|
||||
|
||||
public final class CodeEnumUtils {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.kri.common.excel;
|
||||
package com.kri.common.feature.excel;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.kri.common.excel;
|
||||
package com.kri.common.feature.excel;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
@@ -1455,4 +1455,4 @@ public final class ExcelMapper {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.kri.common.excel;
|
||||
package com.kri.common.feature.excel;
|
||||
|
||||
import org.apache.poi.ss.usermodel.Cell;
|
||||
import org.apache.poi.ss.usermodel.DateUtil;
|
||||
@@ -32,4 +32,4 @@ public final class ExcelUtil {
|
||||
default -> null; // ERROR, _NONE 등 나머지 타입
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.kri.common.file;
|
||||
package com.kri.common.feature.file;
|
||||
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
@@ -56,4 +56,4 @@ public final class FileUtil {
|
||||
}
|
||||
return s;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.kri.common.http;
|
||||
package com.kri.common.feature.http;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
@@ -99,4 +99,4 @@ public class ExternalApiClient {
|
||||
throw new RuntimeException("External API 호출 중 오류가 발생했습니다: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.kri.common.http;
|
||||
package com.kri.common.feature.http;
|
||||
|
||||
import java.net.http.HttpHeaders;
|
||||
import java.util.List;
|
||||
@@ -34,4 +34,4 @@ public class ExternalApiResponse {
|
||||
public String getBody() {
|
||||
return body;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
package com.kri.common.jpa;
|
||||
package com.kri.common.feature.jpa;
|
||||
|
||||
import com.kri.common.enums.CodeEnum;
|
||||
import com.kri.common.enums.CodeEnumUtils;
|
||||
import com.kri.common.feature.enums.CodeEnum;
|
||||
import com.kri.common.feature.enums.CodeEnumUtils;
|
||||
import jakarta.persistence.AttributeConverter;
|
||||
|
||||
public abstract class AbstractCodeEnumConverter<
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.kri.common.jpa;
|
||||
package com.kri.common.feature.jpa;
|
||||
|
||||
import jakarta.persistence.MappedSuperclass;
|
||||
import jakarta.persistence.PrePersist;
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.kri.common.json;
|
||||
package com.kri.common.feature.json;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.kri.common.redis;
|
||||
package com.kri.common.feature.redis;
|
||||
|
||||
import java.time.Duration;
|
||||
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.kri.common.util;
|
||||
package com.kri.common.feature.util;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.kri.common.util;
|
||||
package com.kri.common.feature.util;
|
||||
|
||||
public final class NumericUtil {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.kri.common.util;
|
||||
package com.kri.common.feature.util;
|
||||
|
||||
public final class StringUtil {
|
||||
|
||||
@@ -19,4 +19,4 @@ public final class StringUtil {
|
||||
public static boolean hasText(String s) {
|
||||
return s != null && !s.isBlank();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.kri.common.web;
|
||||
package com.kri.common.feature.web;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.kri.common.webmvc;
|
||||
package com.kri.common.feature.web;
|
||||
|
||||
import lombok.Builder;
|
||||
import lombok.Value;
|
||||
@@ -13,7 +13,7 @@ public class RequestUserContext {
|
||||
String userName;
|
||||
Integer userRole;
|
||||
|
||||
String clientIp; // IpUtil 에서 가져온 값
|
||||
String clientIp; // IpUtil ?먯꽌 媛?몄삩 媛?
|
||||
|
||||
public boolean isAuthenticated() {
|
||||
return userId != null;
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.kri.common.webmvc.page;
|
||||
package com.kri.common.feature.web.page;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.Getter;
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.kri.common.webmvc.page;
|
||||
package com.kri.common.feature.web.page;
|
||||
|
||||
import java.util.List;
|
||||
import lombok.Builder;
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.kri.common.webmvc.page;
|
||||
package com.kri.common.feature.web.page;
|
||||
|
||||
|
||||
import java.beans.Introspector;
|
||||
@@ -1,6 +1,8 @@
|
||||
package com.kri.common.auth.apikey;
|
||||
package com.kri.common.runtime.auth.apikey;
|
||||
|
||||
import com.kri.common.auth.jwt.JwtService;
|
||||
import com.kri.common.autoconfigure.auth.apikey.ApiKeyProperties;
|
||||
import com.kri.common.feature.auth.apikey.ApiKeyUserClaimsProvider;
|
||||
import com.kri.common.feature.auth.jwt.JwtService;
|
||||
import jakarta.servlet.FilterChain;
|
||||
import jakarta.servlet.ServletException;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
@@ -12,10 +14,6 @@ import org.springframework.security.core.context.SecurityContextHolder;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.web.filter.OncePerRequestFilter;
|
||||
|
||||
/**
|
||||
* X-API-Key (또는 설정된 헤더) 기반 API Key 인증 필터. - API Key를 검증하는 로직은 ApiKeyUserClaimsProvider 구현체에 위임. -
|
||||
* 유효하면 JwtUserClaimsDto → Authentication 으로 변환해서 SecurityContext에 세팅.
|
||||
*/
|
||||
@RequiredArgsConstructor
|
||||
public class ApiKeyAuthFilter extends OncePerRequestFilter {
|
||||
|
||||
@@ -24,26 +22,22 @@ public class ApiKeyAuthFilter extends OncePerRequestFilter {
|
||||
private final JwtService jwtService;
|
||||
|
||||
@Override
|
||||
protected void doFilterInternal(HttpServletRequest request,
|
||||
protected void doFilterInternal(
|
||||
HttpServletRequest request,
|
||||
HttpServletResponse response,
|
||||
FilterChain filterChain)
|
||||
throws ServletException, IOException {
|
||||
|
||||
// API Key 기능이 꺼져 있으면 아무 것도 안 함
|
||||
FilterChain filterChain
|
||||
) throws ServletException, IOException {
|
||||
if (!apiKeyProperties.isEnabled()) {
|
||||
filterChain.doFilter(request, response);
|
||||
return;
|
||||
}
|
||||
|
||||
// 이미 다른 방식(JWT 등)으로 인증되어 있으면 패스
|
||||
if (SecurityContextHolder.getContext().getAuthentication() != null) {
|
||||
filterChain.doFilter(request, response);
|
||||
return;
|
||||
}
|
||||
|
||||
String headerName = apiKeyProperties.getHeaderName();
|
||||
String apiKey = request.getHeader(headerName);
|
||||
|
||||
String apiKey = request.getHeader(apiKeyProperties.getHeaderName());
|
||||
if (StringUtils.hasText(apiKey)) {
|
||||
apiKeyUserClaimsProvider.findUserByApiKey(apiKey).ifPresent(dto -> {
|
||||
Authentication auth = jwtService.toAuthentication(dto);
|
||||
@@ -53,4 +47,4 @@ public class ApiKeyAuthFilter extends OncePerRequestFilter {
|
||||
|
||||
filterChain.doFilter(request, response);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,10 @@
|
||||
package com.kri.common.auth.apikey;
|
||||
package com.kri.common.runtime.auth.apikey;
|
||||
|
||||
import com.kri.common.auth.dto.JwtUserClaimsDto;
|
||||
import com.kri.common.auth.persistence.entity.TApiKeyEntity;
|
||||
import com.kri.common.auth.persistence.repository.ApiKeyRepository;
|
||||
import com.kri.common.autoconfigure.auth.apikey.ApiKeyProperties;
|
||||
import com.kri.common.feature.auth.apikey.ApiKeyUserClaimsProvider;
|
||||
import com.kri.common.feature.auth.dto.JwtUserClaimsDto;
|
||||
import com.kri.common.runtime.auth.apikey.persistence.entity.TApiKeyEntity;
|
||||
import com.kri.common.runtime.auth.apikey.persistence.repository.ApiKeyRepository;
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
@@ -12,16 +14,10 @@ import org.springframework.boot.ApplicationArguments;
|
||||
import org.springframework.boot.ApplicationRunner;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
|
||||
public class PreloadedApiKeyUserClaimsProvider implements ApiKeyUserClaimsProvider,
|
||||
ApplicationRunner {
|
||||
public class PreloadedApiKeyUserClaimsProvider implements ApiKeyUserClaimsProvider, ApplicationRunner {
|
||||
|
||||
private final ApiKeyProperties props;
|
||||
private final ApiKeyRepository repo;
|
||||
|
||||
/**
|
||||
* 캐시를 통째로 교체(swap)하기 위한 AtomicReference
|
||||
*/
|
||||
private final AtomicReference<Map<String, JwtUserClaimsDto>> cacheRef =
|
||||
new AtomicReference<>(Collections.emptyMap());
|
||||
|
||||
@@ -30,9 +26,6 @@ public class PreloadedApiKeyUserClaimsProvider implements ApiKeyUserClaimsProvid
|
||||
this.repo = repo;
|
||||
}
|
||||
|
||||
/**
|
||||
* 런타임에는 DB 조회 금지: 캐시만 조회
|
||||
*/
|
||||
@Override
|
||||
public Optional<JwtUserClaimsDto> findUserByApiKey(String apiKey) {
|
||||
if (!StringUtils.hasText(apiKey)) {
|
||||
@@ -41,40 +34,29 @@ public class PreloadedApiKeyUserClaimsProvider implements ApiKeyUserClaimsProvid
|
||||
return Optional.ofNullable(cacheRef.get().get(apiKey));
|
||||
}
|
||||
|
||||
/**
|
||||
* 기동 시 preload
|
||||
*/
|
||||
@Override
|
||||
public void run(ApplicationArguments args) {
|
||||
if (!props.isEnabled()) {
|
||||
return;
|
||||
}
|
||||
if (!props.isPreloadOnStartup()) {
|
||||
if (!props.isEnabled() || !props.isPreloadOnStartup()) {
|
||||
return;
|
||||
}
|
||||
reloadFromDb();
|
||||
}
|
||||
|
||||
/**
|
||||
* 운영용: DB에서 다시 읽어 캐시 전체 교체
|
||||
*/
|
||||
public synchronized int reloadFromDb() {
|
||||
if (!StringUtils.hasText(props.getServiceCode())) {
|
||||
// serviceCode 없으면 preload 불가 → 안전하게 빈 캐시
|
||||
cacheRef.set(Collections.emptyMap());
|
||||
return 0;
|
||||
}
|
||||
|
||||
var list = repo.findActiveKeysForService(props.getServiceCode());
|
||||
|
||||
Map<String, JwtUserClaimsDto> next = new ConcurrentHashMap<>(Math.max(16, list.size()));
|
||||
for (TApiKeyEntity e : list) {
|
||||
next.put(e.getApiKey(), JwtUserClaimsDto.builder()
|
||||
.userId(e.getUserId())
|
||||
.userCmpn(e.getUserCmpn())
|
||||
.userTeam(e.getUserTeam())
|
||||
.userName(e.getUserName())
|
||||
.userRole(e.getUserRole())
|
||||
for (TApiKeyEntity entity : list) {
|
||||
next.put(entity.getApiKey(), JwtUserClaimsDto.builder()
|
||||
.userId(entity.getUserId())
|
||||
.userCmpn(entity.getUserCmpn())
|
||||
.userTeam(entity.getUserTeam())
|
||||
.userName(entity.getUserName())
|
||||
.userRole(entity.getUserRole())
|
||||
.build());
|
||||
}
|
||||
|
||||
@@ -82,9 +64,6 @@ public class PreloadedApiKeyUserClaimsProvider implements ApiKeyUserClaimsProvid
|
||||
return next.size();
|
||||
}
|
||||
|
||||
/**
|
||||
* Admin 조회용(마스킹은 컨트롤러에서)
|
||||
*/
|
||||
public Map<String, JwtUserClaimsDto> snapshot() {
|
||||
return cacheRef.get();
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.kri.common.auth.persistence.entity;
|
||||
package com.kri.common.runtime.auth.apikey.persistence.entity;
|
||||
|
||||
import jakarta.persistence.Column;
|
||||
import jakarta.persistence.Entity;
|
||||
@@ -44,7 +44,7 @@ public class TApiKeyEntity {
|
||||
@Column(name = "user_role", nullable = false)
|
||||
private Integer userRole;
|
||||
|
||||
/*기본 정보 외*/
|
||||
/* 기본 정보 외 */
|
||||
@Column(name = "user_json")
|
||||
@JdbcTypeCode(SqlTypes.JSON)
|
||||
private Map<String, Object> userJson;
|
||||
@@ -55,5 +55,4 @@ public class TApiKeyEntity {
|
||||
@Column(name = "reg_dt", nullable = false)
|
||||
@ColumnDefault("CURRENT_TIMESTAMP")
|
||||
private LocalDateTime regDt;
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.kri.common.auth.persistence.entity;
|
||||
package com.kri.common.runtime.auth.apikey.persistence.entity;
|
||||
|
||||
import jakarta.persistence.EmbeddedId;
|
||||
import jakarta.persistence.Entity;
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.kri.common.auth.persistence.entity;
|
||||
package com.kri.common.runtime.auth.apikey.persistence.entity;
|
||||
|
||||
import jakarta.persistence.Column;
|
||||
import jakarta.persistence.Embeddable;
|
||||
@@ -1,6 +1,6 @@
|
||||
package com.kri.common.auth.persistence.repository;
|
||||
package com.kri.common.runtime.auth.apikey.persistence.repository;
|
||||
|
||||
import com.kri.common.auth.persistence.entity.TApiKeyEntity;
|
||||
import com.kri.common.runtime.auth.apikey.persistence.entity.TApiKeyEntity;
|
||||
import java.util.List;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
@@ -18,4 +18,4 @@ public interface ApiKeyRepository extends JpaRepository<TApiKeyEntity, String> {
|
||||
where a.status = 1 and m.id.serviceCode = :serviceCode
|
||||
""")
|
||||
List<TApiKeyEntity> findActiveKeysForService(@Param("serviceCode") String serviceCode);
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
package com.kri.common.security;
|
||||
package com.kri.common.runtime.security;
|
||||
|
||||
import com.kri.common.auth.jwt.JwtService;
|
||||
import com.kri.common.feature.auth.jwt.JwtService;
|
||||
import io.jsonwebtoken.JwtException;
|
||||
import jakarta.servlet.FilterChain;
|
||||
import jakarta.servlet.ServletException;
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.kri.common.swagger;
|
||||
package com.kri.common.runtime.swagger;
|
||||
|
||||
import jakarta.servlet.Filter;
|
||||
import jakarta.servlet.FilterChain;
|
||||
@@ -128,4 +128,4 @@ public class SwaggerIpFilter implements Filter {
|
||||
|
||||
return ip;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.kri.common.web;
|
||||
package com.kri.common.runtime.web;
|
||||
|
||||
import com.kri.common.feature.web.BusinessException;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.validation.ConstraintViolationException;
|
||||
import java.time.Instant;
|
||||
@@ -33,9 +34,9 @@ public class ErrorAdvice {
|
||||
private static final Logger log = LoggerFactory.getLogger(ErrorAdvice.class);
|
||||
|
||||
// 기본값은 모두 false → 외부 응답엔 노출 안 함
|
||||
@Value("${common.error.expose.handler:false}")
|
||||
@Value("${kri.error.expose.handler:false}")
|
||||
private boolean exposeHandler;
|
||||
@Value("${common.error.expose.service:false}")
|
||||
@Value("${kri.error.expose.service:false}")
|
||||
private boolean exposeService;
|
||||
|
||||
private static String resolveSupportedMethods(HttpRequestMethodNotSupportedException e) {
|
||||
@@ -1,13 +1,17 @@
|
||||
package com.kri.common.web;
|
||||
package com.kri.common.runtime.web;
|
||||
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import org.slf4j.MDC;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.web.method.HandlerMethod;
|
||||
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@ConditionalOnExpression("${kri.web.mdc.enabled:true}")
|
||||
public class MdcHandlerConfig {
|
||||
|
||||
@Bean
|
||||
@@ -1,13 +1,11 @@
|
||||
package com.kri.common.web;
|
||||
package com.kri.common.runtime.web;
|
||||
|
||||
import org.aspectj.lang.ProceedingJoinPoint;
|
||||
import org.aspectj.lang.annotation.Around;
|
||||
import org.aspectj.lang.annotation.Aspect;
|
||||
import org.slf4j.MDC;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Aspect
|
||||
@Component
|
||||
public class ServiceMdcAspect {
|
||||
|
||||
@Around("within(@org.springframework.stereotype.Service *)")
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.kri.common.web;
|
||||
package com.kri.common.runtime.web;
|
||||
|
||||
import jakarta.servlet.Filter;
|
||||
import jakarta.servlet.FilterChain;
|
||||
@@ -28,7 +28,7 @@ public class TraceFilter implements Filter {
|
||||
}
|
||||
|
||||
try (MDC.MDCCloseable ignored = MDC.putCloseable(MDC_KEY, id)) {
|
||||
httpRes.setHeader(REQ_HEADER, id); // 응답에도 echo
|
||||
httpRes.setHeader(REQ_HEADER, id); // ?묐떟?먮룄 echo
|
||||
chain.doFilter(req, res);
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
package com.kri.common.webmvc;
|
||||
package com.kri.common.runtime.webmvc;
|
||||
|
||||
import com.kri.common.enums.CodeEnum;
|
||||
import com.kri.common.feature.enums.CodeEnum;
|
||||
import org.springframework.core.convert.converter.Converter;
|
||||
import org.springframework.core.convert.converter.ConverterFactory;
|
||||
|
||||
@@ -16,7 +16,7 @@ public class CodeEnumConverter implements ConverterFactory<String, CodeEnum> {
|
||||
return null;
|
||||
}
|
||||
|
||||
// 1) enum name으로 먼저 시도 (EMAIL, SMS 등)
|
||||
// 1) enum name 먼저 비교합니다. (EMAIL, SMS 등)
|
||||
if (targetType.isEnum()) {
|
||||
for (T e : targetType.getEnumConstants()) {
|
||||
if (((Enum<?>) e).name().equalsIgnoreCase(source)) {
|
||||
@@ -25,7 +25,7 @@ public class CodeEnumConverter implements ConverterFactory<String, CodeEnum> {
|
||||
}
|
||||
}
|
||||
|
||||
// 2) code로 시도 ("11" 등)
|
||||
// 2) code 값도 비교합니다. ("11" 등)
|
||||
for (T e : targetType.getEnumConstants()) {
|
||||
Object code = e.getCode();
|
||||
if (code != null && source.equals(String.valueOf(code))) {
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.kri.common.webmvc;
|
||||
package com.kri.common.runtime.webmvc;
|
||||
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.kri.common.webmvc;
|
||||
package com.kri.common.runtime.webmvc;
|
||||
|
||||
import com.kri.common.autoconfigure.webmvc.KriIpAccessProperties;
|
||||
import java.util.List;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.security.web.util.matcher.IpAddressMatcher;
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.kri.common.webmvc;
|
||||
package com.kri.common.runtime.webmvc;
|
||||
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
|
||||
@@ -24,4 +24,4 @@ public final class IpUtil {
|
||||
// 3. 최후 fallback
|
||||
return request.getRemoteAddr();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,8 @@
|
||||
package com.kri.common.webmvc;
|
||||
package com.kri.common.runtime.webmvc;
|
||||
|
||||
import com.kri.common.auth.dto.JwtUserClaimsDto;
|
||||
import com.kri.common.auth.jwt.JwtService;
|
||||
import com.kri.common.feature.auth.dto.JwtUserClaimsDto;
|
||||
import com.kri.common.feature.auth.jwt.JwtService;
|
||||
import com.kri.common.feature.web.RequestUserContext;
|
||||
import io.jsonwebtoken.JwtException;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
@@ -32,12 +33,9 @@ public class RequestUserContextArgumentResolver implements HandlerMethodArgument
|
||||
@Nullable WebDataBinderFactory binderFactory
|
||||
) {
|
||||
HttpServletRequest request = webRequest.getNativeRequest(HttpServletRequest.class);
|
||||
|
||||
String clientIp = request != null ? IpUtil.getClientIp(request) : null;
|
||||
|
||||
JwtUserClaimsDto claims = null;
|
||||
|
||||
// 1) SecurityContext 에 이미 JwtUserClaimsDto 가 principal 로 들어있는 경우
|
||||
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
|
||||
if (auth != null) {
|
||||
Object principal = auth.getPrincipal();
|
||||
@@ -51,25 +49,18 @@ public class RequestUserContextArgumentResolver implements HandlerMethodArgument
|
||||
}
|
||||
}
|
||||
|
||||
// 2) 현재 공통 JwtService 는 principal 을 userId(String) 로 쓰고 있으니까,
|
||||
// 필요하면 Authorization 헤더에서 직접 토큰 파싱
|
||||
if (claims == null && request != null) {
|
||||
String authHeader = request.getHeader("Authorization");
|
||||
if (authHeader != null && authHeader.startsWith("Bearer ")) {
|
||||
String token = authHeader.substring(7);
|
||||
try {
|
||||
claims = jwtService.toUserClaimsDto(token);
|
||||
} catch (JwtException ex) {
|
||||
// 토큰 이상하면 그냥 anonymous 로 둠
|
||||
claims = jwtService.toUserClaimsDto(authHeader.substring(7));
|
||||
} catch (JwtException ignored) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (claims == null) {
|
||||
// 비인증 상태: IP만 채운 빈 컨텍스트
|
||||
return RequestUserContext.builder()
|
||||
.clientIp(clientIp)
|
||||
.build();
|
||||
return RequestUserContext.builder().clientIp(clientIp).build();
|
||||
}
|
||||
|
||||
return RequestUserContext.builder()
|
||||
@@ -1,24 +0,0 @@
|
||||
package com.kri.common.web;
|
||||
|
||||
import org.springframework.boot.autoconfigure.AutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.boot.web.servlet.FilterRegistrationBean;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.core.Ordered;
|
||||
|
||||
@AutoConfiguration
|
||||
@ConditionalOnProperty(name = "common.web.enabled", matchIfMissing = true)
|
||||
public class CommonWebAutoConfiguration {
|
||||
|
||||
@Bean
|
||||
public ErrorAdvice errorAdvice() {
|
||||
return new ErrorAdvice();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public FilterRegistrationBean<TraceFilter> traceFilter() {
|
||||
var reg = new FilterRegistrationBean<>(new TraceFilter());
|
||||
reg.setOrder(Ordered.HIGHEST_PRECEDENCE);
|
||||
return reg;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user