Scrivio Interview library
DebuggingFoundations6 min read · updated 12 November 2026

A React app on https://app.example.com calls a Spring Boot API on https://api.example.com and the browser reports a CORS error on preflight. The controller has @CrossOrigin and it still fails when Spring Security is enabled. Why?

The browser's preflight is an OPTIONS request without credentials, and Spring Security's filters run before Spring MVC, so they reject it as unauthenticated before @CrossOrigin is ever applied. Enable CORS in Spring Security with http.cors(Customizer.withDefaults()) and a CorsConfigurationSource bean, so its CorsFilter answers the preflight early in the chain. Allow the methods and headers the client sends, such as Authorization and Content-Type, and do not combine allowedOrigins("*") with allowCredentials(true); remember that CORS is enforced only by browsers.

Why this question is asked

Almost every team that puts a React or Angular front end on a different subdomain from its API hits this error once. The @CrossOrigin annotation is right there on the controller, it worked before security was added, and the browser console still says the preflight failed. Interviewers like the question because the answer depends on knowing the order in which a request meets Spring's components. In this answer, we will recap what CORS and a preflight are, see why the annotation is never reached, fix it with Spring Security's CORS support, get the configuration details right, and be clear about what CORS does not protect.

1A Quick Recap of CORS and Preflight

Browsers apply the same-origin policy: a page from https://app.example.com may send requests to https://api.example.com, but by default its JavaScript cannot read the responses, because the origin (scheme, host and port) differs. CORS (cross-origin resource sharing) is how the API tells the browser that a given origin is allowed, through response headers such as Access-Control-Allow-Origin.

For requests that are not simple, for example a request with an Authorization header or a JSON Content-Type, the browser first sends a preflight: an OPTIONS request asking whether the real request is allowed. It carries headers like Origin, Access-Control-Request-Method and Access-Control-Request-Headers, and, importantly, no cookies and no Authorization header. Only if the preflight response approves does the browser send the real request.

2Why @CrossOrigin Is Never Reached

@CrossOrigin is read by Spring MVC, inside the DispatcherServlet, when it maps a request to a controller. But Spring Security is a chain of servlet filters that runs before the DispatcherServlet. The preflight arrives without credentials, the authorization filter sees an anonymous request to a protected URL, and it answers 401 or 403. The request never reaches MVC, so the CORS headers from @CrossOrigin are never added, and the browser reports a CORS error.

Java
OPTIONS /orders HTTP/1.1
Origin: https://app.example.com
Access-Control-Request-Method: POST
Access-Control-Request-Headers: authorization, content-type
HTTP/1.1 401 Unauthorized <- from Spring Security, no Access-Control-Allow-Origin
// browser console: CORS preflight did not succeed; the POST is never sent
Step throughWhere the preflight is stopped
BrowserSpring Security filtersDispatcherServlet (@CrossOrigin)
OPTIONS /orders (no credentials)
Anonymous request to protected URL
401, no CORS headers
Report CORS error; skip the POST

4 messages in order. Play them, or step through them one at a time.

MVC, where @CrossOrigin lives, never sees the request.

Notice that the error the browser shows is about CORS, but the server's real answer is 401. Looking at the network tab, or the server log, shows that the request was rejected by security, not by CORS configuration.

3The Fix: Let Spring Security Handle CORS

Spring Security has its own CORS support. With http.cors(...) enabled, it adds a CorsFilter early in its chain, before authentication and authorization. That filter recognises a preflight, answers it with the right headers from a CorsConfigurationSource, and ends it there; for real requests it adds the CORS headers and lets them continue to authentication as usual.

Java
@Bean
SecurityFilterChain api(HttpSecurity http) throws Exception {
http.cors(Customizer.withDefaults()) // uses the CorsConfigurationSource bean
.authorizeHttpRequests(a -> a.anyRequest().authenticated())
.oauth2ResourceServer(o -> o.jwt(Customizer.withDefaults()));
return http.build();
}
@Bean
CorsConfigurationSource corsConfigurationSource() {
CorsConfiguration c = new CorsConfiguration();
c.setAllowedOrigins(List.of("https://app.example.com"));
c.setAllowedMethods(List.of("GET", "POST", "PUT", "DELETE"));
c.setAllowedHeaders(List.of("Authorization", "Content-Type"));
c.setMaxAge(3600L); // browser may cache the preflight for an hour
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", c);
return source;
}
// OPTIONS /orders from app.example.com -> 200, Access-Control-Allow-Origin: https://app.example.com
// POST /orders with Bearer token -> 201, with CORS headers
// OPTIONS /orders from evil.example -> 403

What changed is the order. CORS is now decided before security asks for credentials, so the preflight gets an answer, and the real request then goes through normal authentication. One central CorsConfigurationSource also replaces scattered @CrossOrigin annotations. If the application relies on MVC CORS configuration and has no CorsConfigurationSource bean, Spring Security can fall back to the MVC configuration through the handler mapping, but a single explicit bean is easier to reason about.

4Getting the Details Right

The allowed methods and headers must cover what the client actually sends. A React app sending a bearer token and JSON needs Authorization and Content-Type in the allowed headers; if a custom header such as X-Request-Id is added later, the preflight will fail again until it is listed. If the client must read a response header, such as Location, it goes into exposed headers.

Credentials need special care. If the browser app sends cookies, it uses credentials: "include", and the server must set allowCredentials(true). In that case the server cannot answer with the wildcard origin, and Spring refuses the combination of allowedOrigins("*") with allowCredentials(true). The fix is to list the exact origins, or use allowedOriginPatterns with specific patterns such as https://*.example.com. A bearer-token API usually does not need credentials at all, because the token travels in a header the app sets itself.

5What CORS Is Not

CORS is a browser rule. It decides whether JavaScript in a page from another origin may read our responses, and the browser enforces it. curl, Postman, a mobile app or an attacker's script on a server ignore it completely and can call the API directly. So CORS is not access control: the API must still authenticate and authorise every request on its own. Restricting origins protects users' browsers from other sites misusing their credentials, which matters most when cookies are involved.

6Follow-up questions

Why did it work before Spring Security was added?

Without the security filters, the preflight went straight to the DispatcherServlet, where @CrossOrigin answered it. Adding security put a filter in front that rejected unauthenticated requests, including the preflight.

Could I just permit all OPTIONS requests instead?

It makes the error go away, but the preflight then reaches MVC and depends on @CrossOrigin being present on each controller. Letting Spring Security's CorsFilter handle CORS is cleaner, keeps one configuration, and rejects disallowed origins before anything else runs.

How do I see the real cause in the browser?

Open the network tab and look at the OPTIONS request itself. Its status code, 401 or 403, and the missing Access-Control-Allow-Origin header show that the server rejected it before CORS headers were added.

7Conclusion

In this answer, we saw that the preflight OPTIONS request carries no credentials and meets Spring Security's filters before Spring MVC, so it is rejected before @CrossOrigin can apply. Enabling http.cors with a CorsConfigurationSource puts Spring Security's CorsFilter early in the chain, where it answers preflights and decorates real requests. The configuration must list the client's methods and headers, never mix a wildcard origin with credentials, and CORS remains a browser protection rather than access control.

What to say in the interviewAbout 90 seconds aloud

Open with

The reason is the order of processing. For a request with an Authorization header or JSON body, the browser first sends a preflight OPTIONS request, and that preflight carries no credentials. @CrossOrigin is handled by Spring MVC inside the DispatcherServlet, but Spring Security is a filter chain that runs before it.

Then, if they want more

So the security filters see an anonymous request to a protected URL and return 401 or 403, without any CORS headers, and the browser reports it as a CORS error. The request never reaches the annotation. The fix is to enable CORS in Spring Security itself with http.cors and Customizer.withDefaults, and define a CorsConfigurationSource bean.

Then Spring Security puts its CorsFilter early in the chain, before authentication, and it answers the preflight directly and adds the headers to real requests.

In that configuration I would list the exact origin, https://app.example.com, allow the methods the app uses and the headers it sends, at least Authorization and Content-Type, and set a max age so the browser caches the preflight.

If cookies are used, I need allowCredentials true, and then I cannot use the wildcard origin; I list origins or use specific origin patterns. Finally, I would point out that CORS is only enforced by browsers, so it is not access control: curl or a server can call the API anyway, and authentication still has to protect it.

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
OWASP Top Ten
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 React app on https://app.example.com calls a Spring Boot API on...": https://scrivio-interview-library.netlify.app/spring-boot/web-security-threats/5e370d4a27/