CorsConfiguration now supports pattern based origins.
Closes gh-24763
This commit is contained in:
committed by
Rossen Stoyanchev
parent
a1bab14140
commit
8632118e8d
@@ -315,6 +315,9 @@ public class RequestMappingHandlerMapping extends RequestMappingInfoHandlerMappi
|
||||
for (String origin : annotation.origins()) {
|
||||
config.addAllowedOrigin(resolveCorsAnnotationValue(origin));
|
||||
}
|
||||
for (String originsPattern : annotation.originsPatterns()) {
|
||||
config.addAllowedOriginPattern(resolveCorsAnnotationValue(originsPattern));
|
||||
}
|
||||
for (RequestMethod method : annotation.methods()) {
|
||||
config.addAllowedMethod(method.name());
|
||||
}
|
||||
|
||||
@@ -110,6 +110,21 @@ public class CorsUrlHandlerMappingTests {
|
||||
assertThat(exchange.getResponse().getHeaders().getFirst(HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN)).isEqualTo("*");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void actualRequestWithGlobalPatternCorsConfig() throws Exception {
|
||||
CorsConfiguration mappedConfig = new CorsConfiguration();
|
||||
mappedConfig.addAllowedOriginPattern(".*\\.domain2.com");
|
||||
this.handlerMapping.setCorsConfigurations(Collections.singletonMap("/welcome.html", mappedConfig));
|
||||
|
||||
String origin = "https://example.domain2.com";
|
||||
ServerWebExchange exchange = createExchange(HttpMethod.GET, "/welcome.html", origin);
|
||||
Object actual = this.handlerMapping.getHandler(exchange).block();
|
||||
|
||||
assertThat(actual).isNotNull();
|
||||
assertThat(actual).isSameAs(this.welcomeController);
|
||||
assertThat(exchange.getResponse().getHeaders().getFirst(HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN)).isEqualTo("https://example.domain2.com");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void preFlightRequestWithGlobalCorsConfig() throws Exception {
|
||||
CorsConfiguration mappedConfig = new CorsConfiguration();
|
||||
|
||||
@@ -68,6 +68,7 @@ class CrossOriginAnnotationIntegrationTests extends AbstractRequestMappingIntegr
|
||||
context.register(WebConfig.class);
|
||||
Properties props = new Properties();
|
||||
props.setProperty("myOrigin", "https://site1.com");
|
||||
props.setProperty("myOriginPattern", ".*\\.com");
|
||||
context.getEnvironment().getPropertySources().addFirst(new PropertiesPropertySource("ps", props));
|
||||
context.register(PropertySourcesPlaceholderConfigurer.class);
|
||||
context.refresh();
|
||||
@@ -206,6 +207,26 @@ class CrossOriginAnnotationIntegrationTests extends AbstractRequestMappingIntegr
|
||||
assertThat(entity.getBody()).isEqualTo("placeholder");
|
||||
}
|
||||
|
||||
@ParameterizedHttpServerTest
|
||||
void customOriginPatternDefinedViaValueAttribute(HttpServer httpServer) throws Exception {
|
||||
startServer(httpServer);
|
||||
|
||||
ResponseEntity<String> entity = performGet("/origin-pattern-value-attribute", this.headers, String.class);
|
||||
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
|
||||
assertThat(entity.getHeaders().getAccessControlAllowOrigin()).isEqualTo("https://site1.com");
|
||||
assertThat(entity.getBody()).isEqualTo("pattern-value-attribute");
|
||||
}
|
||||
|
||||
@ParameterizedHttpServerTest
|
||||
void customOriginPatternDefinedViaPlaceholder(HttpServer httpServer) throws Exception {
|
||||
startServer(httpServer);
|
||||
|
||||
ResponseEntity<String> entity = performGet("/origin-pattern-placeholder", this.headers, String.class);
|
||||
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
|
||||
assertThat(entity.getHeaders().getAccessControlAllowOrigin()).isEqualTo("https://site1.com");
|
||||
assertThat(entity.getBody()).isEqualTo("pattern-placeholder");
|
||||
}
|
||||
|
||||
@ParameterizedHttpServerTest
|
||||
void classLevel(HttpServer httpServer) throws Exception {
|
||||
startServer(httpServer);
|
||||
@@ -335,6 +356,18 @@ class CrossOriginAnnotationIntegrationTests extends AbstractRequestMappingIntegr
|
||||
public String customOriginDefinedViaPlaceholder() {
|
||||
return "placeholder";
|
||||
}
|
||||
|
||||
@CrossOrigin(originsPatterns = ".*\\.com")
|
||||
@GetMapping("/origin-pattern-value-attribute")
|
||||
public String customOriginPatternDefinedViaValueAttribute() {
|
||||
return "pattern-value-attribute";
|
||||
}
|
||||
|
||||
@CrossOrigin(originsPatterns = "${myOriginPattern}")
|
||||
@GetMapping("/origin-pattern-placeholder")
|
||||
public String customOriginPatternDefinedViaPlaceholder() {
|
||||
return "pattern-placeholder";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user