Refine CORS preflight requests handling with no configuration
This commit makes CORS preflight requests handling more flexible by just skipping setting CORS response headers when no configuration is defined instead of rejecting them. That will have the same effect on user agent side (the preflight request will be considered as not authorized and the actual request not performed) but is more flexible and more efficient. Closes gh-31839
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2024 the original author or authors.
|
||||
* Copyright 2002-2025 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -31,7 +31,6 @@ import reactor.test.StepVerifier;
|
||||
import org.springframework.core.annotation.AnnotatedElementUtils;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.server.PathContainer;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.CrossOrigin;
|
||||
@@ -124,7 +123,10 @@ class HandlerMethodMappingTests {
|
||||
|
||||
this.mapping.getHandler(exchange).block();
|
||||
|
||||
assertThat(exchange.getResponse().getStatusCode()).isEqualTo(HttpStatus.FORBIDDEN);
|
||||
MockServerHttpResponse response = exchange.getResponse();
|
||||
assertThat(response.getStatusCode()).isNull();
|
||||
assertThat(response.getHeaders().getAccessControlAllowOrigin()).isNull();
|
||||
assertThat(response.getHeaders().getAccessControlAllowMethods()).isEmpty();
|
||||
}
|
||||
|
||||
@Test // gh-26490
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2021 the original author or authors.
|
||||
* Copyright 2002-2025 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -35,13 +35,11 @@ import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.client.HttpClientErrorException;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
import org.springframework.web.reactive.config.EnableWebFlux;
|
||||
import org.springframework.web.testfixture.http.server.reactive.bootstrap.HttpServer;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.fail;
|
||||
|
||||
/**
|
||||
* Integration tests with {@code @CrossOrigin} and {@code @RequestMapping}
|
||||
@@ -105,13 +103,11 @@ class CrossOriginAnnotationIntegrationTests extends AbstractRequestMappingIntegr
|
||||
void preflightRequestWithoutAnnotation(HttpServer httpServer) throws Exception {
|
||||
startServer(httpServer);
|
||||
this.headers.add(HttpHeaders.ACCESS_CONTROL_REQUEST_METHOD, "GET");
|
||||
try {
|
||||
performOptions("/no", this.headers, Void.class);
|
||||
fail("Preflight request without CORS configuration should fail");
|
||||
}
|
||||
catch (HttpClientErrorException ex) {
|
||||
assertThat(ex.getStatusCode()).isEqualTo(HttpStatus.FORBIDDEN);
|
||||
}
|
||||
ResponseEntity<Void> entity = performOptions("/no", this.headers, Void.class);
|
||||
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
|
||||
assertThat(entity.getHeaders().getAccessControlAllowOrigin()).isNull();
|
||||
assertThat(entity.getHeaders().getAccessControlAllowMethods()).isEmpty();
|
||||
|
||||
}
|
||||
|
||||
@ParameterizedHttpServerTest
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2022 the original author or authors.
|
||||
* Copyright 2002-2025 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -135,9 +135,10 @@ class GlobalCorsConfigIntegrationTests extends AbstractRequestMappingIntegration
|
||||
startServer(httpServer);
|
||||
|
||||
this.headers.add(HttpHeaders.ACCESS_CONTROL_REQUEST_METHOD, "GET");
|
||||
assertThatExceptionOfType(HttpClientErrorException.class).isThrownBy(() ->
|
||||
performOptions("/welcome", this.headers, String.class))
|
||||
.satisfies(ex -> assertThat(ex.getStatusCode()).isEqualTo(HttpStatus.FORBIDDEN));
|
||||
ResponseEntity<String> entity = performOptions("/welcome", this.headers, String.class);
|
||||
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
|
||||
assertThat(entity.getHeaders().getAccessControlAllowOrigin()).isNull();
|
||||
assertThat(entity.getHeaders().getAccessControlAllowMethods()).isEmpty();
|
||||
}
|
||||
|
||||
@ParameterizedHttpServerTest
|
||||
|
||||
Reference in New Issue
Block a user