From 4e586495f47f9124c1fdbec04b32acc812a6adfb Mon Sep 17 00:00:00 2001 From: Mark Paluch Date: Fri, 21 Jun 2019 11:04:53 +0200 Subject: [PATCH] DATAREST-1397 - Adapt to CORS changes in AbstractHandlerMapping. We now enable CORS handling for all requests by overriding the newly introduced hasCorsConfigurationSource method. We cannot detect CORS configuration handling solely on the handler but require path headers to resolve repository interface mappings. --- .../rest/webmvc/jpa/CorsIntegrationTests.java | 3 +- .../jpa/LocalConfigCorsIntegrationTests.java | 62 +++++++++++++++++++ .../webmvc/BasePathAwareHandlerMapping.java | 9 +++ .../webmvc/RepositoryRestHandlerMapping.java | 9 +++ 4 files changed, 82 insertions(+), 1 deletion(-) create mode 100755 spring-data-rest-tests/spring-data-rest-tests-jpa/src/test/java/org/springframework/data/rest/webmvc/jpa/LocalConfigCorsIntegrationTests.java diff --git a/spring-data-rest-tests/spring-data-rest-tests-jpa/src/test/java/org/springframework/data/rest/webmvc/jpa/CorsIntegrationTests.java b/spring-data-rest-tests/spring-data-rest-tests-jpa/src/test/java/org/springframework/data/rest/webmvc/jpa/CorsIntegrationTests.java index dfb30beff..473cb3c21 100755 --- a/spring-data-rest-tests/spring-data-rest-tests-jpa/src/test/java/org/springframework/data/rest/webmvc/jpa/CorsIntegrationTests.java +++ b/spring-data-rest-tests/spring-data-rest-tests-jpa/src/test/java/org/springframework/data/rest/webmvc/jpa/CorsIntegrationTests.java @@ -36,7 +36,8 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; /** - * Web integration tests specific to Cross-origin resource sharing. + * Web integration tests specific to Cross-origin resource sharing applying global CORS config defaults mixed with local + * controller/repository declarations. * * @author Mark Paluch * @soundtrack 2 Unlimited - No Limit diff --git a/spring-data-rest-tests/spring-data-rest-tests-jpa/src/test/java/org/springframework/data/rest/webmvc/jpa/LocalConfigCorsIntegrationTests.java b/spring-data-rest-tests/spring-data-rest-tests-jpa/src/test/java/org/springframework/data/rest/webmvc/jpa/LocalConfigCorsIntegrationTests.java new file mode 100755 index 000000000..32a2338a0 --- /dev/null +++ b/spring-data-rest-tests/spring-data-rest-tests-jpa/src/test/java/org/springframework/data/rest/webmvc/jpa/LocalConfigCorsIntegrationTests.java @@ -0,0 +1,62 @@ +/* + * Copyright 2019 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. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.rest.webmvc.jpa; + +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; + +import org.junit.Test; +import org.springframework.context.annotation.Bean; +import org.springframework.data.rest.tests.AbstractWebIntegrationTests; +import org.springframework.data.rest.webmvc.config.RepositoryRestConfigurer; +import org.springframework.hateoas.Link; +import org.springframework.hateoas.LinkRelation; +import org.springframework.http.HttpHeaders; +import org.springframework.test.context.ContextConfiguration; + +/** + * Web integration tests specific to Cross-origin resource sharing using repository interface CORS configuration. + * + * @author Mark Paluch + * @soundtrack RFLKTD - Liquid Crystals + */ +@ContextConfiguration +public class LocalConfigCorsIntegrationTests extends AbstractWebIntegrationTests { + + static class CorsConfig extends JpaRepositoryConfig { + + @Bean + RepositoryRestConfigurer repositoryRestConfigurer() { + return RepositoryRestConfigurer.withConfig(c -> {}); + } + } + + /** + * @see ItemRepository + */ + @Test // DATAREST-1397 + public void appliesRepositoryCorsConfiguration() throws Exception { + + Link findItems = client.discoverUnique(LinkRelation.of("items")); + + // Preflight request + mvc.perform(options(findItems.expand().getHref()).header(HttpHeaders.ORIGIN, "http://far.far.example") + .header(HttpHeaders.ACCESS_CONTROL_REQUEST_METHOD, "POST")) // + .andExpect(status().isOk()) // + .andExpect( + header().string(HttpHeaders.ACCESS_CONTROL_ALLOW_METHODS, "GET,HEAD,POST,PUT,PATCH,DELETE,OPTIONS,TRACE")); + } +} diff --git a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/BasePathAwareHandlerMapping.java b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/BasePathAwareHandlerMapping.java index 90f772c33..188a79220 100644 --- a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/BasePathAwareHandlerMapping.java +++ b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/BasePathAwareHandlerMapping.java @@ -112,6 +112,15 @@ public class BasePathAwareHandlerMapping extends RequestMappingHandlerMapping { return super.lookupHandlerMethod(lookupPath, new CustomAcceptHeaderHttpServletRequest(request, mediaTypes)); } + /* + * (non-Javadoc) + * @see org.springframework.web.servlet.handler.AbstractHandlerMapping#hasCorsConfigurationSource(java.lang.Object) + */ + @Override + protected boolean hasCorsConfigurationSource(Object handler) { + return true; + } + /* * (non-Javadoc) * @see org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping#getMappingForMethod(java.lang.reflect.Method, java.lang.Class) diff --git a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/RepositoryRestHandlerMapping.java b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/RepositoryRestHandlerMapping.java index 2024fdaa5..55f799e8b 100644 --- a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/RepositoryRestHandlerMapping.java +++ b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/RepositoryRestHandlerMapping.java @@ -190,6 +190,15 @@ public class RepositoryRestHandlerMapping extends BasePathAwareHandlerMapping { return AnnotationUtils.findAnnotation(type, RepositoryRestController.class) != null; } + /* + * (non-Javadoc) + * @see org.springframework.web.servlet.handler.AbstractHandlerMapping#hasCorsConfigurationSource(java.lang.Object) + */ + @Override + protected boolean hasCorsConfigurationSource(Object handler) { + return true; + } + /* * (non-Javadoc) * @see org.springframework.web.servlet.handler.AbstractHandlerMapping#extendInterceptors(java.util.List)