liveBook · Manning

CORS

As explained in Spring in Action, Sixth Edition

CORS is a web security mechanism that allows a server to declare which origins may access its resources from browser-based clients.

CORS (cross-origin resource sharing) lets a server declare which browser-based clients may access its resources. Browsers commonly block a JavaScript interface hosted on a different host or port from consuming an API; CORS response headers provide the permitted exception. section 7.1.1

In Spring, apply CORS with @CrossOrigin. Set origins to the allowed client origin, such as http://tacocloud:8080, or provide an array when multiple origins need access. section 7.1.1

Allow a separate Taco Cloud client

@RestController @RequestMapping(path="/api/tacos", produces="application/json") @CrossOrigin(origins={"http://tacocloud:8080", "http://tacocloud.com"}) public class TacoController { ... } This controller permits browser clients from the two listed origins to consume the JSON API.

How Spring in Action, Sixth Edition covers it

How deep it goes
Explains the browser restriction and demonstrates annotation-based origin configuration, but does not detail CORS protocol exchanges.
Examples
Demonstrates @CrossOrigin on TacoController with one or multiple allowed origins.
What it assumes
Assumes basic familiarity with browser clients and REST controllers while introducing cross-origin browser restrictions directly.
What you can do after
Apply CORS via Spring's @CrossOrigin annotation to permit named browser client origins.
How it connects
Connects CORS with browsers, JavaScript clients, REST APIs, Spring, and HTTP response headers.

Also explained in

Compare how 5 books explain CORS →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Spring in Action, Sixth Edition. The section links point to the parts of the book it draws on.