From the course: Complete Guide to Spring MVC

Unlock this course with a free trial

Join today to access over 24,600 courses taught by industry experts.

CORS introduction

CORS introduction

- [Instructor] Now, we are going to officially introduce the class to CORS. What is CORS? CORS is Cross-Origin Resource Sharing. It is a mechanism that allows a web application running on one domain, which is the origin, to access resources on another domain. It adds HTTP headers to server responses to indicate whether the requesting domain is permitted to access the service resources. Without CORS, browsers block cross-origin requests due to security restrictions. For example, if a front-end app running at localhost:3000 attempts to access a backend API of localhosts:8080/greeting, the browser blocks this request unless CORS is configured properly. Spring MVC provides multiple ways to configure CORS, allowing you to specify which origins, which HTTP methods, or which headers are allowed in cross-origin requests. The two main ways to handle CORS are with a peer controller or peer method with the annotation of CrossOrigin or globally via WebMvcConfigurer. For example, here in our…

Contents