Complete the code to enable CORS in a Spring Security configuration.
http.cors([1]());The cors(withDefaults()) method enables CORS support in Spring Security. Calling withDefaults() activates it with default settings.
Complete the code to define a CORS configuration source bean.
@Bean public CorsConfigurationSource [1]() { CorsConfiguration configuration = new CorsConfiguration(); configuration.addAllowedOrigin("*"); UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); source.registerCorsConfiguration("/**", configuration); return source; }
The method name corsConfigurationSource is the standard name Spring Security looks for to get CORS settings.
Fix the error in the CORS configuration to allow credentials.
CorsConfiguration configuration = new CorsConfiguration();
configuration.setAllowCredentials([1]);The setAllowCredentials method expects a Boolean true or false, not a string or null.
Fill both blanks to restrict allowed HTTP methods to GET and POST in CORS configuration.
CorsConfiguration configuration = new CorsConfiguration(); configuration.setAllowedMethods(List.of([1], [2]));
To allow only GET and POST methods, pass their names as strings in the allowed methods list.
Fill all three blanks to create a complete Spring Security CORS configuration bean with allowed origins, methods, and credentials.
CorsConfiguration configuration = new CorsConfiguration(); configuration.setAllowedOrigins(List.of([1])); configuration.setAllowedMethods(List.of([2])); configuration.setAllowCredentials([3]);
This sets allowed origins to a specific URL, allowed methods to GET, and enables credentials support.