251121_01_ENH : JWT 수정, CodeEnum 추가, API 통신 모듈 추가
This commit is contained in:
@@ -60,14 +60,13 @@ public class JwtService {
|
||||
claims.put("userId", dto.getUserId());
|
||||
claims.put("userTeam", dto.getUserTeam());
|
||||
claims.put("userName", dto.getUserName());
|
||||
claims.put("role", dto.getRole());
|
||||
claims.put("role", dto.getUserRole());
|
||||
return claims;
|
||||
}
|
||||
|
||||
// ======================================================
|
||||
// 토큰 생성 (DTO 기반)
|
||||
// ======================================================
|
||||
|
||||
/**
|
||||
* JwtUserClaimsDto 기반 Access Token 생성
|
||||
* - subject = userId
|
||||
@@ -131,13 +130,13 @@ public class JwtService {
|
||||
|
||||
// 숫자는 Integer / Long 등으로 들어올 수 있으니 Number로 받았다가 intValue()
|
||||
Number roleNumber = claims.get("role", Number.class);
|
||||
Integer role = roleNumber != null ? roleNumber.intValue() : null;
|
||||
Integer userRole = roleNumber != null ? roleNumber.intValue() : null;
|
||||
|
||||
return JwtUserClaimsDto.builder()
|
||||
.userId(userId)
|
||||
.userTeam(userTeam)
|
||||
.userName(userName)
|
||||
.role(role)
|
||||
.userRole(userRole)
|
||||
.build();
|
||||
}
|
||||
|
||||
@@ -164,8 +163,8 @@ public class JwtService {
|
||||
String principal = dto.getUserId();
|
||||
|
||||
List<GrantedAuthority> authorities = new ArrayList<>();
|
||||
if (dto.getRole() != null) {
|
||||
String authority = "ROLE_" + dto.getRole();
|
||||
if (dto.getUserRole() != null) {
|
||||
String authority = "ROLE_" + dto.getUserRole();
|
||||
authorities.add(new SimpleGrantedAuthority(authority));
|
||||
}
|
||||
|
||||
|
||||
@@ -22,5 +22,5 @@ public class JwtUserClaimsDto {
|
||||
String userName;
|
||||
|
||||
@Schema(description = "권한 코드")
|
||||
Integer role;
|
||||
Integer userRole;
|
||||
}
|
||||
|
||||
5
src/main/java/com/kri/common/enums/CodeEnum.java
Normal file
5
src/main/java/com/kri/common/enums/CodeEnum.java
Normal file
@@ -0,0 +1,5 @@
|
||||
package com.kri.common.enums;
|
||||
|
||||
public interface CodeEnum<T> {
|
||||
T getCode();
|
||||
}
|
||||
20
src/main/java/com/kri/common/enums/CodeEnumUtils.java
Normal file
20
src/main/java/com/kri/common/enums/CodeEnumUtils.java
Normal file
@@ -0,0 +1,20 @@
|
||||
package com.kri.common.enums;
|
||||
|
||||
public final class CodeEnumUtils {
|
||||
|
||||
private CodeEnumUtils() {}
|
||||
|
||||
public static <E extends Enum<E> & CodeEnum<V>, V> E fromCode(Class<E> enumClass, V code) {
|
||||
if (code == null) return null;
|
||||
|
||||
for (E e : enumClass.getEnumConstants()) {
|
||||
if (e.getCode().equals(code)) {
|
||||
return e;
|
||||
}
|
||||
}
|
||||
|
||||
throw new IllegalArgumentException(
|
||||
"Unknown code [" + code + "] for enum class " + enumClass.getName()
|
||||
);
|
||||
}
|
||||
}
|
||||
99
src/main/java/com/kri/common/http/ExternalApiClient.java
Normal file
99
src/main/java/com/kri/common/http/ExternalApiClient.java
Normal file
@@ -0,0 +1,99 @@
|
||||
package com.kri.common.http;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
import java.net.http.HttpClient;
|
||||
import java.net.http.HttpRequest;
|
||||
import java.net.http.HttpResponse;
|
||||
import java.time.Duration;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 간단한 외부 API 통신 모듈.
|
||||
* - url, 추가 헤더, body를 받아 호출합니다.
|
||||
* - GET, POST, PUT, PATCH, DELETE 메서드를 제공합니다.
|
||||
* - body는 문자열(JSON 등) 기준입니다.
|
||||
*/
|
||||
public class ExternalApiClient {
|
||||
|
||||
private final HttpClient httpClient;
|
||||
private final Duration requestTimeout;
|
||||
|
||||
/**
|
||||
* 기본 생성자 (커넥션 풀/타임아웃 기본값 세팅)
|
||||
*/
|
||||
public ExternalApiClient() {
|
||||
this(HttpClient.newBuilder()
|
||||
.connectTimeout(Duration.ofSeconds(10))
|
||||
.build(), Duration.ofSeconds(30));
|
||||
}
|
||||
|
||||
/**
|
||||
* 사용자 지정 HttpClient/요청 타임아웃으로 생성
|
||||
*/
|
||||
public ExternalApiClient(HttpClient httpClient, Duration requestTimeout) {
|
||||
this.httpClient = httpClient;
|
||||
this.requestTimeout = requestTimeout == null ? Duration.ofSeconds(30) : requestTimeout;
|
||||
}
|
||||
|
||||
public ExternalApiResponse get(String url, Map<String, String> headers) {
|
||||
return send("GET", url, headers, null);
|
||||
}
|
||||
|
||||
public ExternalApiResponse delete(String url, Map<String, String> headers) {
|
||||
return send("DELETE", url, headers, null);
|
||||
}
|
||||
|
||||
public ExternalApiResponse post(String url, Map<String, String> headers, String body) {
|
||||
return send("POST", url, headers, body);
|
||||
}
|
||||
|
||||
public ExternalApiResponse put(String url, Map<String, String> headers, String body) {
|
||||
return send("PUT", url, headers, body);
|
||||
}
|
||||
|
||||
public ExternalApiResponse patch(String url, Map<String, String> headers, String body) {
|
||||
return send("PATCH", url, headers, body);
|
||||
}
|
||||
|
||||
private ExternalApiResponse send(String method, String url, Map<String, String> headers, String body) {
|
||||
try {
|
||||
HttpRequest.Builder builder = HttpRequest.newBuilder()
|
||||
.uri(URI.create(url))
|
||||
.timeout(requestTimeout)
|
||||
.method(method, buildBodyPublisher(method, body));
|
||||
|
||||
// 추가 헤더 설정
|
||||
if (headers != null) {
|
||||
headers.forEach((k, v) -> {
|
||||
if (k != null && v != null) builder.header(k, v);
|
||||
});
|
||||
}
|
||||
|
||||
// body가 있고 Content-Type이 없다면 기본 application/json
|
||||
if (body != null && (headers == null || !headers.keySet().stream()
|
||||
.map(String::toLowerCase).anyMatch("content-type"::equals))) {
|
||||
builder.header("Content-Type", "application/json; charset=utf-8");
|
||||
}
|
||||
|
||||
HttpRequest request = builder.build();
|
||||
HttpResponse<String> resp = httpClient.send(request, HttpResponse.BodyHandlers.ofString());
|
||||
return ExternalApiResponse.of(resp.statusCode(), resp.headers(), resp.body());
|
||||
} catch (IOException | InterruptedException e) {
|
||||
// 인터럽트 복원
|
||||
if (e instanceof InterruptedException) Thread.currentThread().interrupt();
|
||||
throw new RuntimeException("External API 호출 중 오류가 발생했습니다: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
private static HttpRequest.BodyPublisher buildBodyPublisher(String method, String body) {
|
||||
// GET/DELETE 등 body 없는 메서드는 noBody 사용
|
||||
if (body == null || body.isEmpty()) {
|
||||
return switch (method) {
|
||||
case "POST", "PUT", "PATCH" -> HttpRequest.BodyPublishers.ofString("");
|
||||
default -> HttpRequest.BodyPublishers.noBody();
|
||||
};
|
||||
}
|
||||
return HttpRequest.BodyPublishers.ofString(body);
|
||||
}
|
||||
}
|
||||
36
src/main/java/com/kri/common/http/ExternalApiResponse.java
Normal file
36
src/main/java/com/kri/common/http/ExternalApiResponse.java
Normal file
@@ -0,0 +1,36 @@
|
||||
package com.kri.common.http;
|
||||
|
||||
import java.net.http.HttpHeaders;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 외부 API 호출 결과를 단순하게 담는 DTO.
|
||||
*/
|
||||
public class ExternalApiResponse {
|
||||
private final int statusCode;
|
||||
private final Map<String, List<String>> headers;
|
||||
private final String body;
|
||||
|
||||
public ExternalApiResponse(int statusCode, Map<String, List<String>> headers, String body) {
|
||||
this.statusCode = statusCode;
|
||||
this.headers = headers;
|
||||
this.body = body;
|
||||
}
|
||||
|
||||
public int getStatusCode() {
|
||||
return statusCode;
|
||||
}
|
||||
|
||||
public Map<String, List<String>> getHeaders() {
|
||||
return headers;
|
||||
}
|
||||
|
||||
public String getBody() {
|
||||
return body;
|
||||
}
|
||||
|
||||
public static ExternalApiResponse of(int status, HttpHeaders headers, String body) {
|
||||
return new ExternalApiResponse(status, headers.map(), body);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.kri.common.jpa;
|
||||
|
||||
import com.kri.common.enums.CodeEnum;
|
||||
import com.kri.common.enums.CodeEnumUtils;
|
||||
import jakarta.persistence.AttributeConverter;
|
||||
|
||||
public abstract class AbstractCodeEnumConverter<
|
||||
E extends Enum<E> & CodeEnum<V>,
|
||||
V
|
||||
> implements AttributeConverter<E, V> {
|
||||
|
||||
private final Class<E> enumClass;
|
||||
|
||||
protected AbstractCodeEnumConverter(Class<E> enumClass) {
|
||||
this.enumClass = enumClass;
|
||||
}
|
||||
|
||||
@Override
|
||||
public V convertToDatabaseColumn(E attribute) {
|
||||
return attribute == null ? null : attribute.getCode();
|
||||
}
|
||||
|
||||
@Override
|
||||
public E convertToEntityAttribute(V dbData) {
|
||||
return CodeEnumUtils.fromCode(enumClass, dbData);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user