Skip to content

Commit 1cbdebc

Browse files
committed
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.
1 parent b4183e5 commit 1cbdebc

File tree

3 files changed

+80
-0
lines changed

3 files changed

+80
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Copyright 2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.rest.webmvc.jpa;
17+
18+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
19+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
20+
21+
import org.junit.Test;
22+
import org.springframework.context.annotation.Bean;
23+
import org.springframework.data.rest.tests.AbstractWebIntegrationTests;
24+
import org.springframework.data.rest.webmvc.config.RepositoryRestConfigurer;
25+
import org.springframework.hateoas.Link;
26+
import org.springframework.hateoas.LinkRelation;
27+
import org.springframework.http.HttpHeaders;
28+
import org.springframework.test.context.ContextConfiguration;
29+
30+
/**
31+
* Web integration tests specific to Cross-origin resource sharing.
32+
*
33+
* @author Mark Paluch
34+
* @soundtrack RFLKTD - Liquid Crystals
35+
*/
36+
@ContextConfiguration
37+
public class LocalConfigCorsIntegrationTests extends AbstractWebIntegrationTests {
38+
39+
static class CorsConfig extends JpaRepositoryConfig {
40+
41+
@Bean
42+
RepositoryRestConfigurer repositoryRestConfigurer() {
43+
return RepositoryRestConfigurer.withConfig(c -> {});
44+
}
45+
}
46+
47+
/**
48+
* @see ItemRepository
49+
*/
50+
@Test // DATAREST-1397
51+
public void appliesRepositoryCorsConfiguration() throws Exception {
52+
53+
Link findItems = client.discoverUnique(LinkRelation.of("items"));
54+
55+
// Preflight request
56+
mvc.perform(options(findItems.expand().getHref()).header(HttpHeaders.ORIGIN, "http://far.far.example")
57+
.header(HttpHeaders.ACCESS_CONTROL_REQUEST_METHOD, "POST")) //
58+
.andExpect(status().isOk()) //
59+
.andExpect(
60+
header().string(HttpHeaders.ACCESS_CONTROL_ALLOW_METHODS, "GET,HEAD,POST,PUT,PATCH,DELETE,OPTIONS,TRACE"));
61+
}
62+
}

spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/BasePathAwareHandlerMapping.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,15 @@ protected HandlerMethod lookupHandlerMethod(String lookupPath, HttpServletReques
112112
return super.lookupHandlerMethod(lookupPath, new CustomAcceptHeaderHttpServletRequest(request, mediaTypes));
113113
}
114114

115+
/*
116+
* (non-Javadoc)
117+
* @see org.springframework.web.servlet.handler.AbstractHandlerMapping#hasCorsConfigurationSource(java.lang.Object)
118+
*/
119+
@Override
120+
protected boolean hasCorsConfigurationSource(Object handler) {
121+
return true;
122+
}
123+
115124
/*
116125
* (non-Javadoc)
117126
* @see org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping#getMappingForMethod(java.lang.reflect.Method, java.lang.Class)

spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/RepositoryRestHandlerMapping.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,15 @@ protected boolean isHandler(Class<?> beanType) {
190190
return AnnotationUtils.findAnnotation(type, RepositoryRestController.class) != null;
191191
}
192192

193+
/*
194+
* (non-Javadoc)
195+
* @see org.springframework.web.servlet.handler.AbstractHandlerMapping#hasCorsConfigurationSource(java.lang.Object)
196+
*/
197+
@Override
198+
protected boolean hasCorsConfigurationSource(Object handler) {
199+
return true;
200+
}
201+
193202
/*
194203
* (non-Javadoc)
195204
* @see org.springframework.web.servlet.handler.AbstractHandlerMapping#extendInterceptors(java.util.List)

0 commit comments

Comments
 (0)