Quick revision
The short answer
The PUT needs a CORS preflight, an OPTIONS request that the browser sends without the Authorization header. Spring Security sees an unauthenticated request and rejects it with 401 before any CORS handling runs, so the browser never gets permission and never sends the PUT. The fix is to configure CORS inside Spring Security with http.cors(...) and a CorsConfigurationSource, which answers preflights before authentication, with explicit origins, methods and headers. GET worked because a simple GET needs no preflight.
What to remember
In this answer, we read the network tab and saw that the failing request was the preflight OPTIONS, which browsers send without credentials whenever a request uses PUT, a JSON body or custom headers. Spring Security rejected it with 401 before the MVC-level @CrossOrigin could help. Configuring CORS in the security chain with http.cors(...) and a CorsConfigurationSource answers preflights before authentication, with explicit origins, methods and headers, and simple GET requests never needed a preflight in the first place.
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.
The PUT needs a CORS preflight, an OPTIONS request that the browser sends without the Authorization header. Spring Security sees an unauthenticated request and rejects it with 401 before any CORS handling runs, so the browser never gets permission and never sends the PUT. The fix is to configure CORS inside Spring Security with http.cors(...) and a CorsConfigurationSource, which answers preflights before authentication, with explicit origins, methods and headers. GET worked because a simple GET needs no preflight.
Why this question is asked
This bug confuses people because the API looks fine: the same PUT works from curl and Postman, GET works from the browser, and the controller even has @CrossOrigin. The network tab tells the real story if we know how to read it: the failing request is not the PUT at all, but an OPTIONS request in front of it. Interviewers use it to check that we understand both the browser's CORS rules and the order of Spring Security's filters. In this answer, we will read the evidence, explain why the preflight gets 401, fix the configuration, and verify the fix with curl.
1Reading the Network Tab
In the browser's developer tools, the failing call shows two entries: an OPTIONS /orders/1 with status 401, and then the PUT marked as blocked by CORS policy, never actually sent. The OPTIONS entry is the preflight. A preflight is a request the browser sends on its own before a cross-origin request that is not simple, to ask the server whether the real request is allowed.
A request is simple only if it uses GET, HEAD or POST, adds no custom headers, and has a Content-Type of text/plain, form-urlencoded or multipart. Our PUT breaks all three rules: the method is PUT, it sends Content-Type: application/json, and it sends an Authorization header with a bearer token. The GET in this app is sent as a simple request: it has no body and no custom header, for example because it reads a public endpoint or relies on a cookie. So the browser sends it directly, without a preflight, and that is why it worked.
2Why the Preflight Gets 401
The key fact is that browsers send preflights without credentials. There is no Authorization header and no cookies, only Origin, Access-Control-Request-Method and Access-Control-Request-Headers. That is by design: the browser is asking for permission before sharing anything.
4 messages in order. Play them, or step through them one at a time.
@CrossOrigin in Spring MVC is never consulted, because security answers first.
Spring Security's filters run before the DispatcherServlet. The authorization rule says every request must be authenticated, the preflight has no token, so the security chain answers 401 and stops. The @CrossOrigin annotation lives in Spring MVC, behind the security chain, so it never sees the preflight. The browser receives a 401 without any Access-Control-Allow-* headers and concludes that the PUT is not allowed.
3The Fix: Let Spring Security Handle CORS
We move the CORS policy into the security chain. http.cors(...) adds a CorsFilter before the authentication filters. When it sees a valid preflight, it writes the CORS headers and ends the request at once, so authentication never runs for it.
Notice that we list the exact origin, the methods and the headers the SPA sends. Allowing the origin * with credentials is not permitted by browsers, and Spring rejects that combination. With a bearer token in the Authorization header and no cookies, allowCredentials is not required, but the Authorization header must be listed in the allowed headers. Once this works, the @CrossOrigin annotation can be removed so there is only one policy.
Some teams fix the symptom with requestMatchers(HttpMethod.OPTIONS, "/**").permitAll(). That lets the preflight through security, but it only works if something else then adds the CORS headers, and it opens every OPTIONS request to anonymous callers. Handling CORS in the security chain is the precise fix.
4Verifying the Fix With curl
We can reproduce the browser's preflight exactly, which is much faster than reloading the app.
The second call checks that an unknown origin is still refused. A MockMvc test with the same headers keeps both behaviours from regressing. And if a gateway or load balancer sits in front, we repeat the curl through it, because some gateways answer or strip OPTIONS requests themselves.
5Follow-up questions
Why does the same PUT work from Postman?
Postman is not a browser, so it does not apply the same-origin policy and never sends a preflight. It sends the PUT with the token directly, security authenticates it, and the controller answers. CORS only exists in browsers.
Would a GET with an Authorization header also need a preflight?
Yes. Authorization is not one of the headers allowed in a simple request, so a GET that sends a bearer token is preflighted too. If the GET in our case worked without CORS in security, it was sent without that header, for example with a cookie, or the app only adds the token to write calls.
Can we cache preflights to reduce the number of OPTIONS calls?
Yes, with Access-Control-Max-Age, set by maxAge in the CORS configuration. The browser then reuses the answer for that origin, URL and method for the given number of seconds. Browsers cap the value, Chrome at two hours, so very large values have no effect.
6Conclusion
In this answer, we read the network tab and saw that the failing request was the preflight OPTIONS, which browsers send without credentials whenever a request uses PUT, a JSON body or custom headers. Spring Security rejected it with 401 before the MVC-level @CrossOrigin could help. Configuring CORS in the security chain with http.cors(...) and a CorsConfigurationSource answers preflights before authentication, with explicit origins, methods and headers, and simple GET requests never needed a preflight in the first place.
Open with
The key is in the network tab: the 401 is on the OPTIONS request, not on the PUT. A PUT with a JSON body and an Authorization header is not a simple CORS request, so the browser first sends a preflight OPTIONS asking whether the origin may use PUT with those headers.
Then, if they want more
Browsers send that preflight without credentials, so there is no token. Spring Security's filters run before Spring MVC, the rule says every request must be authenticated, so the preflight gets 401 without any Access-Control-Allow headers, and the browser blocks the PUT. Any @CrossOrigin on the controller never runs because the request never reaches MVC.
The GET worked because it was a simple request with no preflight. The fix is to configure CORS in Spring Security: http.cors(Customizer.withDefaults()) plus a CorsConfigurationSource bean with the exact origin, the methods including PUT, and the headers Authorization and Content-Type. That puts the CorsFilter before authentication, so preflights are answered directly.
I would avoid wildcard origins with credentials, which Spring rejects anyway, and I would verify with a curl OPTIONS request carrying Origin and Access-Control-Request-Method, plus a MockMvc test.
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 app gets a CORS error on a PUT request. The network tab shows the OPTIONS...": https://scrivio-interview-library.netlify.app/spring-boot/rest-controllers/771d5e5927/