diff --git a/spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/zuul/ZuulProxyConfiguration.java b/spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/zuul/ZuulProxyConfiguration.java index 78c679a6..02ca6dc7 100644 --- a/spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/zuul/ZuulProxyConfiguration.java +++ b/spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/zuul/ZuulProxyConfiguration.java @@ -112,6 +112,7 @@ public class ZuulProxyConfiguration extends ZuulConfiguration { helper.setTraces(this.traces); } helper.setIgnoredHeaders(this.zuulProperties.getIgnoredHeaders()); + helper.setTraceRequestBody(this.zuulProperties.isTraceRequestBody()); return helper; } diff --git a/spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/zuul/filters/ProxyRequestHelper.java b/spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/zuul/filters/ProxyRequestHelper.java index 0251cc11..e5db5a8a 100644 --- a/spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/zuul/filters/ProxyRequestHelper.java +++ b/spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/zuul/filters/ProxyRequestHelper.java @@ -34,6 +34,7 @@ import java.util.Set; import javax.servlet.http.HttpServletRequest; import org.springframework.boot.actuate.trace.TraceRepository; +import org.springframework.cloud.netflix.zuul.util.RequestUtils; import org.springframework.http.HttpHeaders; import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; @@ -69,6 +70,8 @@ public class ProxyRequestHelper { private Set whitelistHosts = new LinkedHashSet<>(); + private boolean traceRequestBody = true; + public void setWhitelistHosts(Set whitelistHosts) { this.whitelistHosts.addAll(whitelistHosts); } @@ -85,6 +88,10 @@ public class ProxyRequestHelper { this.traces = traces; } + public void setTraceRequestBody(boolean traceRequestBody) { + this.traceRequestBody = traceRequestBody; + } + public String buildZuulRequestURI(HttpServletRequest request) { RequestContext context = RequestContext.getCurrentContext(); String uri = request.getRequestURI(); @@ -225,7 +232,7 @@ public class ProxyRequestHelper { public Map debug(String verb, String uri, MultiValueMap headers, MultiValueMap params, InputStream requestEntity) throws IOException { - Map info = new LinkedHashMap(); + Map info = new LinkedHashMap<>(); if (this.traces != null) { RequestContext context = RequestContext.getCurrentContext(); info.put("method", verb); @@ -233,8 +240,8 @@ public class ProxyRequestHelper { info.put("query", getQueryString(params)); info.put("remote", true); info.put("proxy", context.get("proxy")); - Map trace = new LinkedHashMap(); - Map input = new LinkedHashMap(); + Map trace = new LinkedHashMap<>(); + Map input = new LinkedHashMap<>(); trace.put("request", input); info.put("headers", trace); for (Entry> entry : headers.entrySet()) { @@ -258,9 +265,9 @@ public class ProxyRequestHelper { return info; } - private boolean shouldDebugBody(RequestContext ctx) { + /* for tests */ boolean shouldDebugBody(RequestContext ctx) { HttpServletRequest request = ctx.getRequest(); - if (ctx.isChunkedRequestBody()) { + if (!this.traceRequestBody || ctx.isChunkedRequestBody() || RequestUtils.isZuulServletRequest()) { return false; } if (request == null || request.getContentType() == null) { diff --git a/spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/zuul/filters/ZuulProperties.java b/spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/zuul/filters/ZuulProperties.java index 9b61d645..94ff90a2 100644 --- a/spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/zuul/filters/ZuulProperties.java +++ b/spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/zuul/filters/ZuulProperties.java @@ -72,6 +72,8 @@ public class ZuulProperties { private Host host = new Host(); + private boolean traceRequestBody = true; + public Set getIgnoredHeaders() { Set ignoredHeaders = new LinkedHashSet<>(this.ignoredHeaders); if (ClassUtils.isPresent( diff --git a/spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/zuul/filters/ProxyRequestHelperTests.java b/spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/zuul/filters/ProxyRequestHelperTests.java index 48d5f22d..b2c5539c 100644 --- a/spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/zuul/filters/ProxyRequestHelperTests.java +++ b/spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/zuul/filters/ProxyRequestHelperTests.java @@ -26,6 +26,7 @@ import org.springframework.boot.actuate.trace.InMemoryTraceRepository; import org.springframework.boot.actuate.trace.Trace; import org.springframework.boot.actuate.trace.TraceRepository; import org.springframework.http.HttpHeaders; +import org.springframework.http.MediaType; import org.springframework.mock.web.MockHttpServletRequest; import org.springframework.mock.web.MockHttpServletResponse; import org.springframework.util.LinkedMultiValueMap; @@ -85,6 +86,85 @@ public class ProxyRequestHelperTests { } + @Test + public void shouldDebugBodyDisabled() throws Exception { + RequestContext context = RequestContext.getCurrentContext(); + + ProxyRequestHelper helper = new ProxyRequestHelper(); + helper.setTraceRequestBody(false); + + assertThat("shouldDebugBody wrong", helper.shouldDebugBody(context), is(false)); + } + + @Test + public void shouldDebugBodyChunked() throws Exception { + MockHttpServletRequest request = new MockHttpServletRequest("POST", "/"); + RequestContext context = RequestContext.getCurrentContext(); + context.setChunkedRequestBody(); + context.setRequest(request); + + ProxyRequestHelper helper = new ProxyRequestHelper(); + + assertThat("shouldDebugBody wrong", helper.shouldDebugBody(context), is(false)); + } + + @Test + public void shouldDebugBodyServlet() throws Exception { + MockHttpServletRequest request = new MockHttpServletRequest("POST", "/"); + RequestContext context = RequestContext.getCurrentContext(); + context.setZuulEngineRan(); + context.setRequest(request); + + ProxyRequestHelper helper = new ProxyRequestHelper(); + + assertThat("shouldDebugBody wrong", helper.shouldDebugBody(context), is(false)); + } + + @Test + public void shouldDebugBodyNullContentType() throws Exception { + MockHttpServletRequest request = new MockHttpServletRequest("POST", "/"); + request.setContentType(null); + RequestContext context = RequestContext.getCurrentContext(); + context.setRequest(request); + + ProxyRequestHelper helper = new ProxyRequestHelper(); + + assertThat("shouldDebugBody wrong", helper.shouldDebugBody(context), is(true)); + } + + @Test + public void shouldDebugBodyNullRequest() throws Exception { + RequestContext context = RequestContext.getCurrentContext(); + + ProxyRequestHelper helper = new ProxyRequestHelper(); + + assertThat("shouldDebugBody wrong", helper.shouldDebugBody(context), is(true)); + } + + @Test + public void shouldDebugBodyNotMultitypeContentType() throws Exception { + MockHttpServletRequest request = new MockHttpServletRequest("POST", "/"); + request.setContentType(MediaType.APPLICATION_JSON_VALUE); + RequestContext context = RequestContext.getCurrentContext(); + context.setRequest(request); + + ProxyRequestHelper helper = new ProxyRequestHelper(); + + assertThat("shouldDebugBody wrong", helper.shouldDebugBody(context), is(true)); + } + + @Test + public void shouldDebugBodyMultitypeContentType() throws Exception { + MockHttpServletRequest request = new MockHttpServletRequest("POST", "/"); + request.setContentType(MediaType.MULTIPART_FORM_DATA_VALUE); + RequestContext context = RequestContext.getCurrentContext(); + context.setRequest(request); + + ProxyRequestHelper helper = new ProxyRequestHelper(); + + assertThat("shouldDebugBody wrong", helper.shouldDebugBody(context), is(false)); + } + @Test public void buildZuulRequestHeadersWork() { MockHttpServletRequest request = new MockHttpServletRequest("GET", "/");