Skip to content

Commit 0c17fd1

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

File tree

2 files changed

+9
-11
lines changed

2 files changed

+9
-11
lines changed

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

+7-10
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.springframework.util.ClassUtils;
2525
import org.springframework.web.cors.CorsConfiguration;
2626
import org.springframework.web.cors.CorsConfigurationSource;
27+
import org.springframework.web.cors.PreFlightRequestHandler;
2728
import org.springframework.web.filter.CorsFilter;
2829
import org.springframework.web.servlet.handler.HandlerMappingIntrospector;
2930

@@ -98,24 +99,20 @@ private CorsFilter getCorsFilter(ApplicationContext context) {
9899

99100
static class MvcCorsFilter {
100101

101-
private static final String HANDLER_MAPPING_INTROSPECTOR_BEAN_NAME = "mvcHandlerMappingIntrospector";
102-
103102
/**
104103
* This needs to be isolated into a separate class as Spring MVC is an optional
105104
* dependency and will potentially cause ClassLoading issues
106105
* @param context
107106
* @return
108107
*/
109108
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.");
109+
if (context.getBeanNamesForType(CorsConfigurationSource.class).length > 0) {
110+
CorsConfigurationSource corsConfigurationSource = context.getBean(CorsConfigurationSource.class);
111+
return new CorsFilter(corsConfigurationSource);
115112
}
116-
HandlerMappingIntrospector mappingIntrospector = context.getBean(HANDLER_MAPPING_INTROSPECTOR_BEAN_NAME,
117-
HandlerMappingIntrospector.class);
118-
return new CorsFilter(mappingIntrospector);
113+
throw new NoSuchBeanDefinitionException(CorsConfigurationSource.class,
114+
"Failed to find a bean that implements `CorsConfigurationSource`. Please ensure that you are using "
115+
+ "`@EnableWebMvc`, are publishing a `WebMvcConfigurer`, or are publishing a `CorsConfigurationSource` bean.");
119116
}
120117

121118
}

Diff for: 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

0 commit comments

Comments
 (0)