Explain Cross-Origin Resource Sharing (CORS) and how you would configure it in a Spring Boot application.

CORS is a browser-enforced mechanism that lets a server explicitly declare which other origins are allowed to make requests to it, relaxing the default same-origin restriction in a controlled way.

Key Points

Example

A React app running on localhost:3000 calling a Spring Boot API on localhost:8080 needs the API to allow that origin explicitly, otherwise the browser blocks the response even though the server itself would have processed it fine.

@Bean
public CorsConfigurationSource corsConfigurationSource() {
    CorsConfiguration config = new CorsConfiguration();
    config.setAllowedOrigins(List.of("https://app.example.com"));
    config.setAllowedMethods(List.of("GET", "POST", "PUT", "DELETE"));
    config.setAllowedHeaders(List.of("Authorization", "Content-Type"));

    UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
    source.registerCorsConfiguration("/**", config);
    return source;
}

Interview Tip

A concise interview answer is

CORS lets a server tell browsers which other origins are allowed to call it, since browsers block cross-origin requests by default. In Spring Boot I'd define a global CorsConfigurationSource bean specifying allowed origins, methods, and headers rather than scattering @CrossOrigin annotations across controllers.

Last updated: 16 July 2026

Previous question: How does Spring Security integrate with OAuth2 for authorization?

Next question: What are SecurityContext and SecurityContextHolder in Spring Security?

More Spring Security - Level I interview questions

All Spring Security - Level I questions