Quick revision
The short answer
A PUT with a JSON body is not a simple request, so before sending it the browser asks the API with a preflight OPTIONS request whether app.example.com may send a PUT with a Content-Type: application/json header. The API answers without the right Access-Control-Allow-* headers, or rejects the OPTIONS request, so the browser blocks the PUT. We fix it by configuring CORS once, with WebMvcConfigurer.addCorsMappings or a CorsConfigurationSource, enabling it in Spring Security with http.cors(...) so preflights are handled before authentication, and listing an explicit origin rather than * when credentials are used.
What to remember
In this answer, we saw that a PUT with a JSON body triggers a preflight OPTIONS request, which the server must answer with the allowed origin, methods and headers before the browser sends the real request. We configured CORS globally with WebMvcConfigurer.addCorsMappings or a CorsConfigurationSource, enabled it in Spring Security with http.cors(...) so preflights are answered before authentication, and noted that allowed origin * cannot be combined with credentials.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
A PUT with a JSON body is not a simple request, so before sending it the browser asks the API with a preflight OPTIONS request whether app.example.com may send a PUT with a Content-Type: application/json header. The API answers without the right Access-Control-Allow-* headers, or rejects the OPTIONS request, so the browser blocks the PUT. We fix it by configuring CORS once, with WebMvcConfigurer.addCorsMappings or a CorsConfigurationSource, enabling it in Spring Security with http.cors(...) so preflights are handled before authentication, and listing an explicit origin rather than * when credentials are used.
Why this question is asked
Single page applications almost always live on a different origin from their API, and the first time a team adds an update form, it hits a CORS error that GET requests never showed. The error message in the browser is vague, the server logs may show nothing, and people start adding annotations at random. Interviewers ask this to see whether we understand the browser's side of the protocol and where Spring answers it. In this answer, we will recap what an origin and CORS are, see why only the PUT fails, configure Spring MVC and Spring Security correctly, and look at the credentials rule that catches many teams.
1A Quick Recap of Origins and CORS
An origin is the scheme, host and port of a page, such as https://app.example.com. Browsers apply the same-origin policy: JavaScript on one origin may not read responses from another origin unless that other server allows it. https://api.example.com is a different origin, even though both share example.com. CORS, Cross-Origin Resource Sharing, is the protocol the server uses to say which other origins may call it, through response headers such as Access-Control-Allow-Origin.
Notice that CORS is enforced by the browser, not the server. A curl request to the same API works perfectly, which is why the bug seems to exist only in the browser.
2Why Only PUT With JSON Fails
The browser sorts cross-origin requests into two groups. A simple request uses GET, HEAD or POST, with only basic headers and a Content-Type of text/plain, multipart/form-data or application/x-www-form-urlencoded. The browser sends it directly and only checks the Access-Control-Allow-Origin header of the response. Everything else, including any PUT, DELETE or PATCH, a JSON Content-Type, or a custom header such as Authorization, needs a preflight first.
A preflight is an OPTIONS request that carries no body and no credentials. It asks: may this origin use this method with these headers? Only if the answer allows it does the browser send the real request.
6 messages in order. Play them, or step through them one at a time.
So the GET worked because it was a simple request and the API, or a @CrossOrigin somewhere, added Access-Control-Allow-Origin. The PUT fails because its preflight gets an answer without PUT in Allow-Methods, without content-type in Allow-Headers, or with a 401 or 403 from security. Max-Age lets the browser cache a successful preflight, here for an hour, so it is not repeated before every call.
3Configuring CORS in Spring MVC
Without Spring Security, the global way to configure CORS is a WebMvcConfigurer. It applies to all handlers, including the automatic preflight handling.
This is better than @CrossOrigin on individual controllers, because one place defines the policy and nobody forgets a new controller.
4Configuring CORS in Spring Security
With Spring Security in the chain, the security filters run before Spring MVC. If they require authentication and the preflight carries no credentials, they reject it with 401 or 403 before the MVC configuration ever sees it. So CORS must be handled inside the security chain, by a CorsFilter that runs before authentication.
http.cors(Customizer.withDefaults()) adds the CorsFilter early in the chain. For a valid preflight it writes the Access-Control-Allow-* headers and returns at once, so authentication never sees it; for a real request it adds the headers and continues. The older no-argument http.cors() style is gone in recent Spring Security versions, so the lambda form is the one to use. One CorsConfigurationSource bean is then the single source of truth for both Security and MVC.
5The Credentials and Wildcard Rule
If the SPA sends cookies or uses fetch with credentials: 'include', the response must contain Access-Control-Allow-Credentials: true, and then the browser refuses Access-Control-Allow-Origin: *. The server has to echo the exact origin. Spring enforces this too: allowCredentials(true) together with allowedOrigins("*") fails with an IllegalArgumentException when the configuration is used. If we really need several subdomains, allowedOriginPatterns("https://*.example.com") echoes the matching origin, but each pattern widens who can make credentialed calls, so an explicit list is safer. Allowing all headers with * is also tempting; listing the ones the SPA really sends keeps the policy readable and reviewable.
6Follow-up questions
Is CORS a security feature of the API?
Not really. It protects users' browsers from scripts on other sites reading responses with the user's credentials. It does not stop curl, a server, or an attacker's script from calling the API directly, so authentication and authorization are still needed on every endpoint.
Why not simply permit all OPTIONS requests in Spring Security?
Permitting OPTIONS /** without CORS configuration makes the preflight pass security, but then nothing adds the Access-Control-Allow-* headers unless MVC handles it, and it also opens every OPTIONS handler to anonymous callers. Letting the CorsFilter answer preflights is precise and keeps the policy in one place.
Could we avoid CORS completely?
Yes, by serving the SPA and the API from the same origin, for example with a reverse proxy that routes /api to the backend. Then the browser makes same-origin calls and no preflight is needed. That is often the simplest architecture, but when the API has its own domain, a correct CORS configuration is the answer.
7Conclusion
In this answer, we saw that a PUT with a JSON body triggers a preflight OPTIONS request, which the server must answer with the allowed origin, methods and headers before the browser sends the real request. We configured CORS globally with WebMvcConfigurer.addCorsMappings or a CorsConfigurationSource, enabled it in Spring Security with http.cors(...) so preflights are answered before authentication, and noted that allowed origin * cannot be combined with credentials.
Open with
The GET works because it is a simple request, so the browser sends it directly and only checks Access-Control-Allow-Origin on the response. A PUT with Content-Type application/json is not simple, so the browser first sends a preflight: an OPTIONS request with Origin, Access-Control-Request-Method PUT and Access-Control-Request-Headers content-type, and without any credentials.
Then, if they want more
Unless the API answers it with the allowed origin, methods and headers, the browser never sends the PUT and shows a CORS error. app.example.com and api.example.com are different origins, so this applies. In Spring I would define the policy once. Without Security, a WebMvcConfigurer with addCorsMappings is enough.
With Spring Security, I would define a CorsConfigurationSource bean with the exact origin, the methods including PUT, the headers the SPA sends, allowCredentials if it uses cookies or tokens, and a max age, and enable it with http.cors(Customizer.withDefaults()). That puts the CorsFilter before authentication, so the preflight is answered and never rejected with 401.
I would list the origin explicitly, because Access-Control-Allow-Origin star cannot be combined with credentials, and Spring rejects that combination anyway.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Spring Framework reference, Spring Web MVC
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "A browser SPA on app.example.com calls an API on api.example.com and requests fail with...": https://scrivio-interview-library.netlify.app/spring-boot/rest-controllers/2ef75e32f5/