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.
This commit is contained in:
Mark Paluch
2019-06-21 11:04:53 +02:00
committed by Oliver Drotbohm
parent 436e7e1e13
commit 4e586495f4
4 changed files with 82 additions and 1 deletions

View File

@@ -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

View File

@@ -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"));
}
}

View File

@@ -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)

View File

@@ -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)