diff --git a/spring-boot/src/main/java/org/springframework/boot/context/embedded/undertow/UndertowEmbeddedServletContainer.java b/spring-boot/src/main/java/org/springframework/boot/context/embedded/undertow/UndertowEmbeddedServletContainer.java index 7833c98e79..f6c0471aae 100644 --- a/spring-boot/src/main/java/org/springframework/boot/context/embedded/undertow/UndertowEmbeddedServletContainer.java +++ b/spring-boot/src/main/java/org/springframework/boot/context/embedded/undertow/UndertowEmbeddedServletContainer.java @@ -71,6 +71,8 @@ public class UndertowEmbeddedServletContainer implements EmbeddedServletContaine private final String contextPath; + private final boolean useForwardHeaders; + private final boolean autoStart; private final Compression compression; @@ -81,9 +83,16 @@ public class UndertowEmbeddedServletContainer implements EmbeddedServletContaine public UndertowEmbeddedServletContainer(Builder builder, DeploymentManager manager, String contextPath, int port, boolean autoStart, Compression compression) { + this(builder, manager, contextPath, port, false, autoStart, compression); + } + + public UndertowEmbeddedServletContainer(Builder builder, DeploymentManager manager, + String contextPath, int port, boolean useForwardHeaders, boolean autoStart, + Compression compression) { this.builder = builder; this.manager = manager; this.contextPath = contextPath; + this.useForwardHeaders = useForwardHeaders; this.autoStart = autoStart; this.compression = compression; } @@ -105,7 +114,11 @@ public class UndertowEmbeddedServletContainer implements EmbeddedServletContaine private Undertow createUndertowServer() { try { HttpHandler httpHandler = this.manager.start(); - this.builder.setHandler(getContextHandler(httpHandler)); + httpHandler = getContextHandler(httpHandler); + if (this.useForwardHeaders) { + httpHandler = Handlers.proxyPeerAddress(httpHandler); + } + this.builder.setHandler(httpHandler); return this.builder.build(); } catch (ServletException ex) { diff --git a/spring-boot/src/main/java/org/springframework/boot/context/embedded/undertow/UndertowEmbeddedServletContainerFactory.java b/spring-boot/src/main/java/org/springframework/boot/context/embedded/undertow/UndertowEmbeddedServletContainerFactory.java index e0b181c2cf..899a40d3e3 100644 --- a/spring-boot/src/main/java/org/springframework/boot/context/embedded/undertow/UndertowEmbeddedServletContainerFactory.java +++ b/spring-boot/src/main/java/org/springframework/boot/context/embedded/undertow/UndertowEmbeddedServletContainerFactory.java @@ -121,6 +121,8 @@ public class UndertowEmbeddedServletContainerFactory extends private boolean accessLogEnabled = false; + private boolean useForwardHeaders; + /** * Create a new {@link UndertowEmbeddedServletContainerFactory} instance. */ @@ -220,7 +222,7 @@ public class UndertowEmbeddedServletContainerFactory extends int port = getPort(); Builder builder = createBuilder(port); return new UndertowEmbeddedServletContainer(builder, manager, getContextPath(), - port, port >= 0, getCompression()); + port, this.useForwardHeaders, port >= 0, getCompression()); } private Builder createBuilder(int port) { @@ -519,6 +521,15 @@ public class UndertowEmbeddedServletContainerFactory extends return this.accessLogEnabled; } + /** + * Set if x-forward-* headers should be processed. + * @param useForwardHeaders if x-forward headers should be used + * @since 1.3.0 + */ + public void setUseForwardHeaders(boolean useForwardHeaders) { + this.useForwardHeaders = useForwardHeaders; + } + /** * Undertow {@link ResourceManager} for JAR resources. */ diff --git a/spring-boot/src/test/java/org/springframework/boot/context/embedded/undertow/UndertowEmbeddedServletContainerFactoryTests.java b/spring-boot/src/test/java/org/springframework/boot/context/embedded/undertow/UndertowEmbeddedServletContainerFactoryTests.java index ea2b43919b..140e4f3cea 100644 --- a/spring-boot/src/test/java/org/springframework/boot/context/embedded/undertow/UndertowEmbeddedServletContainerFactoryTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/context/embedded/undertow/UndertowEmbeddedServletContainerFactoryTests.java @@ -149,6 +149,13 @@ public class UndertowEmbeddedServletContainerFactoryTests extends assertEquals("/", contextPath.get()); } + @Test + public void useForwardHeaders() throws Exception { + UndertowEmbeddedServletContainerFactory factory = getFactory(); + factory.setUseForwardHeaders(true); + assertForwardHeaderIsUsed(factory); + } + @Override protected Object getJspServlet() { return null; // Undertow does not support JSPs