Skip to content

Commit

Permalink
[feature] add X-Request-ID and X-User-IP fields to headers.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ebrahimiar authored Apr 8, 2024
1 parent bfdd91a commit fbbc8c8
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

/**
Expand Down Expand Up @@ -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));
}
};
}

Expand Down

0 comments on commit fbbc8c8

Please sign in to comment.