From fbbc8c80bb1e7fe675f86c6f51042dbca202a2a7 Mon Sep 17 00:00:00 2001 From: ebrahimiar <66822910+ebrahimiar@users.noreply.github.com> Date: Mon, 8 Apr 2024 17:26:08 +0330 Subject: [PATCH] [feature] add X-Request-ID and X-User-IP fields to headers. the value of X-Request-ID is extracted from requestId field in MDC. the value of X-User-IP is extracted from clientIP field in MDC. --- .../java/com/tosan/client/http/core/Constants.java | 6 ++++++ .../configuration/AbstractFeignConfiguration.java | 12 ++++++++++-- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/tosan-httpclient-spring-boot-core/src/main/java/com/tosan/client/http/core/Constants.java b/tosan-httpclient-spring-boot-core/src/main/java/com/tosan/client/http/core/Constants.java index f88ebd1..3af9c4e 100644 --- a/tosan-httpclient-spring-boot-core/src/main/java/com/tosan/client/http/core/Constants.java +++ b/tosan-httpclient-spring-boot-core/src/main/java/com/tosan/client/http/core/Constants.java @@ -10,4 +10,10 @@ public interface Constants { SecureParameter AUTHORIZATION_SECURE_PARAM = new SecureParameter("authorization", MaskType.COMPLETE); SecureParameter PROXY_AUTHORIZATION_SECURE_PARAM = new SecureParameter("proxy-authorization", MaskType.COMPLETE); + String X_USER_IP = "X-User-IP"; + String MDC_CLIENT_IP = "clientIP"; + String MDC_REQUEST_ID = "requestId"; + String X_REQUEST_ID = "X-Request-ID"; + String ACCEPT_HEADER = "Accept"; + String CONTENT_TYPE_HEADER = "Content-Type"; } diff --git a/tosan-httpclient-spring-boot-starter/src/main/java/com/tosan/client/http/starter/configuration/AbstractFeignConfiguration.java b/tosan-httpclient-spring-boot-starter/src/main/java/com/tosan/client/http/starter/configuration/AbstractFeignConfiguration.java index 068c7df..2982a05 100644 --- a/tosan-httpclient-spring-boot-starter/src/main/java/com/tosan/client/http/starter/configuration/AbstractFeignConfiguration.java +++ b/tosan-httpclient-spring-boot-starter/src/main/java/com/tosan/client/http/starter/configuration/AbstractFeignConfiguration.java @@ -27,6 +27,7 @@ import org.apache.hc.client5.http.impl.classic.HttpClientBuilder; import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManagerBuilder; import org.apache.hc.core5.http.ContentType; +import org.slf4j.MDC; import org.springframework.beans.factory.ObjectFactory; import org.springframework.boot.autoconfigure.http.HttpMessageConverters; import org.springframework.cloud.openfeign.AnnotatedParameterProcessor; @@ -49,6 +50,7 @@ import java.util.List; import java.util.concurrent.TimeUnit; +import static com.tosan.client.http.core.Constants.*; import static com.tosan.tools.mask.starter.configuration.MaskBeanConfiguration.SECURED_PARAMETERS; /** @@ -110,8 +112,14 @@ public Client feignClient(HttpClient httpClient) { public RequestInterceptor requestInterceptor() { return requestTemplate -> { - requestTemplate.header("Accept", ContentType.APPLICATION_JSON.getMimeType()); - requestTemplate.header("Content-Type", ContentType.APPLICATION_JSON.getMimeType()); + requestTemplate.header(ACCEPT_HEADER, ContentType.APPLICATION_JSON.getMimeType()); + requestTemplate.header(CONTENT_TYPE_HEADER, ContentType.APPLICATION_JSON.getMimeType()); + if (MDC.get(MDC_REQUEST_ID) != null) { + requestTemplate.header(X_REQUEST_ID, MDC.get(MDC_REQUEST_ID)); + } + if (MDC.get(MDC_CLIENT_IP) != null) { + requestTemplate.header(X_USER_IP, MDC.get(MDC_CLIENT_IP)); + } }; }