CORS

About

Cross-Origin Resource Sharing (CORS) is a security feature enforced by web browsers to restrict how resources on a web page can be requested from a different origin (domain, protocol, or port).

By default, browsers block cross-origin HTTP requests made via fetch, XHR (XMLHttpRequest), or AJAX unless explicitly allowed by the server.

For Example,

Blocked by CORS A website running on https://example.com tries to fetch data from https://api.otherdomain.com.

Allowed by CORS (If API Allows) The server at https://api.otherdomain.com responds with a special CORS header (Access-Control-Allow-Origin: https://example.com), telling the browser it’s safe to proceed.

Why is CORS Needed?

Modern web applications often consume APIs hosted on different domains. Without CORS, these requests would be blocked by the browser's Same-Origin Policy (SOP).

CORS bypasses SOP securely by allowing the server to specify which domains can access its resources.

How CORS Works?

Imagine we own a bank website (https://mybank.com) that needs to fetch account details from an API server(https://api.mybank.com).

Problem: By default, web browsers block requests from one domain (mybank.com) to another (api.mybank.com) due to Same-Origin Policy (SOP)—a security rule that prevents unauthorized access to user data.

Solution: The API server (https://api.mybank.com) must explicitly allow requests from https://mybank.com using CORS headers in its response.

CORS Request Flow

1 - The browser sends a request to https://api.mybank.com asking, “Can I access your data?”

2 - The API server responds with CORS headers, allowing or denying access.

Allowed Response:

Blocked Response (No CORS Headers):

3 - If allowed, the browser processes the response and displays the data. Otherwise, it blocks the request.

Preflight Requests (Extra Security Check)

If the request uses special HTTP methods (POST, PUT, DELETE) or custom headers, the browser first sends a preflight request (OPTIONS request) to ask:

Browser: "API, are you okay with me sending a DELETE request?"

API Server: "Yes! I allow DELETE requests from https://mybank.com."

Types of CORS Requests

1. Simple Requests

A request is Simple if:

  • It uses GET, POST, or HEAD.

  • It has only allowed headers (e.g., Content-Type: application/x-www-form-urlencoded).

Example (Simple CORS Request)

Server Response Headers (Success)

2. Preflight Requests (For Non-Simple Requests)

A Preflight Request is an OPTIONS request sent before the actual request to check if the server allows CORS.

  • Happens when using methods like PUT, DELETE, PATCH.

  • Happens when sending custom headers.

  • Ensures security before sending sensitive data.

Example (Preflight Request - Sent by Browser Automatically)

Server Response (Approving CORS Request)

Access-Control-Allow-Origin → Specifies allowed domains.

Access-Control-Allow-Methods → Lists allowed HTTP methods.

Access-Control-Allow-Headers → Lists allowed request headers.

Access-Control-Max-Age → Specifies how long the preflight response is cached.

3. Credentialed Requests (Cookies, Authorization Headers, etc.)

If a request includes credentials (cookies, authentication headers), the server must allow it explicitly.

Client Request with Credentials

Server Response for Credentialed Requests

  • Access-Control-Allow-Credentials: true MUST be set to allow credentials.

  • Access-Control-Allow-Origin: * CANNOT be used with credentials.

How to Enable CORS in Spring Boot

1. Global CORS Configuration (For All Endpoints)

2. CORS for a Specific Controller

  • @CrossOrigin enables CORS only for this controller.

  • The allowedHeaders and methods define which requests are allowed.

3. CORS Configuration for Spring Security

If Spring Security is enabled, we must configure CORS explicitly in SecurityFilterChain.

  • If Spring Security is enabled, CORS must be configured inside the security filter chain.

  • setAllowedOrigins(List.of("https://example.com")) → Allows requests only from example.com.

  • setAllowCredentials(true) → Allows credentials (cookies, authentication headers).

Common CORS Issues and Fixes

Issue

Cause

Fix

CORS policy error: No 'Access-Control-Allow-Origin' header

Server did not include the CORS headers

Ensure server explicitly allows the requesting domain

Preflight request blocked

Server did not allow the method or headers in OPTIONSresponse

Add Access-Control-Allow-Methods and Access-Control-Allow-Headers

Credentials request blocked

Access-Control-Allow-Credentials is missing

Add Access-Control-Allow-Credentials: true and avoid * in Access-Control-Allow-Origin

Last updated