0% found this document useful (0 votes)
7 views

CORS

Cross-Origin Resource Sharing (CORS) is a security feature in web browsers that restricts JavaScript requests to different origins. To handle CORS issues, RESTful web services must support CORS and allow access from specific ports, which can be configured using the @CrossOrigin annotation for individual methods or globally through WebMvcConfigurer in a Spring Boot application. The document provides examples of how to enable CORS for a RESTful web service application using these configurations.

Uploaded by

Suresh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

CORS

Cross-Origin Resource Sharing (CORS) is a security feature in web browsers that restricts JavaScript requests to different origins. To handle CORS issues, RESTful web services must support CORS and allow access from specific ports, which can be configured using the @CrossOrigin annotation for individual methods or globally through WebMvcConfigurer in a Spring Boot application. The document provides examples of how to enable CORS for a RESTful web service application using these configurations.

Uploaded by

Suresh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 1

Cross-Origin Resource Sharing (CORS) is a security concept that allows restricting the resources implemented in web

browsers. It prevents the JavaScript code producing or consuming the requests against different origin.

For example, your web application is running on 8080 port and by using JavaScript you are trying to consuming
RESTful web services from 9090 port. Under such situations, you will face the Cross-Origin Resource Sharing security
issue on your web browsers.

Two requirements are needed to handle this issue −

 RESTful web services should support the Cross-Origin Resource Sharing.


 RESTful web service application should allow accessing the API(s) from the 8080 port.
In this chapter, we are going to learn in detail about How to Enable Cross-Origin Requests for a RESTful Web Service
application.

Enable CORS in Controller Method

We need to set the origins for RESTful web service by using @CrossOrigin annotation for the controller method. This
@CrossOrigin annotation supports specific REST API, and not for the entire application.

@RequestMapping(value = "/products")
@CrossOrigin(origins = "http://localhost:8080")

public ResponseEntity<Object> getProduct() {


return null;
}

Global CORS Configuration

We need to define the shown @Bean configuration to set the CORS configuration support globally to your Spring Boot
application.

@Bean
public WebMvcConfigurer corsConfigurer() {
return new WebMvcConfigurerAdapter() {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/products").allowedOrigins("http://localhost:9000");
}
};
}

To code to set the CORS configuration globally in main Spring Boot application is given below.

package com.tutorialspoint.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@Bean
public WebMvcConfigurer corsConfigurer() {
return new WebMvcConfigurerAdapter() {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/products").allowedOrigins("http://localhost:8080");
}
};
}
}

You might also like