Rename CorsConfigurationMapping to UrlBasedCorsConfigurationSource

Issue: SPR-13192
This commit is contained in:
Sebastien Deleuze 2015-07-20 10:09:33 +02:00
parent e9f64cf9ae
commit 70a03ee2a4
4 changed files with 29 additions and 28 deletions

View File

@ -36,7 +36,7 @@ import org.springframework.web.util.UrlPathHelper;
* @author Sebastien Deleuze * @author Sebastien Deleuze
* @since 4.2 * @since 4.2
*/ */
public class CorsConfigurationMapping implements CorsConfigurationSource { public class UrlBasedCorsConfigurationSource implements CorsConfigurationSource {
private final Map<String, CorsConfiguration> corsConfigurations = private final Map<String, CorsConfiguration> corsConfigurations =
new LinkedHashMap<String, CorsConfiguration>(); new LinkedHashMap<String, CorsConfiguration>();

View File

@ -24,18 +24,19 @@ import javax.servlet.http.HttpServletResponse;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import org.springframework.web.cors.CorsConfiguration; import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.CorsConfigurationMapping;
import org.springframework.web.cors.CorsConfigurationSource; import org.springframework.web.cors.CorsConfigurationSource;
import org.springframework.web.cors.CorsProcessor; import org.springframework.web.cors.CorsProcessor;
import org.springframework.web.cors.CorsUtils; import org.springframework.web.cors.CorsUtils;
import org.springframework.web.cors.DefaultCorsProcessor; import org.springframework.web.cors.DefaultCorsProcessor;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
/** /**
* {@link javax.servlet.Filter} that handles CORS preflight requests and intercepts CORS * {@link javax.servlet.Filter} that handles CORS preflight requests and intercepts CORS
* simple and actual requests thanks to a {@link CorsProcessor} implementation * simple and actual requests thanks to a {@link CorsProcessor} implementation
* ({@link DefaultCorsProcessor} by default) in order to add the relevant CORS response * ({@link DefaultCorsProcessor} by default) in order to add the relevant CORS response
* headers (like {@code Access-Control-Allow-Origin}) using the provided * 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 * <p>This filter could be used in conjunction with {@link DelegatingFilterProxy} in order
* to help with its initialization. * to help with its initialization.
@ -48,16 +49,16 @@ public class CorsFilter extends OncePerRequestFilter {
private CorsProcessor processor = new DefaultCorsProcessor(); private CorsProcessor processor = new DefaultCorsProcessor();
private final CorsConfigurationSource source; private final CorsConfigurationSource configSource;
/** /**
* Constructor accepting a {@link CorsConfigurationSource}, this source will be used * Constructor accepting a {@link CorsConfigurationSource} used by the filter to find
* by the filter to find the {@link CorsConfiguration} to use for each incoming request. * the {@link CorsConfiguration} to use for each incoming request.
* @see CorsConfigurationMapping * @see UrlBasedCorsConfigurationSource
*/ */
public CorsFilter(CorsConfigurationSource source) { public CorsFilter(CorsConfigurationSource configSource) {
this.source = source; this.configSource = configSource;
} }
/** /**
@ -75,7 +76,7 @@ public class CorsFilter extends OncePerRequestFilter {
FilterChain filterChain) throws ServletException, IOException { FilterChain filterChain) throws ServletException, IOException {
if (CorsUtils.isCorsRequest(request)) { if (CorsUtils.isCorsRequest(request)) {
CorsConfiguration corsConfiguration = this.source.getCorsConfiguration(request); CorsConfiguration corsConfiguration = this.configSource.getCorsConfiguration(request);
if (corsConfiguration != null) { if (corsConfiguration != null) {
boolean isValid = this.processor.processRequest(corsConfiguration, request, response); boolean isValid = this.processor.processRequest(corsConfiguration, request, response);
if (!isValid || CorsUtils.isPreFlightRequest(request)) { if (!isValid || CorsUtils.isPreFlightRequest(request)) {

View File

@ -23,29 +23,29 @@ import org.springframework.http.HttpMethod;
import org.springframework.mock.web.test.MockHttpServletRequest; import org.springframework.mock.web.test.MockHttpServletRequest;
/** /**
* Unit tests for {@link CorsConfigurationMapping}. * Unit tests for {@link UrlBasedCorsConfigurationSource}.
* @author Sebastien Deleuze * @author Sebastien Deleuze
*/ */
public class CorsConfigurationMappingTests { public class UrlBasedCorsConfigurationSourceTests {
private final CorsConfigurationMapping mapping = new CorsConfigurationMapping(); private final UrlBasedCorsConfigurationSource configSource = new UrlBasedCorsConfigurationSource();
@Test @Test
public void empty() { 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 @Test
public void registerAndMatch() { public void registerAndMatch() {
CorsConfiguration config = new CorsConfiguration(); CorsConfiguration config = new CorsConfiguration();
this.mapping.registerCorsConfiguration("/bar/**", config); this.configSource.registerCorsConfiguration("/bar/**", config);
assertNull(this.mapping.getCorsConfiguration(new MockHttpServletRequest(HttpMethod.GET.name(), "/foo/test.html"))); assertNull(this.configSource.getCorsConfiguration(new MockHttpServletRequest(HttpMethod.GET.name(), "/foo/test.html")));
assertEquals(config, this.mapping.getCorsConfiguration(new MockHttpServletRequest(HttpMethod.GET.name(), "/bar/test.html"))); assertEquals(config, this.configSource.getCorsConfiguration(new MockHttpServletRequest(HttpMethod.GET.name(), "/bar/test.html")));
} }
@Test(expected = UnsupportedOperationException.class) @Test(expected = UnsupportedOperationException.class)
public void unmodifiableConfigurationsMap() { public void unmodifiableConfigurationsMap() {
this.mapping.getCorsConfigurations().put("/**", new CorsConfiguration()); this.configSource.getCorsConfigurations().put("/**", new CorsConfiguration());
} }
} }

View File

@ -28,7 +28,7 @@ import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactoryUtils; import org.springframework.beans.factory.BeanFactoryUtils;
import org.springframework.core.Ordered; import org.springframework.core.Ordered;
import org.springframework.web.HttpRequestHandler; 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.CorsProcessor;
import org.springframework.web.cors.CorsConfiguration; import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.CorsConfigurationSource; import org.springframework.web.cors.CorsConfigurationSource;
@ -81,7 +81,7 @@ public abstract class AbstractHandlerMapping extends WebApplicationObjectSupport
private CorsProcessor corsProcessor = new DefaultCorsProcessor(); 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) { public void setAlwaysUseFullPath(boolean alwaysUseFullPath) {
this.urlPathHelper.setAlwaysUseFullPath(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) { public void setUrlDecode(boolean urlDecode) {
this.urlPathHelper.setUrlDecode(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) { public void setRemoveSemicolonContent(boolean removeSemicolonContent) {
this.urlPathHelper.setRemoveSemicolonContent(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) { public void setUrlPathHelper(UrlPathHelper urlPathHelper) {
Assert.notNull(urlPathHelper, "UrlPathHelper must not be null"); Assert.notNull(urlPathHelper, "UrlPathHelper must not be null");
this.urlPathHelper = urlPathHelper; 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) { public void setPathMatcher(PathMatcher pathMatcher) {
Assert.notNull(pathMatcher, "PathMatcher must not be null"); Assert.notNull(pathMatcher, "PathMatcher must not be null");
this.pathMatcher = pathMatcher; this.pathMatcher = pathMatcher;
this.corsMapping.setPathMatcher(pathMatcher); this.corsConfigSource.setPathMatcher(pathMatcher);
} }
/** /**
@ -226,14 +226,14 @@ public abstract class AbstractHandlerMapping extends WebApplicationObjectSupport
* @since 4.2 * @since 4.2
*/ */
public void setCorsConfigurations(Map<String, CorsConfiguration> corsConfigurations) { public void setCorsConfigurations(Map<String, CorsConfiguration> corsConfigurations) {
this.corsMapping.setCorsConfigurations(corsConfigurations); this.corsConfigSource.setCorsConfigurations(corsConfigurations);
} }
/** /**
* Get the CORS configuration. * Get the CORS configuration.
*/ */
public Map<String, CorsConfiguration> getCorsConfigurations() { 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); HandlerExecutionChain executionChain = getHandlerExecutionChain(handler, request);
if (CorsUtils.isCorsRequest(request)) { if (CorsUtils.isCorsRequest(request)) {
CorsConfiguration globalConfig = this.corsMapping.getCorsConfiguration(request); CorsConfiguration globalConfig = this.corsConfigSource.getCorsConfiguration(request);
CorsConfiguration handlerConfig = getCorsConfiguration(handler, request); CorsConfiguration handlerConfig = getCorsConfiguration(handler, request);
CorsConfiguration config = (globalConfig != null ? globalConfig.combine(handlerConfig) : handlerConfig); CorsConfiguration config = (globalConfig != null ? globalConfig.combine(handlerConfig) : handlerConfig);
executionChain = getCorsHandlerExecutionChain(request, executionChain, config); executionChain = getCorsHandlerExecutionChain(request, executionChain, config);