Skip to content

Commit a7d8103

Browse files
committed
Polish CORS documentation in the reference manual
1 parent 39d689d commit a7d8103

File tree

1 file changed

+28
-23
lines changed

1 file changed

+28
-23
lines changed

src/asciidoc/web-cors.adoc

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,19 @@
55

66
For security reasons, browsers prohibit AJAX calls to resources residing outside the
77
current origin. For example, as you're checking your bank account in one tab, you
8-
could have the evil.com website in another tab. The scripts from evil.com should not
9-
be able to make AJAX requests to your bank API (withdrawing money from your account!)
8+
could have the evil.com website open in another tab. The scripts from evil.com should not
9+
be able to make AJAX requests to your bank API (e.g., withdrawing money from your account!)
1010
using your credentials.
1111

1212
http://en.wikipedia.org/wiki/Cross-origin_resource_sharing[Cross-origin resource sharing]
1313
(CORS) is a http://www.w3.org/TR/cors/[W3C specification] implemented by
1414
http://caniuse.com/#feat=cors[most browsers] that allows you to specify in a flexible
1515
way what kind of cross domain requests are authorized, instead of using some less secured
16-
and less powerful hacks like IFrame or JSONP.
16+
and less powerful hacks like IFRAME or JSONP.
1717

1818
As of Spring Framework 4.2, CORS is supported out of the box. CORS requests
1919
(https://github.com/spring-projects/spring-framework/blob/master/spring-webmvc/src/main/java/org/springframework/web/servlet/FrameworkServlet.java#L906[including preflight ones with an `OPTIONS` method])
20-
are automatically dispatched to the various `HandlerMapping` registered. They handle
20+
are automatically dispatched to the various registered ++HandlerMapping++s. They handle
2121
CORS preflight requests and intercept CORS simple and actual requests thanks to a
2222
http://docs.spring.io/spring/docs/4.2.x/javadoc-api/org/springframework/web/cors/CorsProcessor.html[CorsProcessor]
2323
implementation (https://github.com/spring-projects/spring-framework/blob/master/spring-web/src/main/java/org/springframework/web/cors/DefaultCorsProcessor.java[DefaultCorsProcessor]
@@ -26,17 +26,18 @@ based on the CORS configuration you have provided.
2626

2727
[NOTE]
2828
====
29-
Since CORS requests are automatically dispatched, you *do not need* to change
30-
`DispatcherServlet` `dispatchOptionsRequest` init parameter value, using its default value
29+
Since CORS requests are automatically dispatched, you *do not need* to change the
30+
`DispatcherServlet` `dispatchOptionsRequest` init parameter value; using its default value
3131
(`false`) is the recommended approach.
3232
====
3333

3434
== Controller method CORS configuration
3535

36-
You can add to your `@RequestMapping` annotated handler method a
36+
You can add an
3737
http://docs.spring.io/spring/docs/4.2.x/javadoc-api/org/springframework/web/bind/annotation/CrossOrigin.html[`@CrossOrigin`]
38-
annotation in order to enable CORS on it (by default `@CrossOrigin` allows all origins
39-
and the HTTP methods specified in the `@RequestMapping` annotation):
38+
annotation to your `@RequestMapping` annotated handler method in order to enable CORS on
39+
it. By default `@CrossOrigin` allows all origins and the HTTP methods specified in the
40+
`@RequestMapping` annotation:
4041

4142
[source,java,indent=0]
4243
[subs="verbatim,quotes"]
@@ -51,7 +52,7 @@ public class AccountController {
5152
// ...
5253
}
5354
54-
@RequestMapping(method = RequestMethod.DELETE, value = "/{id}")
55+
@RequestMapping(method = RequestMethod.DELETE, path = "/{id}")
5556
public void remove(@PathVariable Long id) {
5657
// ...
5758
}
@@ -73,16 +74,19 @@ public class AccountController {
7374
// ...
7475
}
7576
76-
@RequestMapping(method = RequestMethod.DELETE, value = "/{id}")
77+
@RequestMapping(method = RequestMethod.DELETE, path = "/{id}")
7778
public void remove(@PathVariable Long id) {
7879
// ...
7980
}
8081
}
8182
----
8283

83-
In this example CORS support is enabled for both `retrieve()` and `remove()` handler methods, and you can also see how you can customize the CORS configuration using `@CrossOrigin` attributes.
84+
In the above example CORS support is enabled for both the `retrieve()` and the `remove()`
85+
handler methods, and you can also see how you can customize the CORS configuration using
86+
`@CrossOrigin` attributes.
8487

85-
You can even use both controller and method level CORS configurations, Spring will then combine both annotation attributes to create a merged CORS configuration.
88+
You can even use both controller-level and method-level CORS configurations; Spring will
89+
then combine attributes from both annotations to create merged CORS configuration.
8690

8791
[source,java,indent=0]
8892
[subs="verbatim,quotes"]
@@ -98,7 +102,7 @@ public class AccountController {
98102
// ...
99103
}
100104
101-
@RequestMapping(method = RequestMethod.DELETE, value = "/{id}")
105+
@RequestMapping(method = RequestMethod.DELETE, path = "/{id}")
102106
public void remove(@PathVariable Long id) {
103107
// ...
104108
}
@@ -109,8 +113,8 @@ public class AccountController {
109113

110114
In addition to fine-grained, annotation-based configuration you'll probably want to
111115
define some global CORS configuration as well. This is similar to using filters but can
112-
be declared withing Spring MVC and combined with fine-grained `@CrossOrigin` configuration.
113-
By default all origins and `GET`, `HEAD` and `POST` methods are allowed.
116+
be declared within Spring MVC and combined with fine-grained `@CrossOrigin` configuration.
117+
By default all origins and `GET`, `HEAD`, and `POST` methods are allowed.
114118

115119
=== JavaConfig
116120

@@ -154,7 +158,8 @@ public class WebConfig extends WebMvcConfigurerAdapter {
154158

155159
=== XML namespace
156160

157-
This minimal XML configuration enable CORS on `/**` path pattern with the same default properties than the JavaConfig one:
161+
The following minimal XML configuration enables CORS for the `/**` path pattern with
162+
the same default properties as with the aforementioned JavaConfig examples:
158163

159164
[source,xml,indent=0]
160165
[subs="verbatim"]
@@ -184,18 +189,18 @@ It is also possible to declare several CORS mappings with customized properties:
184189
</mvc:cors>
185190
----
186191

187-
== Advanced Customizations
192+
== Advanced Customization
188193

189194
http://docs.spring.io/spring/docs/4.2.x/javadoc-api/org/springframework/web/cors/CorsConfiguration.html[CorsConfiguration]
190195
allows you to specify how the CORS requests should be processed: allowed origins, headers, methods, etc.
191196
It can be provided in various ways:
192197

193198
* http://docs.spring.io/spring/docs/4.2.x/javadoc-api/org/springframework/web/servlet/handler/AbstractHandlerMapping.html#setCorsConfiguration-java.util.Map-[`AbstractHandlerMapping#setCorsConfiguration()`]
194199
allows to specify a `Map` with several http://docs.spring.io/spring/docs/4.2.x/javadoc-api/org/springframework/web/cors/CorsConfiguration.html[CorsConfiguration]
195-
mapped on path patterns like `/api/**`
196-
* Subclasses can provide their own `CorsConfiguration` by overriding
197-
`AbstractHandlerMapping#getCorsConfiguration(Object, HttpServletRequest)` method
198-
* Handlers can implement http://docs.spring.io/spring/docs/4.2.x/javadoc-api/org/springframework/web/cors/CorsConfigurationSource.html[`CorsConfigurationSource`]
200+
instances mapped to path patterns like `/api/**`.
201+
* Subclasses can provide their own `CorsConfiguration` by overriding the
202+
`AbstractHandlerMapping#getCorsConfiguration(Object, HttpServletRequest)` method.
203+
* Handlers can implement the http://docs.spring.io/spring/docs/4.2.x/javadoc-api/org/springframework/web/cors/CorsConfigurationSource.html[`CorsConfigurationSource`]
199204
interface (like https://github.com/spring-projects/spring-framework/blob/master/spring-webmvc/src/main/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandler.java[`ResourceHttpRequestHandler`]
200205
now does) in order to provide a http://docs.spring.io/spring/docs/4.2.x/javadoc-api/org/springframework/web/cors/CorsConfiguration.html[CorsConfiguration]
201-
for each request.
206+
instance for each request.

0 commit comments

Comments
 (0)