Rename CorsConfigurationMapping to UrlBasedCorsConfigurationSource
Issue: SPR-13192
This commit is contained in:
parent
e9f64cf9ae
commit
70a03ee2a4
|
@ -36,7 +36,7 @@ import org.springframework.web.util.UrlPathHelper;
|
|||
* @author Sebastien Deleuze
|
||||
* @since 4.2
|
||||
*/
|
||||
public class CorsConfigurationMapping implements CorsConfigurationSource {
|
||||
public class UrlBasedCorsConfigurationSource implements CorsConfigurationSource {
|
||||
|
||||
private final Map<String, CorsConfiguration> corsConfigurations =
|
||||
new LinkedHashMap<String, CorsConfiguration>();
|
|
@ -24,18 +24,19 @@ import javax.servlet.http.HttpServletResponse;
|
|||
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.web.cors.CorsConfiguration;
|
||||
import org.springframework.web.cors.CorsConfigurationMapping;
|
||||
import org.springframework.web.cors.CorsConfigurationSource;
|
||||
import org.springframework.web.cors.CorsProcessor;
|
||||
import org.springframework.web.cors.CorsUtils;
|
||||
import org.springframework.web.cors.DefaultCorsProcessor;
|
||||
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
|
||||
|
||||
/**
|
||||
* {@link javax.servlet.Filter} that handles CORS preflight requests and intercepts CORS
|
||||
* simple and actual requests thanks to a {@link CorsProcessor} implementation
|
||||
* ({@link DefaultCorsProcessor} by default) in order to add the relevant CORS response
|
||||
* headers (like {@code Access-Control-Allow-Origin}) using the provided
|
||||
* {@link CorsConfigurationSource} (for example a {@link CorsConfigurationMapping} instance.
|
||||
* {@link CorsConfigurationSource} (for example an {@link UrlBasedCorsConfigurationSource}
|
||||
* instance.
|
||||
*
|
||||
* <p>This filter could be used in conjunction with {@link DelegatingFilterProxy} in order
|
||||
* to help with its initialization.
|
||||
|
@ -48,16 +49,16 @@ public class CorsFilter extends OncePerRequestFilter {
|
|||
|
||||
private CorsProcessor processor = new DefaultCorsProcessor();
|
||||
|
||||
private final CorsConfigurationSource source;
|
||||
private final CorsConfigurationSource configSource;
|
||||
|
||||
|
||||
/**
|
||||
* Constructor accepting a {@link CorsConfigurationSource}, this source will be used
|
||||
* by the filter to find the {@link CorsConfiguration} to use for each incoming request.
|
||||
* @see CorsConfigurationMapping
|
||||
* Constructor accepting a {@link CorsConfigurationSource} used by the filter to find
|
||||
* the {@link CorsConfiguration} to use for each incoming request.
|
||||
* @see UrlBasedCorsConfigurationSource
|
||||
*/
|
||||
public CorsFilter(CorsConfigurationSource source) {
|
||||
this.source = source;
|
||||
public CorsFilter(CorsConfigurationSource configSource) {
|
||||
this.configSource = configSource;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -75,7 +76,7 @@ public class CorsFilter extends OncePerRequestFilter {
|
|||
FilterChain filterChain) throws ServletException, IOException {
|
||||
|
||||
if (CorsUtils.isCorsRequest(request)) {
|
||||
CorsConfiguration corsConfiguration = this.source.getCorsConfiguration(request);
|
||||
CorsConfiguration corsConfiguration = this.configSource.getCorsConfiguration(request);
|
||||
if (corsConfiguration != null) {
|
||||
boolean isValid = this.processor.processRequest(corsConfiguration, request, response);
|
||||
if (!isValid || CorsUtils.isPreFlightRequest(request)) {
|
||||
|
|
|
@ -23,29 +23,29 @@ import org.springframework.http.HttpMethod;
|
|||
import org.springframework.mock.web.test.MockHttpServletRequest;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link CorsConfigurationMapping}.
|
||||
* Unit tests for {@link UrlBasedCorsConfigurationSource}.
|
||||
* @author Sebastien Deleuze
|
||||
*/
|
||||
public class CorsConfigurationMappingTests {
|
||||
public class UrlBasedCorsConfigurationSourceTests {
|
||||
|
||||
private final CorsConfigurationMapping mapping = new CorsConfigurationMapping();
|
||||
private final UrlBasedCorsConfigurationSource configSource = new UrlBasedCorsConfigurationSource();
|
||||
|
||||
@Test
|
||||
public void empty() {
|
||||
assertNull(this.mapping.getCorsConfiguration(new MockHttpServletRequest(HttpMethod.GET.name(), "/bar/test.html")));
|
||||
assertNull(this.configSource.getCorsConfiguration(new MockHttpServletRequest(HttpMethod.GET.name(), "/bar/test.html")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void registerAndMatch() {
|
||||
CorsConfiguration config = new CorsConfiguration();
|
||||
this.mapping.registerCorsConfiguration("/bar/**", config);
|
||||
assertNull(this.mapping.getCorsConfiguration(new MockHttpServletRequest(HttpMethod.GET.name(), "/foo/test.html")));
|
||||
assertEquals(config, this.mapping.getCorsConfiguration(new MockHttpServletRequest(HttpMethod.GET.name(), "/bar/test.html")));
|
||||
this.configSource.registerCorsConfiguration("/bar/**", config);
|
||||
assertNull(this.configSource.getCorsConfiguration(new MockHttpServletRequest(HttpMethod.GET.name(), "/foo/test.html")));
|
||||
assertEquals(config, this.configSource.getCorsConfiguration(new MockHttpServletRequest(HttpMethod.GET.name(), "/bar/test.html")));
|
||||
}
|
||||
|
||||
@Test(expected = UnsupportedOperationException.class)
|
||||
public void unmodifiableConfigurationsMap() {
|
||||
this.mapping.getCorsConfigurations().put("/**", new CorsConfiguration());
|
||||
this.configSource.getCorsConfigurations().put("/**", new CorsConfiguration());
|
||||
}
|
||||
|
||||
}
|
|
@ -28,7 +28,7 @@ import org.springframework.beans.BeansException;
|
|||
import org.springframework.beans.factory.BeanFactoryUtils;
|
||||
import org.springframework.core.Ordered;
|
||||
import org.springframework.web.HttpRequestHandler;
|
||||
import org.springframework.web.cors.CorsConfigurationMapping;
|
||||
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
|
||||
import org.springframework.web.cors.CorsProcessor;
|
||||
import org.springframework.web.cors.CorsConfiguration;
|
||||
import org.springframework.web.cors.CorsConfigurationSource;
|
||||
|
@ -81,7 +81,7 @@ public abstract class AbstractHandlerMapping extends WebApplicationObjectSupport
|
|||
|
||||
private CorsProcessor corsProcessor = new DefaultCorsProcessor();
|
||||
|
||||
private final CorsConfigurationMapping corsMapping = new CorsConfigurationMapping();
|
||||
private final UrlBasedCorsConfigurationSource corsConfigSource = new UrlBasedCorsConfigurationSource();
|
||||
|
||||
|
||||
/**
|
||||
|
@ -124,7 +124,7 @@ public abstract class AbstractHandlerMapping extends WebApplicationObjectSupport
|
|||
*/
|
||||
public void setAlwaysUseFullPath(boolean alwaysUseFullPath) {
|
||||
this.urlPathHelper.setAlwaysUseFullPath(alwaysUseFullPath);
|
||||
this.corsMapping.setAlwaysUseFullPath(alwaysUseFullPath);
|
||||
this.corsConfigSource.setAlwaysUseFullPath(alwaysUseFullPath);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -136,7 +136,7 @@ public abstract class AbstractHandlerMapping extends WebApplicationObjectSupport
|
|||
*/
|
||||
public void setUrlDecode(boolean urlDecode) {
|
||||
this.urlPathHelper.setUrlDecode(urlDecode);
|
||||
this.corsMapping.setUrlDecode(urlDecode);
|
||||
this.corsConfigSource.setUrlDecode(urlDecode);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -146,7 +146,7 @@ public abstract class AbstractHandlerMapping extends WebApplicationObjectSupport
|
|||
*/
|
||||
public void setRemoveSemicolonContent(boolean removeSemicolonContent) {
|
||||
this.urlPathHelper.setRemoveSemicolonContent(removeSemicolonContent);
|
||||
this.corsMapping.setRemoveSemicolonContent(removeSemicolonContent);
|
||||
this.corsConfigSource.setRemoveSemicolonContent(removeSemicolonContent);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -158,7 +158,7 @@ public abstract class AbstractHandlerMapping extends WebApplicationObjectSupport
|
|||
public void setUrlPathHelper(UrlPathHelper urlPathHelper) {
|
||||
Assert.notNull(urlPathHelper, "UrlPathHelper must not be null");
|
||||
this.urlPathHelper = urlPathHelper;
|
||||
this.corsMapping.setUrlPathHelper(urlPathHelper);
|
||||
this.corsConfigSource.setUrlPathHelper(urlPathHelper);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -176,7 +176,7 @@ public abstract class AbstractHandlerMapping extends WebApplicationObjectSupport
|
|||
public void setPathMatcher(PathMatcher pathMatcher) {
|
||||
Assert.notNull(pathMatcher, "PathMatcher must not be null");
|
||||
this.pathMatcher = pathMatcher;
|
||||
this.corsMapping.setPathMatcher(pathMatcher);
|
||||
this.corsConfigSource.setPathMatcher(pathMatcher);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -226,14 +226,14 @@ public abstract class AbstractHandlerMapping extends WebApplicationObjectSupport
|
|||
* @since 4.2
|
||||
*/
|
||||
public void setCorsConfigurations(Map<String, CorsConfiguration> corsConfigurations) {
|
||||
this.corsMapping.setCorsConfigurations(corsConfigurations);
|
||||
this.corsConfigSource.setCorsConfigurations(corsConfigurations);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the CORS configuration.
|
||||
*/
|
||||
public Map<String, CorsConfiguration> getCorsConfigurations() {
|
||||
return this.corsMapping.getCorsConfigurations();
|
||||
return this.corsConfigSource.getCorsConfigurations();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -362,7 +362,7 @@ public abstract class AbstractHandlerMapping extends WebApplicationObjectSupport
|
|||
}
|
||||
HandlerExecutionChain executionChain = getHandlerExecutionChain(handler, request);
|
||||
if (CorsUtils.isCorsRequest(request)) {
|
||||
CorsConfiguration globalConfig = this.corsMapping.getCorsConfiguration(request);
|
||||
CorsConfiguration globalConfig = this.corsConfigSource.getCorsConfiguration(request);
|
||||
CorsConfiguration handlerConfig = getCorsConfiguration(handler, request);
|
||||
CorsConfiguration config = (globalConfig != null ? globalConfig.combine(handlerConfig) : handlerConfig);
|
||||
executionChain = getCorsHandlerExecutionChain(request, executionChain, config);
|
||||
|
|
Loading…
Reference in New Issue