Earlier processing of forwarded headers

Forwarded headers are now processed before ServerWebExchange is created
through ForwardedHeaderTransformer which has the same logic as the
ForwardedHeaderFilter but works on the request only.

ForwardedHeaderFilter is deprecated as of 5.1 but if registered it is
removed from the list of filters and ForwardedHeaderTransformer is used
instead.

Issue: SPR-17072
This commit is contained in:
Rossen Stoyanchev
2018-08-03 14:41:33 +03:00
parent 0878e438e5
commit a8a1fc6de5
10 changed files with 473 additions and 298 deletions

View File

@@ -16,6 +16,7 @@
package org.springframework.web.reactive.result.method.annotation;
import java.net.URI;
import java.time.Duration;
import org.junit.Test;
@@ -23,11 +24,16 @@ import org.reactivestreams.Publisher;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpHeaders;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.reactive.config.EnableWebFlux;
import org.springframework.web.server.adapter.ForwardedHeaderTransformer;
import static org.junit.Assert.*;
@@ -46,7 +52,7 @@ public class RequestMappingIntegrationTests extends AbstractRequestMappingIntegr
@Override
protected ApplicationContext initApplicationContext() {
AnnotationConfigApplicationContext wac = new AnnotationConfigApplicationContext();
wac.register(WebConfig.class, TestRestController.class);
wac.register(WebConfig.class, TestRestController.class, LocalConfig.class);
wac.refresh();
return wac;
}
@@ -62,6 +68,20 @@ public class RequestMappingIntegrationTests extends AbstractRequestMappingIntegr
assertEquals(3, headers.getContentLength());
}
@Test
public void forwardedHeaders() {
// One integration test to verify triggering of Forwarded header support.
// More fine-grained tests in ForwardedHeaderTransformerTests.
RequestEntity<Void> request = RequestEntity
.get(URI.create("http://localhost:" + this.port + "/uri"))
.header("Forwarded", "host=84.198.58.199;proto=https")
.build();
ResponseEntity<String> entity = getRestTemplate().exchange(request, String.class);
assertEquals("https://84.198.58.199/uri", entity.getBody());
}
@Test
public void stream() throws Exception {
String[] expected = {"0", "1", "2", "3", "4"};
@@ -84,10 +104,25 @@ public class RequestMappingIntegrationTests extends AbstractRequestMappingIntegr
return "Foo";
}
@GetMapping("/uri")
public String uri(ServerHttpRequest request) {
return request.getURI().toString();
}
@GetMapping("/stream")
public Publisher<Long> stream() {
return testInterval(Duration.ofMillis(50), 5);
}
}
@Configuration
static class LocalConfig {
@Bean
public ForwardedHeaderTransformer forwardedHeaderTransformer() {
return new ForwardedHeaderTransformer();
}
}
}