From 5fbdd6dcfe7504327fe805971760f1b229d0cc62 Mon Sep 17 00:00:00 2001 From: Arjen Poutsma Date: Wed, 17 Nov 2021 16:52:17 +0100 Subject: [PATCH] Throw exception using capturing patterns in AntPathMatcher Closes gh-27688 --- .../org/springframework/util/AntPathMatcher.java | 4 ++++ .../PathMatchingUrlHandlerMappingTests.java | 16 +++++++++------- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/spring-core/src/main/java/org/springframework/util/AntPathMatcher.java b/spring-core/src/main/java/org/springframework/util/AntPathMatcher.java index d304f08d2c..63eea1e9ea 100644 --- a/spring-core/src/main/java/org/springframework/util/AntPathMatcher.java +++ b/spring-core/src/main/java/org/springframework/util/AntPathMatcher.java @@ -733,6 +733,10 @@ public class AntPathMatcher implements PathMatcher { } for (int i = 1; i <= matcher.groupCount(); i++) { String name = this.variableNames.get(i - 1); + if (name.startsWith("*")) { + throw new IllegalArgumentException("Capturing patterns (" + name + ") are not " + + "supported by the AntPathMatcher. Use the PathPatternParser instead."); + } String value = matcher.group(i); uriTemplateVariables.put(name, value); } diff --git a/spring-webmvc/src/test/java/org/springframework/web/servlet/handler/PathMatchingUrlHandlerMappingTests.java b/spring-webmvc/src/test/java/org/springframework/web/servlet/handler/PathMatchingUrlHandlerMappingTests.java index 5e4e148c85..4c988f1a7f 100644 --- a/spring-webmvc/src/test/java/org/springframework/web/servlet/handler/PathMatchingUrlHandlerMappingTests.java +++ b/spring-webmvc/src/test/java/org/springframework/web/servlet/handler/PathMatchingUrlHandlerMappingTests.java @@ -155,13 +155,15 @@ public class PathMatchingUrlHandlerMappingTests { chain = getHandler(mapping, wac, request); assertThat(chain.getHandler()).isSameAs(defaultBean); - request = new MockHttpServletRequest("GET", "/administrator/testing/longer/bla"); - chain = getHandler(mapping, wac, request); - assertThat(chain.getHandler()).isSameAs(bean); - - request = new MockHttpServletRequest("GET", "/administrator/testing/longer/test.jsp"); - chain = getHandler(mapping, wac, request); - assertThat(chain.getHandler()).isSameAs(bean); + if (mapping.getPatternParser() != null) { + request = new MockHttpServletRequest("GET", "/administrator/testing/longer/bla"); + chain = getHandler(mapping, wac, request); + assertThat(chain.getHandler()).isSameAs(bean); + + request = new MockHttpServletRequest("GET", "/administrator/testing/longer/test.jsp"); + chain = getHandler(mapping, wac, request); + assertThat(chain.getHandler()).isSameAs(bean); + } request = new MockHttpServletRequest("GET", "/administrator/testing/longer2/notmatching/notmatching"); chain = getHandler(mapping, wac, request);