Improve ambiguous preflight checks in AbstractHandlerMethodMapping

Closes gh-36903
This commit is contained in:
rstoyanchev
2026-06-22 10:42:51 +01:00
parent 0ee636af7f
commit ad5bd67b31
4 changed files with 23 additions and 16 deletions
@@ -334,10 +334,8 @@ public abstract class AbstractHandlerMethodMapping<T> extends AbstractHandlerMap
logger.trace(exchange.getLogPrefix() + matches.size() + " matching mappings: " + matches);
}
if (CorsUtils.isPreFlightRequest(exchange.getRequest())) {
for (Match match : matches) {
if (match.hasCorsConfig()) {
return PREFLIGHT_AMBIGUOUS_MATCH;
}
if (matches.stream().allMatch(Match::hasCorsConfig)) {
return PREFLIGHT_AMBIGUOUS_MATCH;
}
}
else {
@@ -131,8 +131,8 @@ class HandlerMethodMappingTests {
@Test // gh-26490
void ambiguousMatchOnPreFlightRequestWithCorsConfig() throws Exception {
this.mapping.registerMapping("/f?o", this.handler, this.method1);
this.mapping.registerMapping("/fo?", this.handler, this.handler.getClass().getMethod("corsHandlerMethod"));
this.mapping.registerMapping("/f?o", this.handler, this.handler.getClass().getMethod("corsHandlerMethod1"));
this.mapping.registerMapping("/fo?", this.handler, this.handler.getClass().getMethod("corsHandlerMethod2"));
MockServerWebExchange exchange = MockServerWebExchange.from(
MockServerHttpRequest.options("https://example.org/foo")
@@ -256,7 +256,12 @@ class HandlerMethodMappingTests {
@RequestMapping
@CrossOrigin(originPatterns = "*")
public void corsHandlerMethod() {
public void corsHandlerMethod1() {
}
@RequestMapping
@CrossOrigin(originPatterns = "*")
public void corsHandlerMethod2() {
}
}
@@ -409,10 +409,8 @@ public abstract class AbstractHandlerMethodMapping<T> extends AbstractHandlerMap
logger.trace(matches.size() + " matching mappings: " + matches);
}
if (CorsUtils.isPreFlightRequest(request)) {
for (Match match : matches) {
if (match.hasCorsConfig()) {
return PREFLIGHT_AMBIGUOUS_MATCH;
}
if (matches.stream().allMatch(Match::hasCorsConfig)) {
return PREFLIGHT_AMBIGUOUS_MATCH;
}
}
else {
@@ -142,8 +142,8 @@ class HandlerMethodMappingTests {
@Test // gh-26490
void ambiguousMatchOnPreFlightRequestWithCorsConfig() throws Exception {
this.mapping.registerMapping("/f?o", this.handler, this.method1);
this.mapping.registerMapping("/fo?", this.handler, this.handler.getClass().getMethod("corsHandlerMethod"));
this.mapping.registerMapping("/f?o", this.handler, this.handler.getClass().getMethod("corsHandlerMethod1"));
this.mapping.registerMapping("/fo?", this.handler, this.handler.getClass().getMethod("corsHandlerMethod2"));
MockHttpServletRequest request = new MockHttpServletRequest("OPTIONS", "/foo");
request.addHeader(HttpHeaders.ORIGIN, "https://domain.com");
@@ -166,7 +166,7 @@ class HandlerMethodMappingTests {
@Test
void abortInterceptorInPreFlightRequestWithCorsConfig() throws Exception {
this.mapping.registerMapping("/foo", this.handler, this.handler.getClass().getMethod("corsHandlerMethod"));
this.mapping.registerMapping("/foo", this.handler, this.handler.getClass().getMethod("corsHandlerMethod1"));
MockHttpServletRequest request = new MockHttpServletRequest("OPTIONS", "/foo");
request.addParameter("abort", "true");
@@ -297,7 +297,7 @@ class HandlerMethodMappingTests {
@Test
void registerCustomHandlerMethod() throws Exception {
this.mapping.setCustomerHandlerMethod(true);
this.mapping.registerMapping("/foo", this.handler, this.handler.getClass().getMethod("corsHandlerMethod"));
this.mapping.registerMapping("/foo", this.handler, this.handler.getClass().getMethod("corsHandlerMethod1"));
MockHttpServletRequest request = new MockHttpServletRequest("OPTIONS", "/foo");
request.addParameter("abort", "true");
@@ -418,7 +418,13 @@ class HandlerMethodMappingTests {
@RequestMapping
@CrossOrigin(originPatterns = "*")
public void corsHandlerMethod() {
public void corsHandlerMethod1() {
}
@RequestMapping
@CrossOrigin(originPatterns = "*")
public void corsHandlerMethod2() {
}
}
}