Skip to content

Commit 5cb6ef4

Browse files
committed
Update HandlerMappingIntrospector Usage in CORS support
Closes spring-projectsgh-16501 Signed-off-by: Evgeniy Cheban <[email protected]>
1 parent 5bb5d0f commit 5cb6ef4

File tree

3 files changed

+11
-10
lines changed

3 files changed

+11
-10
lines changed

config/src/main/java/org/springframework/security/config/annotation/web/configurers/CorsConfigurer.java

+7-8
Original file line numberDiff line numberDiff line change
@@ -107,15 +107,14 @@ static class MvcCorsFilter {
107107
* @return
108108
*/
109109
private static CorsFilter getMvcCorsFilter(ApplicationContext context) {
110-
if (!context.containsBean(HANDLER_MAPPING_INTROSPECTOR_BEAN_NAME)) {
111-
throw new NoSuchBeanDefinitionException(HANDLER_MAPPING_INTROSPECTOR_BEAN_NAME, "A Bean named "
112-
+ HANDLER_MAPPING_INTROSPECTOR_BEAN_NAME + " of type "
113-
+ HandlerMappingIntrospector.class.getName()
114-
+ " is required to use MvcRequestMatcher. Please ensure Spring Security & Spring MVC are configured in a shared ApplicationContext.");
110+
if (context.getBeanNamesForType(CorsConfigurationSource.class).length > 0) {
111+
CorsConfigurationSource corsConfigurationSource = context
112+
.getBean(HANDLER_MAPPING_INTROSPECTOR_BEAN_NAME, CorsConfigurationSource.class);
113+
return new CorsFilter(corsConfigurationSource);
115114
}
116-
HandlerMappingIntrospector mappingIntrospector = context.getBean(HANDLER_MAPPING_INTROSPECTOR_BEAN_NAME,
117-
HandlerMappingIntrospector.class);
118-
return new CorsFilter(mappingIntrospector);
115+
throw new NoSuchBeanDefinitionException(CorsConfigurationSource.class,
116+
"Failed to find a bean that implements `CorsConfigurationSource`. Please ensure that you are using "
117+
+ "`@EnableWebMvc`, are publishing a `WebMvcConfigurer`, or are publishing a `CorsConfigurationSource` bean.");
119118
}
120119

121120
}

config/src/test/java/org/springframework/security/config/annotation/web/configurers/CorsConfigurerTests.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@ public void configureWhenNoMvcThenException() {
7070
assertThatExceptionOfType(BeanCreationException.class)
7171
.isThrownBy(() -> this.spring.register(DefaultCorsConfig.class).autowire())
7272
.withMessageContaining(
73-
"Please ensure Spring Security & Spring MVC are configured in a shared ApplicationContext");
73+
"Please ensure that you are using `@EnableWebMvc`, are publishing a `WebMvcConfigurer`, "
74+
+ "or are publishing a `CorsConfigurationSource` bean.");
7475
}
7576

7677
@Test

config/src/test/kotlin/org/springframework/security/config/annotation/web/CorsDslTests.kt

+2-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ class CorsDslTests {
5656
fun `CORS when no MVC then exception`() {
5757
assertThatThrownBy { this.spring.register(DefaultCorsConfig::class.java).autowire() }
5858
.isInstanceOf(BeanCreationException::class.java)
59-
.hasMessageContaining("Please ensure Spring Security & Spring MVC are configured in a shared ApplicationContext")
59+
.hasMessageContaining("Please ensure that you are using `@EnableWebMvc`, are publishing a `WebMvcConfigurer`, " +
60+
"or are publishing a `CorsConfigurationSource` bean.")
6061

6162
}
6263

0 commit comments

Comments
 (0)