Skip to content

Commit 5f24286

Browse files
committed
Added optional X-Request-Id to RemoteIpFilter
1 parent 217aca2 commit 5f24286

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed

java/org/apache/catalina/filters/RemoteIpFilter.java

+57
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,14 @@
107107
* <td>x-forwarded-for</td>
108108
* </tr>
109109
* <tr>
110+
* <td>requestIdHeader</td>
111+
* <td>Name of the Http Header read by this servlet filter that holds a request ID passed by a proxy or the requesting
112+
* client</td>
113+
* <td>RequestIdHeader</td>
114+
* <td>Compliant http header name</td>
115+
* <td>x-request-id</td>
116+
* </tr>
117+
* <tr>
110118
* <td>internalProxies</td>
111119
* <td>Regular expression that matches the IP addresses of internal proxies. If they appear in the
112120
* <code>remoteIpHeader</code> value, they will be trusted and will not appear in the <code>proxiesHeader</code>
@@ -210,6 +218,10 @@
210218
* &lt;param-name&gt;protocolHeader&lt;/param-name&gt;
211219
* &lt;param-value&gt;x-forwarded-proto&lt;/param-value&gt;
212220
* &lt;/init-param&gt;
221+
* &lt;init-param&gt;
222+
* &lt;param-name&gt;requestIdHeader&lt;/param-name&gt;
223+
* &lt;param-value&gt;x-request-id&lt;/param-value&gt;
224+
* &lt;/init-param&gt;
213225
* &lt;/filter&gt;
214226
*
215227
* &lt;filter-mapping&gt;
@@ -476,6 +488,8 @@ public static class XForwardedRequest extends HttpServletRequestWrapper {
476488

477489
protected int serverPort;
478490

491+
protected String requestId;
492+
479493
public XForwardedRequest(HttpServletRequest request) {
480494
super(request);
481495
this.localName = request.getLocalName();
@@ -568,6 +582,15 @@ public String getRemoteHost() {
568582
return this.remoteHost;
569583
}
570584

585+
@Override
586+
public String getRequestId() {
587+
if (this.requestId != null) {
588+
return this.requestId;
589+
}
590+
591+
return super.getRequest().getRequestId();
592+
}
593+
571594
@Override
572595
public String getScheme() {
573596
return scheme;
@@ -617,6 +640,10 @@ public void setRemoteHost(String remoteHost) {
617640
this.remoteHost = remoteHost;
618641
}
619642

643+
public void setRequestId(String requestId) {
644+
this.requestId = requestId;
645+
}
646+
620647
public void setScheme(String scheme) {
621648
this.scheme = scheme;
622649
}
@@ -667,6 +694,8 @@ public StringBuffer getRequestURL() {
667694

668695
protected static final String REMOTE_IP_HEADER_PARAMETER = "remoteIpHeader";
669696

697+
protected static final String REQUEST_ID_HEADER_PARAMETER = "requestIdHeader";
698+
670699
protected static final String TRUSTED_PROXIES_PARAMETER = "trustedProxies";
671700

672701
protected static final String ENABLE_LOOKUPS_PARAMETER = "enableLookups";
@@ -717,6 +746,11 @@ public StringBuffer getRequestURL() {
717746
*/
718747
private String remoteIpHeader = "X-Forwarded-For";
719748

749+
/**
750+
* @see #setRequestIdHeader(String)
751+
*/
752+
private String requestIdHeader = "";
753+
720754
/**
721755
* @see #setRequestAttributesEnabled(boolean)
722756
*/
@@ -843,6 +877,14 @@ public void doFilter(HttpServletRequest request, HttpServletResponse response, F
843877
}
844878
}
845879
}
880+
881+
if (!requestIdHeader.isEmpty()) {
882+
String requestIdHeaderValue = request.getHeader(requestIdHeader);
883+
if (requestIdHeaderValue != null) {
884+
xRequest.setRequestId(requestIdHeaderValue);
885+
}
886+
}
887+
846888
request.setAttribute(Globals.REQUEST_FORWARDED_ATTRIBUTE, Boolean.TRUE);
847889

848890
if (log.isTraceEnabled()) {
@@ -1016,6 +1058,10 @@ public void init() throws ServletException {
10161058
setRemoteIpHeader(getInitParameter(REMOTE_IP_HEADER_PARAMETER));
10171059
}
10181060

1061+
if (getInitParameter(REQUEST_ID_HEADER_PARAMETER) != null) {
1062+
setRequestIdHeader(getInitParameter(REQUEST_ID_HEADER_PARAMETER));
1063+
}
1064+
10191065
if (getInitParameter(TRUSTED_PROXIES_PARAMETER) != null) {
10201066
setTrustedProxies(getInitParameter(TRUSTED_PROXIES_PARAMETER));
10211067
}
@@ -1221,6 +1267,17 @@ public void setRemoteIpHeader(String remoteIpHeader) {
12211267
this.remoteIpHeader = remoteIpHeader;
12221268
}
12231269

1270+
/**
1271+
* <p>Name of the http header from which the request id is extracted.</p>
1272+
*
1273+
* <p>Request id propagation is disabled by default. Set a value, e.g. <code>X-Request-Id</code>, to enable it.</p>
1274+
*
1275+
* @param requestIdHeader The header name
1276+
*/
1277+
public void setRequestIdHeader(String requestIdHeader) {
1278+
this.requestIdHeader = requestIdHeader;
1279+
}
1280+
12241281
/**
12251282
* Should this filter set request attributes for IP address, Hostname, protocol and port used for the request? This
12261283
* are typically used in conjunction with an {@link AccessLog} which will otherwise log the original values. Default

webapps/docs/config/filter.xml

+11
Original file line numberDiff line numberDiff line change
@@ -1445,6 +1445,10 @@ FINE: Request "/docs/config/manager.html" with response status "200"
14451445
<param-name>protocolHeader</param-name>
14461446
<param-value>x-forwarded-proto</param-value>
14471447
</init-param>
1448+
<init-param>
1449+
<param-name>requestIdHeader</param-name>
1450+
<param-value>x-request-id</param-value>
1451+
</init-param>
14481452
</filter>]]></source>
14491453
<p>Request values:</p>
14501454
<table class="defaultTable">
@@ -1685,6 +1689,13 @@ FINE: Request "/docs/config/manager.html" with response status "200"
16851689
specified, the default of <code>x-forwarded-for</code> is used.</p>
16861690
</attribute>
16871691

1692+
<attribute name="requestIdHeader" required="false">
1693+
<p>Name of the HTTP Header read by this valve that holds request ID
1694+
if passed by the Proxy server or requesting client. Request ID propagation
1695+
is disabled by default, but can be enabled by setting this attribute,
1696+
e.g. to <code>x-request-id</code>.</p>
1697+
</attribute>
1698+
16881699
<attribute name="internalProxies" required="false">
16891700
<p>Regular expression (using <code>java.util.regex</code>) that a
16901701
proxy&apos;s IP address must match to be considered an internal proxy.

0 commit comments

Comments
 (0)