Support custom CorsConfigurationSource in AbstractHandlerMapping
This commit allows to specify a custom CorsConfigurationSource in AbstractHandlerMapping (both Servlet and Reactive variants). AbstractHandlerMapping#getCorsConfigurations method is now deprecated. Issue: SPR-17067
This commit is contained in:
@@ -53,7 +53,7 @@ public abstract class AbstractHandlerMapping extends ApplicationObjectSupport
|
||||
|
||||
private final PathPatternParser patternParser;
|
||||
|
||||
private final UrlBasedCorsConfigurationSource globalCorsConfigSource;
|
||||
private CorsConfigurationSource corsConfigurationSource;
|
||||
|
||||
private CorsProcessor corsProcessor = new DefaultCorsProcessor();
|
||||
|
||||
@@ -65,7 +65,7 @@ public abstract class AbstractHandlerMapping extends ApplicationObjectSupport
|
||||
|
||||
public AbstractHandlerMapping() {
|
||||
this.patternParser = new PathPatternParser();
|
||||
this.globalCorsConfigSource = new UrlBasedCorsConfigurationSource(this.patternParser);
|
||||
this.corsConfigurationSource = new UrlBasedCorsConfigurationSource(this.patternParser);
|
||||
}
|
||||
|
||||
|
||||
@@ -107,12 +107,25 @@ public abstract class AbstractHandlerMapping extends ApplicationObjectSupport
|
||||
}
|
||||
|
||||
/**
|
||||
* Set "global" CORS configuration based on URL patterns. By default the
|
||||
* first matching URL pattern is combined with handler-level CORS
|
||||
* configuration if any.
|
||||
* Set the "global" CORS configurations based on URL patterns. By default the
|
||||
* first matching URL pattern is combined with handler-level CORS configuration if any.
|
||||
* @see #setCorsConfigurationSource(CorsConfigurationSource)
|
||||
*/
|
||||
public void setCorsConfigurations(Map<String, CorsConfiguration> corsConfigurations) {
|
||||
this.globalCorsConfigSource.setCorsConfigurations(corsConfigurations);
|
||||
Assert.notNull(corsConfigurations, "corsConfigurations must not be null");
|
||||
this.corsConfigurationSource = new UrlBasedCorsConfigurationSource(this.patternParser);
|
||||
((UrlBasedCorsConfigurationSource) this.corsConfigurationSource).setCorsConfigurations(corsConfigurations);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the "global" CORS configuration source. By default the first matching URL
|
||||
* pattern is combined with the CORS configuration for the handler, if any.
|
||||
* @since 5.1
|
||||
* @see #setCorsConfigurations(Map)
|
||||
*/
|
||||
public void setCorsConfigurationSource(CorsConfigurationSource corsConfigurationSource) {
|
||||
Assert.notNull(corsConfigurationSource, "corsConfigurationSource must not be null");
|
||||
this.corsConfigurationSource = corsConfigurationSource;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -163,7 +176,7 @@ public abstract class AbstractHandlerMapping extends ApplicationObjectSupport
|
||||
logger.debug(exchange.getLogPrefix() + "Mapped to " + handler);
|
||||
}
|
||||
if (CorsUtils.isCorsRequest(exchange.getRequest())) {
|
||||
CorsConfiguration configA = this.globalCorsConfigSource.getCorsConfiguration(exchange);
|
||||
CorsConfiguration configA = this.corsConfigurationSource.getCorsConfiguration(exchange);
|
||||
CorsConfiguration configB = getCorsConfiguration(handler, exchange);
|
||||
CorsConfiguration config = (configA != null ? configA.combine(configB) : configB);
|
||||
if (!getCorsProcessor().process(config, exchange) ||
|
||||
|
||||
Reference in New Issue
Block a user