From 92ba64dc96de9b488e60c7b3fecc816a07eb1d86 Mon Sep 17 00:00:00 2001 From: choyeongju Date: Sat, 20 Jul 2024 03:46:40 +0900 Subject: [PATCH] =?UTF-8?q?:sparkles:=20[fix]=20=EC=9D=B4=EC=9A=A9?= =?UTF-8?q?=EC=9E=90=20ID=20=ED=95=84=EB=93=9C=20=EC=B6=94=EA=B0=80=20?= =?UTF-8?q?=EB=B0=8F=20=EC=95=88=EC=93=B0=EB=8A=94=20=EC=A3=BC=EC=84=9D=20?= =?UTF-8?q?=EC=82=AD=EC=A0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../server/discord/external/DiscordAppender.java | 5 ++++- .../java/nutshell/server/discord/filter/MDCFilter.java | 2 ++ src/main/java/nutshell/server/discord/util/MDCUtil.java | 9 +-------- 3 files changed, 7 insertions(+), 9 deletions(-) diff --git a/src/main/java/nutshell/server/discord/external/DiscordAppender.java b/src/main/java/nutshell/server/discord/external/DiscordAppender.java index c0eeeac..942c879 100644 --- a/src/main/java/nutshell/server/discord/external/DiscordAppender.java +++ b/src/main/java/nutshell/server/discord/external/DiscordAppender.java @@ -11,7 +11,6 @@ import nutshell.server.discord.util.MDCUtil; import nutshell.server.discord.model.EmbedObject; import nutshell.server.discord.util.StringUtil; -import nutshell.server.exception.code.InternalServerErrorCode; import java.awt.*; import java.io.IOException; @@ -74,6 +73,10 @@ protected void append(ILoggingEvent eventObject) { "[" + MDCUtil.USER_IP_MDC + "]", StringEscapeUtils.escapeJson(mdcPropertyMap.get(MDCUtil.USER_IP_MDC)), false) + .addField( + "[" + MDCUtil.USER_INFO + "]", + StringEscapeUtils.escapeJson(mdcPropertyMap.get(MDCUtil.USER_INFO)), + false) .addField( "[" + MDCUtil.HEADER_MAP_MDC + "]", StringEscapeUtils.escapeJson(mdcPropertyMap.get(MDCUtil.HEADER_MAP_MDC).replaceAll("[\\{\\{\\}]", "")), diff --git a/src/main/java/nutshell/server/discord/filter/MDCFilter.java b/src/main/java/nutshell/server/discord/filter/MDCFilter.java index 27f5bc6..9539790 100644 --- a/src/main/java/nutshell/server/discord/filter/MDCFilter.java +++ b/src/main/java/nutshell/server/discord/filter/MDCFilter.java @@ -8,6 +8,7 @@ import lombok.extern.slf4j.Slf4j; import nutshell.server.discord.util.HttpRequestUtil; import nutshell.server.discord.util.MDCUtil; +import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.stereotype.Component; import org.springframework.web.filter.OncePerRequestFilter; import org.springframework.web.util.WebUtils; @@ -28,6 +29,7 @@ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse MDCUtil.setJsonValue(MDCUtil.REQUEST_URI_MDC, HttpRequestUtil.getRequestUri(Objects.requireNonNull(httpReq))); MDCUtil.setJsonValue(MDCUtil.USER_IP_MDC, HttpRequestUtil.getUserIP(Objects.requireNonNull(httpReq))); + MDCUtil.setJsonValue(MDCUtil.USER_INFO, SecurityContextHolder.getContext().getAuthentication().getPrincipal()); MDCUtil.setJsonValue(MDCUtil.HEADER_MAP_MDC, HttpRequestUtil.getHeaderMap(httpReq)); MDCUtil.setJsonValue(MDCUtil.PARAMETER_MAP_MDC, HttpRequestUtil.getParamMap(httpReq)); MDCUtil.setJsonValue(MDCUtil.BODY_MDC, HttpRequestUtil.getBody(httpReq)); diff --git a/src/main/java/nutshell/server/discord/util/MDCUtil.java b/src/main/java/nutshell/server/discord/util/MDCUtil.java index 2c5099c..a092e78 100644 --- a/src/main/java/nutshell/server/discord/util/MDCUtil.java +++ b/src/main/java/nutshell/server/discord/util/MDCUtil.java @@ -12,16 +12,13 @@ public class MDCUtil { public static final String REQUEST_URI_MDC = "이용자 요청 URI 정보"; public static final String USER_IP_MDC = "이용자 IP 정보"; + public static final String USER_INFO = "이용자 ID 정보"; public static final String HEADER_MAP_MDC = "HTTP 헤더 정보"; public static final String PARAMETER_MAP_MDC = "Parameter 정보"; public static final String BODY_MDC = "HTTP Body 정보"; private static final ObjectMapper objectMapper = new ObjectMapper(); private static final MDCAdapter mdc = MDC.getMDCAdapter(); - public static void set(String key, String value) { - mdc.put(key, value); - } - public static void setJsonValue(String key, Object value) throws JsonProcessingException { try { if (value != null) { @@ -35,8 +32,4 @@ public static void setJsonValue(String key, Object value) throws JsonProcessingE throw ex; } } - - public static void clear() { - MDC.clear(); - } }