Browse Source

CORS support in HTTP header predicate

This commit introduces CORS support for the HeadersPredicate in
WebMvc.fn and WebFlux.fn.

Closes gh-24564
pull/24674/head
Arjen Poutsma 5 years ago
parent
commit
61569003b5
  1. 7
      spring-webflux/src/main/java/org/springframework/web/reactive/function/server/RequestPredicates.java
  2. 12
      spring-webflux/src/test/java/org/springframework/web/reactive/function/server/RequestPredicatesTests.java
  3. 7
      spring-webmvc/src/main/java/org/springframework/web/servlet/function/RequestPredicates.java
  4. 10
      spring-webmvc/src/test/java/org/springframework/web/servlet/function/RequestPredicatesTests.java

7
spring-webflux/src/main/java/org/springframework/web/reactive/function/server/RequestPredicates.java

@ -550,7 +550,12 @@ public abstract class RequestPredicates { @@ -550,7 +550,12 @@ public abstract class RequestPredicates {
@Override
public boolean test(ServerRequest request) {
return this.headersPredicate.test(request.headers());
if (CorsUtils.isPreFlightRequest(request.exchange().getRequest())) {
return true;
}
else {
return this.headersPredicate.test(request.headers());
}
}
@Override

12
spring-webflux/src/test/java/org/springframework/web/reactive/function/server/RequestPredicatesTests.java

@ -198,6 +198,18 @@ public class RequestPredicatesTests { @@ -198,6 +198,18 @@ public class RequestPredicatesTests {
assertThat(predicate.test(request)).isFalse();
}
@Test
public void headersCors() {
RequestPredicate predicate = RequestPredicates.headers(headers -> false);
MockServerHttpRequest mockRequest = MockServerHttpRequest.options("https://example.com")
.header("Origin", "https://example.com")
.header(HttpHeaders.ACCESS_CONTROL_REQUEST_METHOD, "PUT")
.build();
ServerRequest request = new DefaultServerRequest(MockServerWebExchange.from(mockRequest), Collections.emptyList());
assertThat(predicate.test(request)).isTrue();
}
@Test
public void contentType() {
MediaType json = MediaType.APPLICATION_JSON;

7
spring-webmvc/src/main/java/org/springframework/web/servlet/function/RequestPredicates.java

@ -543,7 +543,12 @@ public abstract class RequestPredicates { @@ -543,7 +543,12 @@ public abstract class RequestPredicates {
@Override
public boolean test(ServerRequest request) {
return this.headersPredicate.test(request.headers());
if (CorsUtils.isPreFlightRequest(request.servletRequest())) {
return true;
}
else {
return this.headersPredicate.test(request.headers());
}
}
@Override

10
spring-webmvc/src/test/java/org/springframework/web/servlet/function/RequestPredicatesTests.java

@ -197,6 +197,16 @@ public class RequestPredicatesTests { @@ -197,6 +197,16 @@ public class RequestPredicatesTests {
assertThat(predicate.test(request)).isFalse();
}
@Test
public void headersCors() {
RequestPredicate predicate = RequestPredicates.headers(headers -> false);
MockHttpServletRequest servletRequest = new MockHttpServletRequest("OPTIONS", "https://example.com");
servletRequest.addHeader("Origin", "https://example.com");
servletRequest.addHeader(HttpHeaders.ACCESS_CONTROL_REQUEST_METHOD, "PUT");
ServerRequest request = new DefaultServerRequest(servletRequest, emptyList());
assertThat(predicate.test(request)).isTrue();
}
@Test
public void contentType() {
MediaType json = MediaType.APPLICATION_JSON;

Loading…
Cancel
Save