CORS Configuration for Secure APIs in Spring Boot

In today’s web applications, security and access control are top priorities. One crucial aspect often overlooked is CORS (Cross-Origin Resource Sharing). Without proper configuration, your API could be exposed to unintended domains. In this post, we’ll walk through everything you need to know about CORS Configuration for Secure APIs using Spring Boot.

We’ll explore what CORS is, why it matters, and how to configure it effectively for secure REST APIs.

CORS Configuration for Secure APIs in Spring Boot

🚦 What is CORS?

CORS is a browser security feature that restricts HTTP requests from scripts running in the browser on a different origin (domain, protocol, or port) than the server.

For example, a frontend running on http://localhost:3000 trying to call a backend API at http://localhost:8080 will trigger a CORS preflight request. If not configured properly, the browser will block the call.

🧠 Why CORS Matters for Secure APIs

  • Prevent unauthorized domains from accessing your APIs.
  • Control which methods, headers, and origins are allowed.
  • Enable frontend-backend interaction across domains securely.

πŸ› οΈ Project Setup

Ensure your Spring Boot project has the following setup:

Dependencies (pom.xml):

🧩 CORS Configuration for Secure APIs (Global Setup)

Use the following configuration class to apply global CORS rules.

πŸ“ com.kscodes.springboot.security.config.WebSecurityConfig.java

πŸ§ͺ Test Controller

πŸ“ com.kscodes.springboot.security.controller.ApiController.java

🎯 Key Takeaways

  • Use CorsConfiguration to define which origins, methods, and headers are allowed.
  • Use CorsConfigurationSource and register it globally.
  • You can restrict secure APIs to only allow trusted domains.

πŸ” Fine-Grained CORS on Specific Controller

If you don’t want to use a global CORS config, you can use @CrossOrigin on controllers.

Use this sparingly if you want per-controller CORS Configuration for Secure APIs.

πŸ”„ CORS and Preflight Explained

Before making a real request, browsers often send a preflight OPTIONS request to verify CORS permissions.

Make sure your backend:

  • Responds to OPTIONS
  • Does not block it via CSRF or security filters

πŸ“Œ Conclusion

Proper CORS Configuration for Secure APIs is essential for enabling cross-domain requests safely. Spring Boot offers flexible options to configure CORS both globally and per-controller.

By limiting CORS to trusted origins and enabling only required methods and headers, you can significantly improve the security posture of your APIs.

πŸ”— Further Reading