diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/handler/AbstractHandlerMapping.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/handler/AbstractHandlerMapping.java index c463ee1fcb..367dcb765b 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/handler/AbstractHandlerMapping.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/handler/AbstractHandlerMapping.java @@ -390,10 +390,15 @@ public abstract class AbstractHandlerMapping extends WebApplicationObjectSupport /** * Retrieve the CORS configuration for the given handler. + * @param handler the handler to check (never {@code null}). + * @param request the current request. + * @return the CORS configuration for the handler or {@code null}. */ protected CorsConfiguration getCorsConfiguration(Object handler, HttpServletRequest request) { - handler = (handler instanceof HandlerExecutionChain) ? ((HandlerExecutionChain) handler).getHandler() : handler; - if (handler != null && handler instanceof CorsConfigurationSource) { + if (handler instanceof HandlerExecutionChain) { + handler = ((HandlerExecutionChain) handler).getHandler(); + } + if (handler instanceof CorsConfigurationSource) { return ((CorsConfigurationSource) handler).getCorsConfiguration(request); } return null; diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/handler/AbstractHandlerMethodMapping.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/handler/AbstractHandlerMethodMapping.java index 9a36308ce0..4fc653eb8b 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/handler/AbstractHandlerMethodMapping.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/handler/AbstractHandlerMethodMapping.java @@ -83,7 +83,7 @@ public abstract class AbstractHandlerMethodMapping extends AbstractHandlerMap private final MultiValueMap nameMap = new LinkedMultiValueMap(); - private final Map corsConfigurations = new LinkedHashMap(); + private final Map corsMap = new LinkedHashMap(); /** @@ -113,20 +113,6 @@ public abstract class AbstractHandlerMethodMapping extends AbstractHandlerMap return Collections.unmodifiableMap(this.handlerMethods); } - protected Map getCorsConfigurations() { - return corsConfigurations; - } - - @Override - protected CorsConfiguration getCorsConfiguration(Object handler, HttpServletRequest request) { - CorsConfiguration config = super.getCorsConfiguration(handler, request); - if (config == null && handler instanceof HandlerMethod) { - HandlerMethod handlerMethod = (HandlerMethod)handler; - config = this.getCorsConfigurations().get(handlerMethod.getMethod()); - } - return config; - } - /** * Return the handler methods mapped to the mapping with the given name. * @param mappingName the mapping name @@ -159,10 +145,9 @@ public abstract class AbstractHandlerMethodMapping extends AbstractHandlerMap BeanFactoryUtils.beanNamesForTypeIncludingAncestors(getApplicationContext(), Object.class) : getApplicationContext().getBeanNamesForType(Object.class)); - for (String beanName : beanNames) { - if (!beanName.startsWith(SCOPED_TARGET_NAME_PREFIX) && - isHandler(getApplicationContext().getType(beanName))){ - detectHandlerMethods(beanName); + for (String name : beanNames) { + if (!name.startsWith(SCOPED_TARGET_NAME_PREFIX) && isHandler(getApplicationContext().getType(name))) { + detectHandlerMethods(name); } } registerMultiMatchCorsConfiguration(); @@ -175,7 +160,7 @@ public abstract class AbstractHandlerMethodMapping extends AbstractHandlerMap config.addAllowedMethod("*"); config.addAllowedHeader("*"); config.setAllowCredentials(true); - this.corsConfigurations.put(PREFLIGHT_MULTI_MATCH_HANDLER_METHOD.getMethod(), config); + this.corsMap.put(PREFLIGHT_MULTI_MATCH_HANDLER_METHOD.getMethod(), config); } /** @@ -262,7 +247,7 @@ public abstract class AbstractHandlerMethodMapping extends AbstractHandlerMap CorsConfiguration config = initCorsConfiguration(handler, method, mapping); if (config != null) { - this.corsConfigurations.put(method, config); + this.corsMap.put(method, config); } } @@ -442,6 +427,14 @@ public abstract class AbstractHandlerMethodMapping extends AbstractHandlerMap return null; } + @Override + protected CorsConfiguration getCorsConfiguration(Object handler, HttpServletRequest request) { + if (handler instanceof HandlerMethod) { + this.corsMap.get(((HandlerMethod) handler).getMethod()); + } + return null; + } + /** * A thin wrapper around a matched HandlerMethod and its mapping, for the purpose of