From 4405ae4eafec8438b988a2b1af63c0c0dbfa51b0 Mon Sep 17 00:00:00 2001 From: Sebastien Deleuze Date: Thu, 16 Jul 2015 18:11:48 +0200 Subject: [PATCH] Add CORS documentation Closes gh-3052 --- .../main/asciidoc/spring-boot-features.adoc | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc b/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc index 1fa755ccfa..d4a3cb3fbb 100644 --- a/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc +++ b/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc @@ -1429,6 +1429,43 @@ described above. +[[boot-features-cors]] +==== CORS support + +http://en.wikipedia.org/wiki/Cross-origin_resource_sharing[Cross-origin resource sharing] +(CORS) is a http://www.w3.org/TR/cors/[W3C specification] implemented by +http://caniuse.com/#feat=cors[most browsers] that allows you to specify in a flexible +way what kind of cross domain requests are authorized, instead of using some less secure +and less powerful approaches like IFRAME or JSONP. + +As of version 4.2, Spring MVC {spring-reference}/#cors[supports CORS] out of the box. +Using {spring-reference}/#_controller_method_cors_configuration[controller method CORS +configuration] with +{spring-javadoc}/org/springframework/web/bind/annotation/CrossOrigin.html[`@CrossOrigin`] +annotations in your Spring Boot application does not require any specific configuration. +{spring-reference}/#_global_cors_configuration[Global CORS configuration] can be defined +by registering a `WebMvcConfigurer` bean with a customized `addCorsMappings(CorsRegistry)` +method: + +[source,java,indent=0] +---- + @Configuration + public class MyConfiguration { + + @Bean + public WebMvcConfigurer corsConfigurer() { + return new WebMvcConfigurerAdapter() { + @Override + public void addCorsMappings(CorsRegistry registry) { + registry.addMapping("/api/**"); + } + }; + } + } +---- + + + [[boot-features-jersey]] === JAX-RS and Jersey If you prefer the JAX-RS programming model for REST endpoints you can use one of the