Spring Boot – CORS Support

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.maixuanviet.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");
         }
      };
   }
}

Now, you can create a Spring Boot web application that runs on 8080 port and your RESTful web service application that can run on the 9090 port. For further details about implementation about RESTful Web Service, you can refer to the chapter titled Consuming RESTful Web Services of this tutorial.

Related posts:

Date Time trong Java 8
Java Optional as Return Type
Spring Cloud AWS – S3
Xây dựng ứng dụng Client-Server với Socket trong Java
Guide to Selenium with JUnit / TestNG
Java Program to Generate All Subsets of a Given Set in the Lexico Graphic Order
Remove HTML tags from a file to extract only the TEXT
Refactoring Design Pattern với tính năng mới trong Java 8
Spring Security – security none, filters none, access permitAll
Java Program to Implement Dijkstra’s Algorithm using Priority Queue
Java Program to Implement Self Balancing Binary Search Tree
Convert char to String in Java
Hướng dẫn sử dụng lớp Console trong java
Java Program to implement Dynamic Array
Hướng dẫn Java Design Pattern – Abstract Factory
Java Program to Implement the Checksum Method for Small String Messages and Detect
Spring Boot - Google OAuth2 Sign-In
Chuyển đổi Array sang ArrayList và ngược lại
Why String is Immutable in Java?
An Example of Load Balancing with Zuul and Eureka
LinkedList trong java
Retrieve User Information in Spring Security
Spring WebClient Requests with Parameters
Java Program to Check whether Graph is a Bipartite using BFS
Java Program to Implement Nth Root Algorithm
Java – Reader to String
Java Program to Implement DelayQueue API
Java Program to Generate All Possible Subsets with Exactly k Elements in Each Subset
Quản lý bộ nhớ trong Java với Heap Space vs Stack
Java Program to Implement the Bin Packing Algorithm
New Features in Java 15
Tránh lỗi ConcurrentModificationException trong Java như thế nào?