Unverified Commit beb84dbe by Spencer Gibb

Merge branch 'molindo-1108-proxy-headers'

parents 34befdf1 d9768b53
...@@ -83,6 +83,11 @@ public class ZuulProperties { ...@@ -83,6 +83,11 @@ public class ZuulProperties {
private boolean addProxyHeaders = true; 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 * Set of service names not to consider for proxying automatically. By default all
* services in the discovery client will be proxied. * services in the discovery client will be proxied.
*/ */
......
...@@ -19,6 +19,8 @@ package org.springframework.cloud.netflix.zuul.filters.pre; ...@@ -19,6 +19,8 @@ package org.springframework.cloud.netflix.zuul.filters.pre;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.URL; import java.net.URL;
import javax.servlet.http.HttpServletRequest;
import org.springframework.cloud.netflix.zuul.filters.ProxyRequestHelper; import org.springframework.cloud.netflix.zuul.filters.ProxyRequestHelper;
import org.springframework.cloud.netflix.zuul.filters.Route; import org.springframework.cloud.netflix.zuul.filters.Route;
import org.springframework.cloud.netflix.zuul.filters.RouteLocator; import org.springframework.cloud.netflix.zuul.filters.RouteLocator;
...@@ -36,6 +38,8 @@ import lombok.extern.apachecommons.CommonsLog; ...@@ -36,6 +38,8 @@ import lombok.extern.apachecommons.CommonsLog;
@CommonsLog @CommonsLog
public class PreDecorationFilter extends ZuulFilter { public class PreDecorationFilter extends ZuulFilter {
public static final int FILTER_ORDER = 5;
private RouteLocator routeLocator; private RouteLocator routeLocator;
private String dispatcherServletPath; private String dispatcherServletPath;
...@@ -58,7 +62,7 @@ public class PreDecorationFilter extends ZuulFilter { ...@@ -58,7 +62,7 @@ public class PreDecorationFilter extends ZuulFilter {
@Override @Override
public int filterOrder() { public int filterOrder() {
return 5; return FILTER_ORDER;
} }
@Override @Override
...@@ -115,8 +119,7 @@ public class PreDecorationFilter extends ZuulFilter { ...@@ -115,8 +119,7 @@ public class PreDecorationFilter extends ZuulFilter {
ctx.addOriginResponseHeader("X-Zuul-ServiceId", location); ctx.addOriginResponseHeader("X-Zuul-ServiceId", location);
} }
if (this.properties.isAddProxyHeaders()) { if (this.properties.isAddProxyHeaders()) {
ctx.addZuulRequestHeader("X-Forwarded-Host", ctx.addZuulRequestHeader("X-Forwarded-Host", toHostHeader(ctx.getRequest()));
ctx.getRequest().getServerName());
ctx.addZuulRequestHeader("X-Forwarded-Port", ctx.addZuulRequestHeader("X-Forwarded-Port",
String.valueOf(ctx.getRequest().getServerPort())); String.valueOf(ctx.getRequest().getServerPort()));
ctx.addZuulRequestHeader(ZuulHeaders.X_FORWARDED_PROTO, ctx.addZuulRequestHeader(ZuulHeaders.X_FORWARDED_PROTO,
...@@ -149,6 +152,9 @@ public class PreDecorationFilter extends ZuulFilter { ...@@ -149,6 +152,9 @@ public class PreDecorationFilter extends ZuulFilter {
} }
ctx.addZuulRequestHeader("X-Forwarded-For", xforwardedfor); ctx.addZuulRequestHeader("X-Forwarded-For", xforwardedfor);
} }
if (this.properties.isAddHostHeader()) {
ctx.addZuulRequestHeader("Host", toHostHeader(ctx.getRequest()));
}
} }
} }
else { else {
...@@ -182,6 +188,15 @@ public class PreDecorationFilter extends ZuulFilter { ...@@ -182,6 +188,15 @@ public class PreDecorationFilter extends ZuulFilter {
return null; 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) { private URL getUrl(String target) {
try { try {
return new URL(target); return new URL(target);
......
...@@ -91,6 +91,34 @@ public class PreDecorationFilterTests { ...@@ -91,6 +91,34 @@ public class PreDecorationFilterTests {
} }
@Test @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 { public void prefixRouteAddsHeader() throws Exception {
this.properties.setPrefix("/api"); this.properties.setPrefix("/api");
this.properties.setStripPrefix(true); 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