261027_01_ENH: KART 프로젝트 목록 조회 및 공유 URL 생성 연동 추가

This commit is contained in:
2026-10-27 15:25:34 +09:00
parent fc62f9cb38
commit 9b0d3ad5c2
6 changed files with 195 additions and 0 deletions

View File

@@ -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);
}
}

View File

@@ -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<String, List<String>> 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<String, List<String>> 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 응답 형식을 확인해 주세요.");
}
}

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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"