0
0
Spring Bootframework~20 mins

CORS configuration in Security in Spring Boot - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
CORS Security Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
1:30remaining
What is the effect of this Spring Security CORS configuration?
Consider this Spring Security configuration snippet for CORS:
http.cors().and().csrf().disable();

What does this configuration do regarding CORS requests?
Spring Boot
http.cors().and().csrf().disable();
AEnables CORS with custom settings but keeps CSRF enabled.
BDisables CORS support and enables CSRF protection.
CDisables both CORS and CSRF protections.
DEnables CORS support using default settings and disables CSRF protection.
Attempts:
2 left
💡 Hint
Think about what calling cors() and csrf().disable() does in Spring Security.
📝 Syntax
intermediate
2:00remaining
Which option correctly defines a CORS configuration source bean in Spring Boot?
You want to define a bean that customizes CORS mappings in Spring Boot Security. Which code snippet is syntactically correct?
Apublic CorsConfigurationSource corsConfigurationSource() { CorsConfiguration config = new CorsConfiguration(); config.addAllowedOrigin("*"); UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); source.registerCorsConfiguration("/**", config); return source; }
B@Bean public void corsConfigurationSource() { CorsConfiguration config = new CorsConfiguration(); config.addAllowedOrigin("*"); UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); source.registerCorsConfiguration("/**", config); }
C@Bean public CorsConfigurationSource corsConfigurationSource() { CorsConfiguration config = new CorsConfiguration(); config.addAllowedOrigin("*"); UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); source.registerCorsConfiguration("/**", config); return source; }
D@Bean CorsConfigurationSource corsConfigurationSource() { CorsConfiguration config = new CorsConfiguration(); config.addAllowedOrigin("*"); UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); source.registerCorsConfiguration("/**", config); return source; }
Attempts:
2 left
💡 Hint
Remember the @Bean annotation and method return type are required for Spring to recognize the bean.
🔧 Debug
advanced
2:30remaining
Why does this CORS configuration not allow requests from 'http://example.com'?
Given this CORS configuration bean:
@Bean
public CorsConfigurationSource corsConfigurationSource() {
  CorsConfiguration config = new CorsConfiguration();
  config.addAllowedOrigin("http://example.com");
  config.addAllowedMethod("GET");
  UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
  source.registerCorsConfiguration("/**", config);
  return source;
}

And the security config:
http.cors().and().csrf().disable();

Why might requests from 'http://example.com' still be blocked by the browser?
AThe allowed origin must be set with a trailing slash like 'http://example.com/'.
BThe allowed origin must be set using 'setAllowedOriginPatterns' instead of 'addAllowedOrigin' to support subdomains or patterns.
CThe CORS configuration bean is not registered because the method lacks the @Configuration annotation.
DCSRF is disabled, so CORS requests are blocked by default.
Attempts:
2 left
💡 Hint
Think about how Spring Security matches origins with patterns.
state_output
advanced
1:30remaining
What is the value of 'allowedMethods' after this CORS config code runs?
Given this code snippet:
CorsConfiguration config = new CorsConfiguration();
config.addAllowedMethod("GET");
config.addAllowedMethod("POST");
config.setAllowedMethods(List.of("PUT", "DELETE"));
List allowedMethods = config.getAllowedMethods();

What is the content of 'allowedMethods'?
A["PUT", "DELETE"]
B["GET", "POST", "PUT", "DELETE"]
C["GET", "POST"]
Dnull
Attempts:
2 left
💡 Hint
Consider what setAllowedMethods does compared to addAllowedMethod.
🧠 Conceptual
expert
2:00remaining
Which statement best describes the role of CORS configuration in Spring Security?
Select the most accurate description of how CORS configuration interacts with Spring Security and browser behavior.
ACORS configuration in Spring Security controls which cross-origin requests the server accepts, but browsers enforce CORS policies based on server response headers.
BCORS configuration in Spring Security automatically adds Access-Control-Allow-Origin: * header to all responses.
CCORS configuration in Spring Security only affects CSRF tokens and does not influence cross-origin requests.
DCORS configuration in Spring Security disables browser CORS checks entirely, allowing all cross-origin requests.
Attempts:
2 left
💡 Hint
Think about the difference between server-side configuration and browser enforcement.