Unverified Commit d9768b53 by Stefan Fussenegger Committed by Spencer Gibb

Improved zuul proxy header support.

- add zuul.add-host-header property to add Host header - add port to X-Forwarded-Host as defined in RFC 7239 - extract PreDecorationFilter.filterOrder() into a public constant fixes gh-1108
parent 34befdf1
......@@ -83,6 +83,11 @@ public class ZuulProperties {
private boolean addProxyHeaders = true;
/**
* Flag to determine whether the proxy forwards the Host header.
*/
private boolean addHostHeader = false;
/**
* Set of service names not to consider for proxying automatically. By default all
* services in the discovery client will be proxied.
*/
......
......@@ -19,6 +19,8 @@ package org.springframework.cloud.netflix.zuul.filters.pre;
import java.net.MalformedURLException;
import java.net.URL;
import javax.servlet.http.HttpServletRequest;
import org.springframework.cloud.netflix.zuul.filters.ProxyRequestHelper;
import org.springframework.cloud.netflix.zuul.filters.Route;
import org.springframework.cloud.netflix.zuul.filters.RouteLocator;
......@@ -36,6 +38,8 @@ import lombok.extern.apachecommons.CommonsLog;
@CommonsLog
public class PreDecorationFilter extends ZuulFilter {
public static final int FILTER_ORDER = 5;
private RouteLocator routeLocator;
private String dispatcherServletPath;
......@@ -58,7 +62,7 @@ public class PreDecorationFilter extends ZuulFilter {
@Override
public int filterOrder() {
return 5;
return FILTER_ORDER;
}
@Override
......@@ -115,8 +119,7 @@ public class PreDecorationFilter extends ZuulFilter {
ctx.addOriginResponseHeader("X-Zuul-ServiceId", location);
}
if (this.properties.isAddProxyHeaders()) {
ctx.addZuulRequestHeader("X-Forwarded-Host",
ctx.getRequest().getServerName());
ctx.addZuulRequestHeader("X-Forwarded-Host", toHostHeader(ctx.getRequest()));
ctx.addZuulRequestHeader("X-Forwarded-Port",
String.valueOf(ctx.getRequest().getServerPort()));
ctx.addZuulRequestHeader(ZuulHeaders.X_FORWARDED_PROTO,
......@@ -149,6 +152,9 @@ public class PreDecorationFilter extends ZuulFilter {
}
ctx.addZuulRequestHeader("X-Forwarded-For", xforwardedfor);
}
if (this.properties.isAddHostHeader()) {
ctx.addZuulRequestHeader("Host", toHostHeader(ctx.getRequest()));
}
}
}
else {
......@@ -182,6 +188,15 @@ public class PreDecorationFilter extends ZuulFilter {
return null;
}
private String toHostHeader(HttpServletRequest request) {
int port = request.getServerPort();
if ((port == 80 && "http".equals(request.getScheme())) || (port == 443 && "https".equals(request.getScheme()))) {
return request.getServerName();
} else {
return request.getServerName() + ":" + port;
}
}
private URL getUrl(String target) {
try {
return new URL(target);
......
......@@ -91,6 +91,34 @@ public class PreDecorationFilterTests {
}
@Test
public void xForwardedHostHasPort() throws Exception {
this.properties.setPrefix("/api");
this.request.setRequestURI("/api/foo/1");
this.request.setRemoteAddr("5.6.7.8");
this.request.setServerPort(8080);
this.routeLocator.addRoute(
new ZuulRoute("foo", "/foo/**", "foo", null, false, null, null));
this.filter.run();
RequestContext ctx = RequestContext.getCurrentContext();
assertEquals("localhost:8080", ctx.getZuulRequestHeaders().get("x-forwarded-host"));
}
@Test
public void hostHeaderSet() throws Exception {
this.properties.setPrefix("/api");
this.properties.setAddHostHeader(true);
this.request.setRequestURI("/api/foo/1");
this.request.setRemoteAddr("5.6.7.8");
this.request.setServerPort(8080);
this.routeLocator.addRoute(
new ZuulRoute("foo", "/foo/**", "foo", null, false, null, null));
this.filter.run();
RequestContext ctx = RequestContext.getCurrentContext();
assertEquals("localhost:8080", ctx.getZuulRequestHeaders().get("x-forwarded-host"));
assertEquals("localhost:8080", ctx.getZuulRequestHeaders().get("host"));
}
@Test
public void prefixRouteAddsHeader() throws Exception {
this.properties.setPrefix("/api");
this.properties.setStripPrefix(true);
......
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