Spring Boot CORS with Spring Security: Preflight Before Authentication
By Ankit Jain · Updated
Configure a CorsConfigurationSource and SecurityFilterChain so OPTIONS is processed before authentication and actual API routes remain protected.
Spring Boot is a Java application framework. For servlet applications using Spring Security, CORS must be integrated with the security filter chain. A controller annotation alone may not fix OPTIONS requests rejected before they reach the controller.
The example uses the component-based SecurityFilterChain style in Spring Security 6/7. Match dependencies to your Spring Boot release. It is a CORS integration example, not a complete authentication or CSRF-token delivery flow.
Configure the security chain and CORS source
import java.util.List;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.Customizer;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.CorsConfigurationSource;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
@Configuration
public class SecurityConfig {
@Bean
SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
http
.cors(Customizer.withDefaults())
.authorizeHttpRequests(auth -> auth.anyRequest().authenticated())
.httpBasic(Customizer.withDefaults());
// CSRF protection stays enabled. Supply a valid token for protected writes.
return http.build();
}
@Bean
CorsConfigurationSource corsConfigurationSource() {
CorsConfiguration config = new CorsConfiguration();
config.setAllowedOrigins(List.of("https://app.example.com"));
config.setAllowedMethods(List.of("GET", "POST", "PUT"));
config.setAllowedHeaders(List.of("Content-Type", "Authorization", "X-CSRF-TOKEN"));
config.setExposedHeaders(List.of("X-Request-ID"));
config.setAllowCredentials(true);
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/api/**", config);
return source;
}
}
Replace the origin and route pattern with your managed frontend and API paths. Credentialed permission needs explicit approved origins. Set allowCredentials false if the endpoint’s integration does not need include-mode access.
Why OPTIONS differs from the actual request
A normal CORS preflight has no session cookie. CORS processing must run before authentication that would otherwise reject it. Spring Security can use the CorsConfigurationSource to handle that permission check. The actual request remains subject to authentication, authorization, and applicable CSRF protection.
If you provide several CorsConfigurationSource beans or several security chains, explicitly wire the source into the chain that handles the API rather than relying on ambiguous automatic selection.
Do not solve a permission failure by removing security
Do not disable CSRF globally just because a frontend is cross-origin. Choose CSRF behavior based on the real authentication design. The sample keeps the default protection and requires your application to provide the corresponding token flow for writes.
A 401 or 403 can be an expected application/security response. Inspect whether it has readable CORS permission. A preflight 403 with an Invalid CORS request message can instead indicate a rejected origin, method, or header.
Verify the chain actually used
Inspect OPTIONS for /api/ routes, then an authenticated request and an unauthorized request. Verify the exact origin, method, requested headers, and actual response permissions. Check custom filters and proxy responses separately.
Continue with preflight, credentials, and readable errors.
Use the DevTools debugging workflow to inspect the frontend request, then analyze captured headers in the CORS Tester.