Skip to content

Commit 4b9f173

Browse files
committed
Remove remaining Spring MVC trailing slash matching
Closes gh-34036
1 parent 2e6046a commit 4b9f173

File tree

3 files changed

+5
-62
lines changed

3 files changed

+5
-62
lines changed

framework-docs/modules/ROOT/pages/web/webmvc/filters.adoc

+5-5
Original file line numberDiff line numberDiff line change
@@ -118,12 +118,12 @@ See the sections on xref:web/webmvc-cors.adoc[CORS] and the xref:web/webmvc-cors
118118
[.small]#xref:web/webflux/reactive-spring.adoc#filters.url-handler[See equivalent in the Reactive stack]#
119119

120120
In previous Spring Framework versions, Spring MVC could be configured to ignore trailing slashes in URL paths
121-
when mapping incoming requests on controller methods. This could be done by enabling the `setUseTrailingSlashMatch`
122-
option on the `PathMatchConfigurer`. This means that sending a "GET /home/" request would be handled by a controller
123-
method annotated with `@GetMapping("/home")`.
121+
when mapping incoming requests on controller methods. This means that sending a "GET /home/" request would be
122+
handled by a controller method annotated with `@GetMapping("/home")`.
124123

125-
This option has been retired, but applications are still expected to handle such requests in a safe way.
126-
The `UrlHandlerFilter` Servlet filter has been designed for this purpose. It can be configured to:
124+
This option was deprecated in 6.0 and removed in 7.0, but applications are still expected to handle such
125+
requests in a safe way. The `UrlHandlerFilter` Servlet filter has been designed for this purpose.
126+
It can be configured to:
127127

128128
* respond with an HTTP redirect status when receiving URLs with trailing slashes, sending browsers to the non-trailing slash URL variant.
129129
* wrap the request to act as if the request was sent without a trailing slash and continue the processing of the request.

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

-34
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,6 @@ public abstract class AbstractUrlHandlerMapping extends AbstractHandlerMapping i
6666
@Nullable
6767
private Object rootHandler;
6868

69-
private boolean useTrailingSlashMatch = false;
70-
7169
private boolean lazyInitHandlers = false;
7270

7371
private final Map<String, Object> handlerMap = new LinkedHashMap<>();
@@ -101,28 +99,6 @@ public Object getRootHandler() {
10199
return this.rootHandler;
102100
}
103101

104-
/**
105-
* Whether to match to URLs irrespective of the presence of a trailing slash.
106-
* If enabled a URL pattern such as "/users" also matches to "/users/".
107-
* <p>The default value is {@code false}.
108-
* @deprecated as of 6.0, see
109-
* {@link PathPatternParser#setMatchOptionalTrailingSeparator(boolean)}
110-
*/
111-
@Deprecated(since = "6.0")
112-
public void setUseTrailingSlashMatch(boolean useTrailingSlashMatch) {
113-
this.useTrailingSlashMatch = useTrailingSlashMatch;
114-
if (getPatternParser() != null) {
115-
getPatternParser().setMatchOptionalTrailingSeparator(useTrailingSlashMatch);
116-
}
117-
}
118-
119-
/**
120-
* Whether to match to URLs irrespective of the presence of a trailing slash.
121-
*/
122-
public boolean useTrailingSlashMatch() {
123-
return this.useTrailingSlashMatch;
124-
}
125-
126102
/**
127103
* Set whether to lazily initialize handlers. Only applicable to
128104
* singleton handlers, as prototypes are always lazily initialized.
@@ -360,11 +336,6 @@ protected Object lookupHandler(String lookupPath, HttpServletRequest request) th
360336
if (getPathMatcher().match(registeredPattern, lookupPath)) {
361337
matchingPatterns.add(registeredPattern);
362338
}
363-
else if (useTrailingSlashMatch()) {
364-
if (!registeredPattern.endsWith("/") && getPathMatcher().match(registeredPattern + "/", lookupPath)) {
365-
matchingPatterns.add(registeredPattern + "/");
366-
}
367-
}
368339
}
369340

370341
String bestMatch = null;
@@ -495,11 +466,6 @@ public RequestMatchResult match(HttpServletRequest request, String pattern) {
495466
if (getPathMatcher().match(pattern, lookupPath)) {
496467
return new RequestMatchResult(pattern, lookupPath, getPathMatcher());
497468
}
498-
else if (useTrailingSlashMatch()) {
499-
if (!pattern.endsWith("/") && getPathMatcher().match(pattern + "/", lookupPath)) {
500-
return new RequestMatchResult(pattern + "/", lookupPath, getPathMatcher());
501-
}
502-
}
503469
return null;
504470
}
505471

spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/condition/PathPatternsRequestConditionTests.java

-23
Original file line numberDiff line numberDiff line change
@@ -126,29 +126,6 @@ void matchSortPatterns() {
126126
assertThat(match).isEqualTo(expected);
127127
}
128128

129-
@Test
130-
@SuppressWarnings("deprecation")
131-
void matchTrailingSlash() {
132-
MockHttpServletRequest request = createRequest("/foo/");
133-
134-
PathPatternParser patternParser = new PathPatternParser();
135-
patternParser.setMatchOptionalTrailingSeparator(true);
136-
137-
PathPatternsRequestCondition condition = new PathPatternsRequestCondition(patternParser, "/foo");
138-
PathPatternsRequestCondition match = condition.getMatchingCondition(request);
139-
140-
assertThat(match).isNotNull();
141-
assertThat(match.getPatternValues()).containsExactly("/foo");
142-
143-
PathPatternParser strictParser = new PathPatternParser();
144-
strictParser.setMatchOptionalTrailingSeparator(false);
145-
146-
condition = new PathPatternsRequestCondition(strictParser, "/foo");
147-
match = condition.getMatchingCondition(request);
148-
149-
assertThat(match).isNull();
150-
}
151-
152129
@Test
153130
void matchPatternContainsExtension() {
154131
MockHttpServletRequest request = createRequest("/foo.html");

0 commit comments

Comments
 (0)