Skip to content

Commit 5a4a677

Browse files
committed
Fix contextPath request matching with PathPatterns
Prior to this commit, `SimpleUrlHandlerMapping` and other implementations of `AbstractUrlHandlerMapping` would not strip the Servlet context path from the request path before performing path matching with PathPattern instances. This resulted in requests not matching as expected if the application was configured with a Servlet context path. This commit ensures that the `RequestPath` "path whithin the application" is used for matching against path patterns. Fixes gh-26411
1 parent 584dabb commit 5a4a677

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

spring-webmvc/src/main/java/org/springframework/web/servlet/handler/AbstractUrlHandlerMapping.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -186,7 +186,7 @@ protected Object lookupHandler(
186186
// Pattern match?
187187
List<PathPattern> matches = null;
188188
for (PathPattern pattern : this.pathPatternHandlerMap.keySet()) {
189-
if (pattern.matches(path)) {
189+
if (pattern.matches(path.pathWithinApplication())) {
190190
matches = (matches != null ? matches : new ArrayList<>());
191191
matches.add(pattern);
192192
}
@@ -207,7 +207,7 @@ protected Object lookupHandler(
207207
handler = obtainApplicationContext().getBean(handlerName);
208208
}
209209
validateHandler(handler, request);
210-
PathContainer pathWithinMapping = pattern.extractPathWithinPattern(path);
210+
PathContainer pathWithinMapping = pattern.extractPathWithinPattern(path.pathWithinApplication());
211211
return buildPathExposingHandler(handler, pattern.getPatternString(), pathWithinMapping.value(), null);
212212
}
213213

spring-webmvc/src/test/java/org/springframework/web/servlet/handler/SimpleUrlHandlerMappingTests.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -108,6 +108,12 @@ void checkMappings(String beanName) throws Exception {
108108
assertThat(request.getAttribute(PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE)).isEqualTo("welcome.x");
109109
assertThat(request.getAttribute(BEST_MATCHING_HANDLER_ATTRIBUTE)).isEqualTo(otherBean);
110110

111+
request = PathPatternsTestUtils.initRequest("GET", "/app", "/welcome.x", usePathPatterns);
112+
chain = getHandler(hm, request);
113+
assertThat(chain.getHandler()).isSameAs(otherBean);
114+
assertThat(request.getAttribute(PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE)).isEqualTo("welcome.x");
115+
assertThat(request.getAttribute(BEST_MATCHING_HANDLER_ATTRIBUTE)).isEqualTo(otherBean);
116+
111117
request = PathPatternsTestUtils.initRequest("GET", "/welcome/", usePathPatterns);
112118
chain = getHandler(hm, request);
113119
assertThat(chain.getHandler()).isSameAs(otherBean);
@@ -122,6 +128,7 @@ void checkMappings(String beanName) throws Exception {
122128
chain = getHandler(hm, request);
123129
assertThat(chain.getHandler()).isSameAs(bean);
124130

131+
125132
request = PathPatternsTestUtils.initRequest("GET", "/show.html", usePathPatterns);
126133
chain = getHandler(hm, request);
127134
assertThat(chain.getHandler()).isSameAs(bean);

0 commit comments

Comments
 (0)