Skip to content

DATAREST-573 Add support for Spring Web MVC CORS configuration mechanisms. #233

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-rest-parent</artifactId>
<version>2.6.0.BUILD-SNAPSHOT</version>
<version>2.6.0.DATAREST-573-SNAPSHOT</version>
<packaging>pom</packaging>

<name>Spring Data REST</name>
Expand Down
2 changes: 1 addition & 1 deletion spring-data-rest-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-rest-parent</artifactId>
<version>2.6.0.BUILD-SNAPSHOT</version>
<version>2.6.0.DATAREST-573-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright 2016 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
*
* http://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.core.config;

import java.util.Map;

import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;

/**
* Spring Data REST specific {@code CorsRegistry} implementation exposing {@link #getCorsConfigurations()}. Assists with
* the registration of {@link CorsConfiguration} mapped to a path pattern.
*
* @author Mark Paluch
* @since 2.6
*/
public class RepositoryCorsRegistry extends CorsRegistry {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the reason we need this dedicated class? It seems we could just get away with using CorsRegistry, right?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CorsRegistry's getCorsConfigurations method is protected so we need to make it usable for us. Didn't want to rely on reflection.


/* (non-Javadoc)
* @see org.springframework.web.servlet.config.annotation.CorsRegistry#getCorsConfigurations()
*/
@Override
public Map<String, CorsConfiguration> getCorsConfigurations() {
return super.getCorsConfigurations();
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2015 the original author or authors.
* Copyright 2012-2016 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.
Expand Down Expand Up @@ -28,6 +28,8 @@
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.servlet.config.annotation.CorsRegistration;

/**
* Spring Data REST configuration options.
Expand All @@ -36,6 +38,7 @@
* @author Oliver Gierke
* @author Jeremy Rickard
* @author Greg Turnquist
* @author Mark Paluch
*/
@SuppressWarnings("deprecation")
public class RepositoryRestConfiguration {
Expand All @@ -58,6 +61,7 @@ public class RepositoryRestConfiguration {
private ResourceMappingConfiguration repoMappings = new ResourceMappingConfiguration();
private RepositoryDetectionStrategy repositoryDetectionStrategy = RepositoryDetectionStrategies.DEFAULT;

private final RepositoryCorsRegistry corsRegistry = new RepositoryCorsRegistry();
private final ProjectionDefinitionConfiguration projectionConfiguration;
private final MetadataConfiguration metadataConfiguration;
private final EntityLookupConfiguration entityLookupConfiguration;
Expand Down Expand Up @@ -549,6 +553,34 @@ public void setRepositoryDetectionStrategy(RepositoryDetectionStrategy repositor
: repositoryDetectionStrategy;
}

/**
* Returns the {@link RepositoryCorsRegistry} to configure Cross-origin resource sharing.
*
* @return the {@link RepositoryCorsRegistry}.
* @since 2.6
* @see RepositoryCorsRegistry
* @see CorsRegistration
*/
public RepositoryCorsRegistry getCorsRegistry() {
return corsRegistry;
}

/**
* Configures Cross-origin resource sharing given a {@code path}.
*
* @param path path or path pattern, must not be {@literal null} or empty.
* @return the {@link CorsRegistration} to build a CORS configuration.
* @since 2.6
* @see CorsConfiguration
*/
public CorsRegistration addCorsMapping(String path) {

Assert.notNull(path, "Path must not be null!");
Assert.hasText(path, "Path must not be empty!");

return corsRegistry.addMapping(path);
}

/**
* Returns the {@link EntityLookupRegistrar} to create custom {@link EntityLookup} instances registered in the
* configuration.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@
*/
package org.springframework.data.rest.core;

import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;

import java.util.Map;

import org.junit.Before;
import org.junit.Test;
import org.springframework.data.rest.core.config.EnumTranslationConfiguration;
Expand All @@ -28,11 +30,13 @@
import org.springframework.data.rest.core.domain.Profile;
import org.springframework.data.rest.core.domain.ProfileRepository;
import org.springframework.http.MediaType;
import org.springframework.web.cors.CorsConfiguration;

/**
* Unit tests for {@link RepositoryRestConfiguration}.
*
* @author Oliver Gierke
* @author Mark Paluch
* @soundtrack Adam F - Circles (Colors)
*/
public class RepositoryRestConfigurationUnitTests {
Expand Down Expand Up @@ -132,10 +136,25 @@ public void returnsBodyForCreateIfExplicitlyActivated() {
* @see DATAREST-776
*/
@Test
public void consideresDomainTypeOfValueRepositoryLookupTypes() {
public void considersDomainTypeOfValueRepositoryLookupTypes() {

configuration.withEntityLookup().forLookupRepository(ProfileRepository.class);

assertThat(configuration.isLookupType(Profile.class), is(true));
}

/**
* @see DATAREST-573
*/
@Test
public void configuresCorsProcessing() {

configuration.addCorsMapping("/hello").maxAge(1234);

Map<String, CorsConfiguration> corsConfigurations = configuration.getCorsRegistry().getCorsConfigurations();
assertThat(corsConfigurations, hasKey("/hello"));

CorsConfiguration corsConfiguration = corsConfigurations.get("/hello");
assertThat(corsConfiguration.getMaxAge(), is(1234L));
}
}
2 changes: 1 addition & 1 deletion spring-data-rest-distribution/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-rest-parent</artifactId>
<version>2.6.0.BUILD-SNAPSHOT</version>
<version>2.6.0.DATAREST-573-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion spring-data-rest-hal-browser/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-rest-parent</artifactId>
<version>2.6.0.BUILD-SNAPSHOT</version>
<version>2.6.0.DATAREST-573-SNAPSHOT</version>
</parent>

<artifactId>spring-data-rest-hal-browser</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion spring-data-rest-tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-rest-parent</artifactId>
<version>2.6.0.BUILD-SNAPSHOT</version>
<version>2.6.0.DATAREST-573-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
4 changes: 2 additions & 2 deletions spring-data-rest-tests/spring-data-rest-tests-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-rest-tests</artifactId>
<version>2.6.0.BUILD-SNAPSHOT</version>
<version>2.6.0.DATAREST-573-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand All @@ -17,7 +17,7 @@
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-rest-webmvc</artifactId>
<version>2.6.0.BUILD-SNAPSHOT</version>
<version>2.6.0.DATAREST-573-SNAPSHOT</version>
</dependency>

<dependency>
Expand Down
4 changes: 2 additions & 2 deletions spring-data-rest-tests/spring-data-rest-tests-gemfire/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-rest-tests</artifactId>
<version>2.6.0.BUILD-SNAPSHOT</version>
<version>2.6.0.DATAREST-573-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand All @@ -17,7 +17,7 @@
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-rest-tests-core</artifactId>
<version>2.6.0.BUILD-SNAPSHOT</version>
<version>2.6.0.DATAREST-573-SNAPSHOT</version>
<type>test-jar</type>
</dependency>

Expand Down
4 changes: 2 additions & 2 deletions spring-data-rest-tests/spring-data-rest-tests-jpa/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-rest-tests</artifactId>
<version>2.6.0.BUILD-SNAPSHOT</version>
<version>2.6.0.DATAREST-573-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand All @@ -21,7 +21,7 @@
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-rest-tests-core</artifactId>
<version>2.6.0.BUILD-SNAPSHOT</version>
<version>2.6.0.DATAREST-573-SNAPSHOT</version>
<type>test-jar</type>
</dependency>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@
package org.springframework.data.rest.webmvc.jpa;

import org.springframework.data.repository.CrudRepository;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMethod;

/**
* @author Oliver Gierke
* @author Mark Paluch
*/
public interface AuthorRepository extends CrudRepository<Author, Long> {

}
@CrossOrigin(origins = "http://not.so.far.away", allowCredentials = "true",
methods = { RequestMethod.GET, RequestMethod.PATCH }, maxAge = 1234)
public interface AuthorRepository extends CrudRepository<Author, Long> {}
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@
package org.springframework.data.rest.webmvc.jpa;

import org.springframework.data.repository.CrudRepository;
import org.springframework.web.bind.annotation.CrossOrigin;

/**
* @author Greg Turnquist
* @author Oliver Gierke
* @author Mark Paluch
* @see DATAREST-463
*/
@CrossOrigin
public interface ItemRepository extends CrudRepository<Item, Long> {}
Loading