From 9b0d3ad5c294aadb58236ba47efce568ce86cf02 Mon Sep 17 00:00:00 2001 From: kric_jspark Date: Tue, 27 Oct 2026 15:25:34 +0900 Subject: [PATCH] =?UTF-8?q?261027=5F01=5FENH:=20KART=20=ED=94=84=EB=A1=9C?= =?UTF-8?q?=EC=A0=9D=ED=8A=B8=20=EB=AA=A9=EB=A1=9D=20=EC=A1=B0=ED=9A=8C=20?= =?UTF-8?q?=EB=B0=8F=20=EA=B3=B5=EC=9C=A0=20URL=20=EC=83=9D=EC=84=B1=20?= =?UTF-8?q?=EC=97=B0=EB=8F=99=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../server/controller/KartController.java | 48 ++++++ .../contact/server/service/KartService.java | 137 ++++++++++++++++++ src/main/resources/application-dev.yml | 1 + src/main/resources/application-local.yml | 2 + src/main/resources/application-prod.yml | 1 + src/main/resources/application.yml | 6 + 6 files changed, 195 insertions(+) create mode 100644 src/main/java/com/kri/contact/server/controller/KartController.java create mode 100644 src/main/java/com/kri/contact/server/service/KartService.java diff --git a/src/main/java/com/kri/contact/server/controller/KartController.java b/src/main/java/com/kri/contact/server/controller/KartController.java new file mode 100644 index 0000000..bfaa59d --- /dev/null +++ b/src/main/java/com/kri/contact/server/controller/KartController.java @@ -0,0 +1,48 @@ +package com.kri.contact.server.controller; + +import com.fasterxml.jackson.databind.JsonNode; +import com.kri.common.feature.web.RequestUserContext; +import com.kri.contact.server.service.KartService; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.validation.constraints.Max; +import jakarta.validation.constraints.Min; +import jakarta.validation.constraints.NotBlank; +import jakarta.validation.constraints.Size; +import lombok.RequiredArgsConstructor; +import org.springframework.validation.annotation.Validated; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequestMapping("/api/v1/kart/project") +@RequiredArgsConstructor +@Validated +public class KartController { + + private final KartService kartService; + + @GetMapping("/list") + public JsonNode projects( + @RequestParam @NotBlank @Size(max = 32) String intranetCode, + @RequestParam @Min(0) @Max(1) int projectType, + @RequestParam(defaultValue = "") String keyword, + HttpServletRequest request, + RequestUserContext user + ) { + return kartService.projects(intranetCode, projectType, keyword, request, user); + } + + @PostMapping("/{projectType}/{pkey}/share") + public String reportUrl( + @PathVariable @Min(0) @Max(1) int projectType, + @PathVariable String pkey, + HttpServletRequest request, + RequestUserContext user + ) { + return kartService.reportUrl(projectType, pkey, request, user); + } +} diff --git a/src/main/java/com/kri/contact/server/service/KartService.java b/src/main/java/com/kri/contact/server/service/KartService.java new file mode 100644 index 0000000..c0ba7f9 --- /dev/null +++ b/src/main/java/com/kri/contact/server/service/KartService.java @@ -0,0 +1,137 @@ +package com.kri.contact.server.service; + +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.kri.common.feature.http.bypass.BypassClient; +import com.kri.common.feature.http.bypass.BypassForwardOptions; +import com.kri.common.feature.http.bypass.BypassRequest; +import com.kri.common.feature.http.bypass.BypassResponse; +import com.kri.common.feature.web.BusinessException; +import com.kri.common.feature.web.RequestUserContext; +import jakarta.servlet.http.HttpServletRequest; +import java.io.IOException; +import java.net.URI; +import java.nio.charset.StandardCharsets; +import java.util.List; +import java.util.Map; +import lombok.RequiredArgsConstructor; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.http.HttpStatus; +import org.springframework.stereotype.Service; + +@Service +@RequiredArgsConstructor +public class KartService { + + private static final byte[] SHARE_BODY = + "{\"realUrl\":\"/Report/Current\",\"mode\":\"U\"}".getBytes(StandardCharsets.UTF_8); + + private final BypassClient bypassClient; + private final ObjectMapper objectMapper; + + @Value("${kri.auth.apikey.key}") + private String apiKey; + + public JsonNode projects(String intranetCode, int projectType, String keyword, + HttpServletRequest request, RequestUserContext user) { + requireAuthenticated(user); + validateProjectType(projectType); + if (intranetCode == null || intranetCode.isBlank() || intranetCode.length() > 32 + || user.getUserTeam() == null || user.getUserTeam().isBlank()) { + throw BusinessException.badRequest("KART_PROJECT_CONTEXT_REQUIRED", + "인트라넷 프로젝트 코드와 사용자 팀 정보가 필요합니다."); + } + Map> query = Map.of( + "projectType", List.of(Integer.toString(projectType)), + "intranetCode", List.of(intranetCode), + "userTeam", List.of(user.getUserTeam()), + "keyword", List.of(keyword == null ? "" : keyword.trim())); + JsonNode result = forward(request, "GET", query, new byte[0]); + if (!result.isArray()) throw invalidResponse(); + for (JsonNode projectInfo : result) { + if (!projectInfo.path("pkey").isTextual() || !isPkey(projectInfo.path("pkey").asText()) + || !projectInfo.path("title").isTextual()) throw invalidResponse(); + } + return result; + } + + public String reportUrl(int projectType, String pkey, + HttpServletRequest request, RequestUserContext user) { + requireAuthenticated(user); + validateProjectType(projectType); + if (!isPkey(pkey)) { + throw BusinessException.badRequest("KART_PKEY_INVALID", "설문 프로젝트 코드를 확인해 주세요."); + } + JsonNode result = forward(request, "POST", Map.of(), SHARE_BODY); + if (!result.path("url").isTextual() || !result.path("urlKey").isTextual() + || !result.path("projectType").isIntegralNumber() + || result.path("projectType").asInt() != projectType + || !result.path("pkey").isTextual() || !pkey.equals(result.path("pkey").asText()) + || !"/Report/Current".equals(result.path("realUrl").asText()) + || !result.path("pageOnly").isBoolean() || result.path("pageOnly").asBoolean()) { + throw invalidResponse(); + } + String url = result.path("url").asText(); + try { + URI uri = URI.create(url); + if (!("https".equals(uri.getScheme()) || "http".equals(uri.getScheme())) + || uri.getHost() == null || uri.getRawUserInfo() != null + || uri.getRawQuery() != null || uri.getRawFragment() != null + || !result.path("urlKey").asText().matches("[A-Za-z0-9_-]{1,128}") + || !("/External/" + result.path("urlKey").asText()).equals(uri.getPath())) { + throw invalidResponse(); + } + } catch (IllegalArgumentException e) { + throw invalidResponse(); + } + return url; + } + + private JsonNode forward(HttpServletRequest request, String method, + Map> query, byte[] body) { + BypassForwardOptions options = BypassForwardOptions.builder() + .stripPrefix("/api/v1/kart") + .requestCustomizer((incoming, context) -> BypassRequest.builder() + .method(method) + .path(incoming.path()) + .queryParams(query) + .header("Content-Type", "application/json") + .header("X-API-Key", apiKey) + .body(body) + .build()) + .build(); + BypassResponse response = bypassClient.forward("kart", request, body, options).requireSuccess(); + if (!response.isJsonBody()) throw invalidResponse(); + try { + JsonNode result = objectMapper.readTree(response.body()); + if (result == null) throw invalidResponse(); + return result; + } catch (IOException e) { + throw invalidResponse(); + } + } + + private void validateProjectType(int projectType) { + if (projectType != 0 && projectType != 1) { + throw BusinessException.badRequest("KART_PROJECT_TYPE_INVALID", "KON/KIWI 구분을 확인해 주세요."); + } + } + + private void requireAuthenticated(RequestUserContext user) { + if (user == null || !user.isAuthenticated() || user.getUserRole() == null) { + throw BusinessException.unauthorized("AUTH_REQUIRED", "로그인이 필요합니다."); + } + } + + private boolean isPkey(String pkey) { + return pkey != null && !pkey.isBlank() && pkey.length() <= 20 + && !pkey.equals(".") && !pkey.equals("..") + && pkey.indexOf('|') < 0 && pkey.indexOf('/') < 0 && pkey.indexOf('\\') < 0 + && pkey.chars().noneMatch(c -> c < 32 || c == 127); + } + + private BusinessException invalidResponse() { + return BusinessException.of(HttpStatus.BAD_GATEWAY, "KART_RESPONSE_INVALID", + "KART 응답 형식을 확인해 주세요."); + } +} diff --git a/src/main/resources/application-dev.yml b/src/main/resources/application-dev.yml index 9b70dc0..5e467c8 100644 --- a/src/main/resources/application-dev.yml +++ b/src/main/resources/application-dev.yml @@ -3,6 +3,7 @@ kri: intranet-extension: https://intranet-ext.kric.com/api/v1 noti-hub: https://noti-hub.kric.com/api/v1 short-url: https://short-url.kric.com/api/v1 + kart: https://test-kart.kric.com/api/kric-internal contact-survey: enabled: true diff --git a/src/main/resources/application-local.yml b/src/main/resources/application-local.yml index 93daad7..68dd75b 100644 --- a/src/main/resources/application-local.yml +++ b/src/main/resources/application-local.yml @@ -5,6 +5,8 @@ kri: #noti-hub: http://localhost:4011/api/v1 noti-hub: https://noti-hub.kric.com/api/v1 short-url: https://short-url.kric.com/api/v1 + #kart: http://localhost:4014/api/kric-internal + kart: https://test-kart.kric.com/api/kric-internal contact-survey: enabled: true diff --git a/src/main/resources/application-prod.yml b/src/main/resources/application-prod.yml index d2158c1..04b6f6d 100644 --- a/src/main/resources/application-prod.yml +++ b/src/main/resources/application-prod.yml @@ -3,6 +3,7 @@ kri: intranet-extension: https://intranet-ext.kric.com/api/v1 noti-hub: https://noti-hub.kric.com/api/v1 short-url: https://short-url.kric.com/api/v1 + kart: https://kart.kric.com/api/kric-internal contact-survey: enabled: true diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 39b29a2..ef5ca37 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -48,6 +48,12 @@ kri: - /message-report/** - /message-send/cancel/single - /message-send/cancel/bulk + kart: + throw-on-error: true + base-url: ${kri.api-url.kart} + allowed-paths: + - /project/list + - /project/*/*/share auth: apikey: key: "4B726963-436F6E74616374-53657276696365-402531213230"