Skip to content

Commit 5da2121

Browse files
committed
Merge remote-tracking branch 'origin/6.5.x'
2 parents 2b87e3c + eca8214 commit 5da2121

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

web/src/main/java/org/springframework/security/web/servlet/util/matcher/PathPatternRequestMatcher.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
* </p>
5050
*
5151
* @author Josh Cummings
52+
* @author Andrey Litvitski
5253
* @since 6.5
5354
*/
5455
public final class PathPatternRequestMatcher implements RequestMatcher {
@@ -234,14 +235,15 @@ public static final class Builder {
234235
*
235236
* <p>
236237
* Prefixes should be of the form {@code /my/prefix}, starting with a slash, not
237-
* ending in a slash, and not containing and wildcards
238+
* ending in a slash, and not containing and wildcards The special value
239+
* {@code "/"} may be used to indicate the root context.
238240
* @param basePath the path prefix
239241
* @return the {@link Builder} for more configuration
240242
*/
241243
public Builder basePath(String basePath) {
242244
Assert.notNull(basePath, "basePath cannot be null");
243245
Assert.isTrue(basePath.startsWith("/"), "basePath must start with '/'");
244-
Assert.isTrue(!basePath.endsWith("/"), "basePath must not end with a slash");
246+
Assert.isTrue("/".equals(basePath) || !basePath.endsWith("/"), "basePath must not end with a slash");
245247
Assert.isTrue(!basePath.contains("*"), "basePath must not contain a star");
246248
return new Builder(this.parser, basePath);
247249
}
@@ -316,7 +318,8 @@ public PathPatternRequestMatcher matcher(String path) {
316318
public PathPatternRequestMatcher matcher(@Nullable HttpMethod method, String path) {
317319
Assert.notNull(path, "pattern cannot be null");
318320
Assert.isTrue(path.startsWith("/"), "pattern must start with a /");
319-
PathPattern pathPattern = this.parser.parse(this.basePath + path);
321+
String prefix = ("/".equals(this.basePath)) ? "" : this.basePath;
322+
PathPattern pathPattern = this.parser.parse(prefix + path);
320323
return new PathPatternRequestMatcher(pathPattern,
321324
(method != null) ? new HttpMethodRequestMatcher(method) : AnyRequestMatcher.INSTANCE);
322325
}

web/src/test/java/org/springframework/security/web/servlet/util/matcher/PathPatternRequestMatcherTests.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,14 @@ void servletPathWhenEndsWithSlashOrStarThenIllegalArgument() {
138138
.isThrownBy(() -> PathPatternRequestMatcher.withDefaults().basePath("/path/"));
139139
}
140140

141+
@Test
142+
void matcherWhenBasePathIsRootThenNoDoubleSlash() {
143+
PathPatternRequestMatcher.Builder builder = PathPatternRequestMatcher.withDefaults().basePath("/");
144+
RequestMatcher matcher = builder.matcher(HttpMethod.GET, "/path");
145+
MockHttpServletRequest mock = get("/path").servletPath("/path").buildRequest(null);
146+
assertThat(matcher.matches(mock)).isTrue();
147+
}
148+
141149
MockHttpServletRequest request(String uri) {
142150
MockHttpServletRequest request = new MockHttpServletRequest("GET", uri);
143151
ServletRequestPathUtils.parseAndCache(request);

0 commit comments

Comments
 (0)