Commit c35105b8 authored by Phillip Webb's avatar Phillip Webb

Add X-Forwarded-For header support to Undertow

Add a `useForwardHeaders` property to allow embedded Undertow
containers to respect X-Forwarded-For headers.

Fixes gh-3881
parent 7f976819
......@@ -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) {
......
......@@ -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.
*/
......
......@@ -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
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment