Skip to content

Commit 2090ece

Browse files
committed
UrlHandlerFilter ignores trailing slash in contextPath
Closes gh-33565
1 parent e587753 commit 2090ece

File tree

4 files changed

+18
-12
lines changed

4 files changed

+18
-12
lines changed

spring-web/src/main/java/org/springframework/web/filter/UrlHandlerFilter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ protected AbstractTrailingSlashHandler(@Nullable Consumer<HttpServletRequest> in
278278

279279
@Override
280280
public boolean canHandle(HttpServletRequest request, RequestPath path) {
281-
List<PathContainer.Element> elements = path.elements();
281+
List<PathContainer.Element> elements = path.pathWithinApplication().elements();
282282
return (elements.size() > 1 && elements.get(elements.size() - 1).value().equals("/"));
283283
}
284284

spring-web/src/main/java/org/springframework/web/filter/reactive/UrlHandlerFilter.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,8 @@ protected AbstractTrailingSlashHandler(@Nullable List<Function<ServerHttpRequest
253253

254254
@Override
255255
public boolean canHandle(ServerWebExchange exchange) {
256-
List<PathContainer.Element> elements = exchange.getRequest().getPath().elements();
256+
ServerHttpRequest request = exchange.getRequest();
257+
List<PathContainer.Element> elements = request.getPath().pathWithinApplication().elements();
257258
return (elements.size() > 1 && elements.get(elements.size() - 1).value().equals("/"));
258259
}
259260

spring-web/src/test/java/org/springframework/web/filter/UrlHandlerFilterTests.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,17 +87,20 @@ void redirect() throws Exception {
8787

8888
@Test
8989
void noUrlHandling() throws Exception {
90-
testNoUrlHandling("/path/**", "/path/123");
91-
testNoUrlHandling("/path/*", "/path/123");
92-
testNoUrlHandling("/**", "/"); // gh-33444
90+
testNoUrlHandling("/path/**", "", "/path/123");
91+
testNoUrlHandling("/path/*", "", "/path/123");
92+
testNoUrlHandling("/**", "", "/"); // gh-33444
93+
testNoUrlHandling("/**", "/myApp", "/myApp/"); // gh-33565
9394
}
9495

95-
private static void testNoUrlHandling(String pattern, String requestURI) throws ServletException, IOException {
96+
private static void testNoUrlHandling(String pattern, String contextPath, String requestURI)
97+
throws ServletException, IOException {
9698

9799
// No request wrapping
98100
UrlHandlerFilter filter = UrlHandlerFilter.trailingSlashHandler(pattern).wrapRequest().build();
99101

100102
MockHttpServletRequest request = new MockHttpServletRequest("GET", requestURI);
103+
request.setContextPath(contextPath);
101104
MockFilterChain chain = new MockFilterChain();
102105
filter.doFilterInternal(request, new MockHttpServletResponse(), chain);
103106

@@ -109,6 +112,7 @@ private static void testNoUrlHandling(String pattern, String requestURI) throws
109112
filter = UrlHandlerFilter.trailingSlashHandler(pattern).redirect(status).build();
110113

111114
request = new MockHttpServletRequest("GET", requestURI);
115+
request.setContextPath(contextPath);
112116
MockHttpServletResponse response = new MockHttpServletResponse();
113117

114118
chain = new MockFilterChain();

spring-web/src/test/java/org/springframework/web/filter/reactive/UrlHandlerFilterTests.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,17 +76,18 @@ void redirect() {
7676

7777
@Test
7878
void noUrlHandling() {
79-
testNoUrlHandling("/path/**", "/path/123");
80-
testNoUrlHandling("/path/*", "/path/123");
81-
testNoUrlHandling("/**", "/"); // gh-33444
79+
testNoUrlHandling("/path/**", "", "/path/123");
80+
testNoUrlHandling("/path/*", "", "/path/123");
81+
testNoUrlHandling("/**", "", "/"); // gh-33444
82+
testNoUrlHandling("/**", "/myApp", "/myApp/"); // gh-33565
8283
}
8384

84-
private static void testNoUrlHandling(String pattern, String path) {
85+
private static void testNoUrlHandling(String pattern, String contextPath, String path) {
8586

8687
// No request mutation
8788
UrlHandlerFilter filter = UrlHandlerFilter.trailingSlashHandler(pattern).mutateRequest().build();
8889

89-
MockServerHttpRequest request = MockServerHttpRequest.get(path).build();
90+
MockServerHttpRequest request = MockServerHttpRequest.get(path).contextPath(contextPath).build();
9091
ServerWebExchange exchange = MockServerWebExchange.from(request);
9192

9293
ServerHttpRequest actual = invokeFilter(filter, exchange);
@@ -98,7 +99,7 @@ private static void testNoUrlHandling(String pattern, String path) {
9899
HttpStatus status = HttpStatus.PERMANENT_REDIRECT;
99100
filter = UrlHandlerFilter.trailingSlashHandler(pattern).redirect(status).build();
100101

101-
request = MockServerHttpRequest.get(path).build();
102+
request = MockServerHttpRequest.get(path).contextPath(contextPath).build();
102103
exchange = MockServerWebExchange.from(request);
103104

104105
actual = invokeFilter(filter, exchange);

0 commit comments

Comments
 (0)