Spring Boot
CORS
application.properties
Cross-Origin Resource Sharing
configuration

Spring Boot enabling CORS by application.properties

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Spring Boot is a popular framework for building standalone, production-grade Spring-based applications. One of its many capabilities is simplifying the configuration of Cross-Origin Resource Sharing (CORS), which is a vital consideration when developing APIs and web applications. In this article, we will discuss how to enable CORS in a Spring Boot application using the application.properties file.

Understanding CORS

CORS is a mechanism that allows restricted resources on a web page to be requested from another domain outside the domain from which the first resource was served. This is crucial for APIs that wish to be accessed from different origins.

Configuring CORS in Spring Boot

Spring Boot offers multiple ways to configure CORS, including through annotations in controllers and global configurations. However, a global configuration using application.properties is often more manageable. Below, we will explore how to implement this configuration.

Global CORS Configuration

To enable CORS configuration for the entire Spring Boot application through application.properties, you can use Spring Security's CORS configuration properties. By tweaking specific properties, you can dictate application-wide CORS policies.

Enabling CORS via application.properties

To globally enable CORS in your Spring Boot application, edit your application.properties file:

  • spring.web.cors.allowed-origins: List the allowed domains. Multiple domains can be separated by commas. Use * to allow all domains.
  • spring.web.cors.allowed-methods: Specifies the HTTP methods allowed for cross-origin requests such as GET, POST, PUT, DELETE, OPTIONS.
  • spring.web.cors.allowed-headers: The headers that can be included in the request.
  • spring.web.cors.exposed-headers: Headers exposed to the client which can be useful for exposing security tokens, etc.
  • spring.web.cors.allow-credentials: Set true if the client is allowed to send cookies with requests.
  • spring.web.cors.max-age: How long, in seconds, the response from a preflight request can be cached by clients.
  • Security Considerations: While enabling CORS is necessary for modern web applications, it opens potential security risks. Carefully choose the domains and methods to allow.
  • Performance: Use the max-age configuration wisely to improve performance by reducing the need for preflight requests.

Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.