From: markt
*
* https
+ *
+ * httpServerPort
+ * Value returned by {@link ServletRequest#getServerPort()} when the
+ * protocolHeader indicates http protocolN/A
+ * integer
+ * 80
+ *
+ *
*
* httpsServerPort
+ * Value returned by {@link ServletRequest#getServerPort()} when the
+ * protocolHeader indicates https protocolN/A
+ * integer
+ * 443
+ *
@@ -575,6 +588,8 @@ public class RemoteIpFilter implements Filter { */ private static final Pattern commaSeparatedValuesPattern = Pattern.compile("\\s*,\\s*"); + protected static final String HTTP_SERVER_PORT_PARAMETER = "httpServerPort"; + protected static final String HTTPS_SERVER_PORT_PARAMETER = "httpsServerPort"; protected static final String INTERNAL_PROXIES_PARAMETER = "internalProxies"; @@ -655,10 +670,15 @@ public class RemoteIpFilter implements Filter { } /** + * @see #setHttpServerPort(int) + */ + private int httpServerPort = 80; + + /** * @see #setHttpsServerPort(int) */ private int httpsServerPort = 443; - + /** * @see #setInternalProxies(String) */ @@ -744,10 +764,16 @@ public class RemoteIpFilter implements Filter { if (protocolHeader != null) { String protocolHeaderValue = request.getHeader(protocolHeader); - if (protocolHeaderValue != null && protocolHeaderHttpsValue.equalsIgnoreCase(protocolHeaderValue)) { + if (protocolHeaderValue == null) { + // don't modify the secure,scheme and serverPort attributes of the request + } else if (protocolHeaderHttpsValue.equalsIgnoreCase(protocolHeaderValue)) { xRequest.setSecure(true); xRequest.setScheme("https"); xRequest.setServerPort(httpsServerPort); + } else { + xRequest.setSecure(false); + xRequest.setScheme("http"); + xRequest.setServerPort(httpServerPort); } } @@ -832,17 +858,38 @@ public class RemoteIpFilter implements Filter { setTrustedProxies(filterConfig.getInitParameter(TRUSTED_PROXIES_PARAMETER)); } + if (filterConfig.getInitParameter(HTTP_SERVER_PORT_PARAMETER) != null) { + try { + setHttpServerPort(Integer.parseInt(filterConfig.getInitParameter(HTTP_SERVER_PORT_PARAMETER))); + } catch (NumberFormatException e) { + throw new NumberFormatException("Illegal " + HTTP_SERVER_PORT_PARAMETER + " : " + e.getMessage()); + } + } + if (filterConfig.getInitParameter(HTTPS_SERVER_PORT_PARAMETER) != null) { try { setHttpsServerPort(Integer.parseInt(filterConfig.getInitParameter(HTTPS_SERVER_PORT_PARAMETER))); } catch (NumberFormatException e) { - throw new NumberFormatException("Illegal serverPort : " + e.getMessage()); + throw new NumberFormatException("Illegal " + HTTPS_SERVER_PORT_PARAMETER + " : " + e.getMessage()); } } } /** *
+ * Server Port value if the {@link #protocolHeader} indicates HTTP (i.e. {@link #protocolHeader} is not null and + * has a value different of {@link #protocolHeaderHttpsValue}). + *
+ *+ * Default value : 80 + *
+ */ + public void setHttpServerPort(int httpServerPort) { + this.httpServerPort = httpServerPort; + } + + /** + ** Server Port value if the {@link #protocolHeader} indicates HTTPS *
*diff --git a/webapps/docs/config/filter.xml b/webapps/docs/config/filter.xml index f28ecf31b..1b32abb8a 100644 --- a/webapps/docs/config/filter.xml +++ b/webapps/docs/config/filter.xml @@ -205,8 +205,9 @@ via a request headers (e.g. "X-Forwarded-For").
Another feature of this filter is to replace the apparent scheme - (http/https) and server port with the scheme presented by a proxy or a load - balancer via a request header (e.g. "X-Forwarded-Proto").
+ (http/https), server port andrequest.secure with the scheme presented
+ by a proxy or a load balancer via a request header
+ (e.g. "X-Forwarded-Proto").
If used in conjunction with Remote Address/Host filters then this filter should be defined first to ensure that the correct client IP address is @@ -272,6 +273,20 @@ used.
+Value returned by ServletRequest.getServerPort()
+ when the protocolHeader indicates http
+ protocol. If not specified, the default of 80 is
+ used.
Value returned by ServletRequest.getServerPort()
+ when the protocolHeader indicates https
+ protocol. If not specified, the default of 443 is
+ used.