Skip to content

Commit 0f3c2c2

Browse files
committed
fix: Restrict automatic CORS configuration to UrlBasedCorsConfigurationSource
- Update CORS configuration logic to automatically enable .cors() only if a UrlBasedCorsConfigurationSource bean is present. - Modify applyCorsIfAvailable method to check for UrlBasedCorsConfigurationSource instances.
1 parent 9d8888c commit 0f3c2c2

File tree

3 files changed

+48
-5
lines changed

3 files changed

+48
-5
lines changed

config/src/main/java/org/springframework/security/config/annotation/web/configuration/HttpSecurityConfiguration.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -48,13 +48,15 @@
4848
import org.springframework.web.accept.ContentNegotiationStrategy;
4949
import org.springframework.web.accept.HeaderContentNegotiationStrategy;
5050
import org.springframework.web.cors.CorsConfigurationSource;
51+
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
5152

5253
import static org.springframework.security.config.Customizer.withDefaults;
5354

5455
/**
5556
* {@link Configuration} that exposes the {@link HttpSecurity} bean.
5657
*
5758
* @author Eleftheria Stein
59+
* @author Jinwoo Bae
5860
* @since 5.4
5961
*/
6062
@Configuration(proxyBeanMethods = false)
@@ -131,8 +133,13 @@ HttpSecurity httpSecurity() throws Exception {
131133
}
132134

133135
private void applyCorsIfAvailable(HttpSecurity http) throws Exception {
134-
String[] beanNames = this.context.getBeanNamesForType(CorsConfigurationSource.class);
135-
if (beanNames.length == 1) {
136+
Map<String, CorsConfigurationSource> corsConfigurationSources = this.context
137+
.getBeansOfType(CorsConfigurationSource.class);
138+
139+
boolean hasUrlBasedCorsConfigurationSource = corsConfigurationSources.values()
140+
.stream()
141+
.anyMatch(UrlBasedCorsConfigurationSource.class::isInstance);
142+
if (hasUrlBasedCorsConfigurationSource) {
136143
http.cors(withDefaults());
137144
}
138145
}

config/src/test/java/org/springframework/security/config/annotation/web/configuration/HttpSecurityConfigurationTests.java

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -377,6 +377,15 @@ public void configureWhenCorsConfigurationSourceThenApplyCors() {
377377
assertThat(configSource).isInstanceOf(UrlBasedCorsConfigurationSource.class);
378378
}
379379

380+
@Test
381+
public void configureWhenNoCorsConfigurationSourceThenNoCorsApplied() throws Exception {
382+
this.spring.register(NonUrlBasedCorsConfig.class, DefaultWithFilterChainConfig.class).autowire();
383+
SecurityFilterChain filterChain = this.spring.getContext().getBean(SecurityFilterChain.class);
384+
assertThat(filterChain.getFilters()).noneMatch((filter) -> filter instanceof CorsFilter);
385+
386+
this.mockMvc.perform(formLogin()).andExpect(header().doesNotExist("Access-Control-Allow-Origin"));
387+
}
388+
380389
@Test
381390
public void configureWhenAddingCustomDslUsingWithThenApplied() throws Exception {
382391
this.spring.register(WithCustomDslConfig.class, UserDetailsConfig.class).autowire();
@@ -711,6 +720,33 @@ CorsConfigurationSource corsConfigurationSource() {
711720

712721
}
713722

723+
@Configuration
724+
@EnableWebSecurity
725+
static class NonUrlBasedCorsConfig {
726+
727+
@Bean
728+
CorsConfigurationSource corsConfigurationSource() {
729+
return new CustomCorsConfigurationSource();
730+
}
731+
732+
@Bean
733+
SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
734+
return http.build();
735+
}
736+
737+
}
738+
739+
static class CustomCorsConfigurationSource implements CorsConfigurationSource {
740+
741+
@Override
742+
public CorsConfiguration getCorsConfiguration(HttpServletRequest request) {
743+
CorsConfiguration configuration = new CorsConfiguration();
744+
configuration.setAllowedOrigins(List.of("http://localhost:8080"));
745+
return configuration;
746+
}
747+
748+
}
749+
714750
static class DefaultConfigurer extends AbstractHttpConfigurer<DefaultConfigurer, HttpSecurity> {
715751

716752
boolean init;

docs/modules/ROOT/pages/servlet/integrations/cors.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ CORS must be processed before Spring Security, because the pre-flight request do
66
If the request does not contain any cookies and Spring Security is first, the request determines that the user is not authenticated (since there are no cookies in the request) and rejects it.
77

88
The easiest way to ensure that CORS is handled first is to use the `CorsFilter`.
9-
Users can integrate the `CorsFilter` with Spring Security by providing a `CorsConfigurationSource`.
9+
Users can integrate the `CorsFilter` with Spring Security by providing a `CorsConfigurationSource`. Note that Spring Security will automatically configure CORS only if a `UrlBasedCorsConfigurationSource` instance is present.
1010
For example, the following will integrate CORS support within Spring Security:
1111

1212
[tabs]

0 commit comments

Comments
 (0)