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
pull/4057/merge
Phillip Webb 9 years ago
parent 7f97681969
commit c35105b868

@ -71,6 +71,8 @@ public class UndertowEmbeddedServletContainer implements EmbeddedServletContaine
private final String contextPath; private final String contextPath;
private final boolean useForwardHeaders;
private final boolean autoStart; private final boolean autoStart;
private final Compression compression; private final Compression compression;
@ -81,9 +83,16 @@ public class UndertowEmbeddedServletContainer implements EmbeddedServletContaine
public UndertowEmbeddedServletContainer(Builder builder, DeploymentManager manager, public UndertowEmbeddedServletContainer(Builder builder, DeploymentManager manager,
String contextPath, int port, boolean autoStart, Compression compression) { 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.builder = builder;
this.manager = manager; this.manager = manager;
this.contextPath = contextPath; this.contextPath = contextPath;
this.useForwardHeaders = useForwardHeaders;
this.autoStart = autoStart; this.autoStart = autoStart;
this.compression = compression; this.compression = compression;
} }
@ -105,7 +114,11 @@ public class UndertowEmbeddedServletContainer implements EmbeddedServletContaine
private Undertow createUndertowServer() { private Undertow createUndertowServer() {
try { try {
HttpHandler httpHandler = this.manager.start(); 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(); return this.builder.build();
} }
catch (ServletException ex) { catch (ServletException ex) {

@ -121,6 +121,8 @@ public class UndertowEmbeddedServletContainerFactory extends
private boolean accessLogEnabled = false; private boolean accessLogEnabled = false;
private boolean useForwardHeaders;
/** /**
* Create a new {@link UndertowEmbeddedServletContainerFactory} instance. * Create a new {@link UndertowEmbeddedServletContainerFactory} instance.
*/ */
@ -220,7 +222,7 @@ public class UndertowEmbeddedServletContainerFactory extends
int port = getPort(); int port = getPort();
Builder builder = createBuilder(port); Builder builder = createBuilder(port);
return new UndertowEmbeddedServletContainer(builder, manager, getContextPath(), return new UndertowEmbeddedServletContainer(builder, manager, getContextPath(),
port, port >= 0, getCompression()); port, this.useForwardHeaders, port >= 0, getCompression());
} }
private Builder createBuilder(int port) { private Builder createBuilder(int port) {
@ -519,6 +521,15 @@ public class UndertowEmbeddedServletContainerFactory extends
return this.accessLogEnabled; 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. * Undertow {@link ResourceManager} for JAR resources.
*/ */

@ -149,6 +149,13 @@ public class UndertowEmbeddedServletContainerFactoryTests extends
assertEquals("/", contextPath.get()); assertEquals("/", contextPath.get());
} }
@Test
public void useForwardHeaders() throws Exception {
UndertowEmbeddedServletContainerFactory factory = getFactory();
factory.setUseForwardHeaders(true);
assertForwardHeaderIsUsed(factory);
}
@Override @Override
protected Object getJspServlet() { protected Object getJspServlet() {
return null; // Undertow does not support JSPs return null; // Undertow does not support JSPs

Loading…
Cancel
Save