Skip to content

Commit 2583f80

Browse files
author
Dave Syer
committed
Enable Tomcat RemoteIpValve by default
Fixes spring-projectsgh-3782
1 parent 8543a3c commit 2583f80

File tree

2 files changed

+32
-12
lines changed

2 files changed

+32
-12
lines changed

spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ServerProperties.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ public void customize(ConfigurableEmbeddedServletContainer container) {
283283
}
284284
if (container instanceof TomcatEmbeddedServletContainerFactory) {
285285
getTomcat()
286-
.customizeTomcat((TomcatEmbeddedServletContainerFactory) container);
286+
.customizeTomcat((TomcatEmbeddedServletContainerFactory) container);
287287
}
288288
if (container instanceof UndertowEmbeddedServletContainerFactory) {
289289
getUndertow().customizeUndertow(
@@ -496,13 +496,13 @@ public static class Tomcat {
496496
/**
497497
* Name of the HTTP header used to override the original port value.
498498
*/
499-
private String portHeader;
499+
private String portHeader = "x-forwarded-port";
500500

501501
/**
502502
* Name of the http header from which the remote ip is extracted. Configured as a
503503
* RemoteIpValve only if remoteIpHeader is also set.
504504
*/
505-
private String remoteIpHeader;
505+
private String remoteIpHeader = "x-forwarded-for";
506506

507507
/**
508508
* Tomcat base directory. If not specified a temporary directory will be used.
@@ -691,13 +691,16 @@ private void customizeHeaders(TomcatEmbeddedServletContainerFactory factory) {
691691
String remoteIpHeader = getRemoteIpHeader();
692692
String protocolHeader = getProtocolHeader();
693693
if (StringUtils.hasText(remoteIpHeader)
694-
|| StringUtils.hasText(protocolHeader)) {
694+
&& StringUtils.hasText(protocolHeader)) {
695695
RemoteIpValve valve = new RemoteIpValve();
696696
valve.setRemoteIpHeader(remoteIpHeader);
697697
valve.setProtocolHeader(protocolHeader);
698+
// The internal proxies default to a white list of "safe" internal IP
699+
// addresses
698700
valve.setInternalProxies(getInternalProxies());
699701
valve.setPortHeader(getPortHeader());
700702
valve.setProtocolHeaderHttpsValue(getProtocolHeaderHttpsValue());
703+
// ... so it's safe to add this valve by default.
701704
factory.addContextValves(valve);
702705
}
703706
}
@@ -1012,7 +1015,7 @@ public void setDir(File dir) {
10121015
* configuration.
10131016
*/
10141017
private static class SessionConfiguringInitializer implements
1015-
ServletContextInitializer {
1018+
ServletContextInitializer {
10161019

10171020
private final Session session;
10181021

spring-boot-docs/src/main/asciidoc/howto.adoc

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -525,11 +525,24 @@ HTTPS connector:
525525

526526
[[howto-use-tomcat-behind-a-proxy-server]]
527527
=== Use Tomcat behind a front-end proxy server
528-
Spring Boot will automatically configure Tomcat's `RemoteIpValve` if you enable it. This
529-
allows you to transparently use the standard `x-forwarded-for` and `x-forwarded-proto`
530-
headers that most front-end proxy servers add. The valve is switched on by setting one or
531-
both of these properties to something non-empty (these are the conventional values used by
532-
most proxies, and if you only set one the other will be set automatically):
528+
Your app might need to send 302 redirects, or render UI templates with
529+
absolute links to itself, or hypermedia links back to itself in the
530+
case of a RESTful service. If the app is behind a proxy, the caller
531+
wants a link to the proxy not to the physical address of the app, so
532+
something has to be done in the backend. Typically this is handled via
533+
a contract with the proxy, which will add headers to tell the back end
534+
how to construct links to itself. If the proxy adds conventional
535+
headers (most do this out of the box) the absolute links should be
536+
rendered correctly by default using the Tomcat server.
537+
538+
Spring Boot using Tomcat automatically adds a `RemoteIpValve`. This
539+
transparently takes the standard `x-forwarded-for` and
540+
`x-forwarded-proto` headers and uses them to change local URLs created
541+
in the `HttpServletRequest`. You can configure the header names in
542+
Spring Boot and the valve is switched on unless one or both of these
543+
properties is empty. These values are the defaults and are the
544+
conventional values used by most proxies, so you don't need to set
545+
them unless you need different values:
533546

534547
[indent=0]
535548
----
@@ -560,8 +573,12 @@ NOTE: The double backslashes are only required when you're using a properties fi
560573
configuration. If you are using YAML, single backslashes are sufficient and a value
561574
that's equivalent to the one shown above would be `192\.168\.\d{1,3}\.\d{1,3}`.
562575

563-
Alternatively, you can take complete control of the configuration of the `RemoteIpValve`
564-
by configuring and adding it in a `TomcatEmbeddedServletContainerFactory` bean.
576+
NOTE: You can trust all proxies by setting the `internal_proxies` to empty (but don't do this in production).
577+
578+
You can take complete control of the configuration of the
579+
`RemoteIpValve` by switching the automatic one off (i.e. set one of
580+
the headers to empty) and adding a new valve instance in a
581+
`TomcatEmbeddedServletContainerFactory` bean.
565582

566583

567584

0 commit comments

Comments
 (0)