diff --git a/spring-web/src/main/java/org/springframework/web/filter/UrlHandlerFilter.java b/spring-web/src/main/java/org/springframework/web/filter/UrlHandlerFilter.java index ef805b2bf1..905fbaafa4 100644 --- a/spring-web/src/main/java/org/springframework/web/filter/UrlHandlerFilter.java +++ b/spring-web/src/main/java/org/springframework/web/filter/UrlHandlerFilter.java @@ -278,7 +278,7 @@ public final class UrlHandlerFilter extends OncePerRequestFilter { @Override public boolean canHandle(HttpServletRequest request, RequestPath path) { - List elements = path.elements(); + List elements = path.pathWithinApplication().elements(); return (elements.size() > 1 && elements.get(elements.size() - 1).value().equals("/")); } diff --git a/spring-web/src/main/java/org/springframework/web/filter/reactive/UrlHandlerFilter.java b/spring-web/src/main/java/org/springframework/web/filter/reactive/UrlHandlerFilter.java index 5d1d2c62f0..d76bffcee7 100644 --- a/spring-web/src/main/java/org/springframework/web/filter/reactive/UrlHandlerFilter.java +++ b/spring-web/src/main/java/org/springframework/web/filter/reactive/UrlHandlerFilter.java @@ -253,7 +253,8 @@ public final class UrlHandlerFilter implements WebFilter { @Override public boolean canHandle(ServerWebExchange exchange) { - List elements = exchange.getRequest().getPath().elements(); + ServerHttpRequest request = exchange.getRequest(); + List elements = request.getPath().pathWithinApplication().elements(); return (elements.size() > 1 && elements.get(elements.size() - 1).value().equals("/")); } diff --git a/spring-web/src/test/java/org/springframework/web/filter/UrlHandlerFilterTests.java b/spring-web/src/test/java/org/springframework/web/filter/UrlHandlerFilterTests.java index d3d1ee73bb..41a4f21555 100644 --- a/spring-web/src/test/java/org/springframework/web/filter/UrlHandlerFilterTests.java +++ b/spring-web/src/test/java/org/springframework/web/filter/UrlHandlerFilterTests.java @@ -87,17 +87,20 @@ public class UrlHandlerFilterTests { @Test void noUrlHandling() throws Exception { - testNoUrlHandling("/path/**", "/path/123"); - testNoUrlHandling("/path/*", "/path/123"); - testNoUrlHandling("/**", "/"); // gh-33444 + testNoUrlHandling("/path/**", "", "/path/123"); + testNoUrlHandling("/path/*", "", "/path/123"); + testNoUrlHandling("/**", "", "/"); // gh-33444 + testNoUrlHandling("/**", "/myApp", "/myApp/"); // gh-33565 } - private static void testNoUrlHandling(String pattern, String requestURI) throws ServletException, IOException { + private static void testNoUrlHandling(String pattern, String contextPath, String requestURI) + throws ServletException, IOException { // No request wrapping UrlHandlerFilter filter = UrlHandlerFilter.trailingSlashHandler(pattern).wrapRequest().build(); MockHttpServletRequest request = new MockHttpServletRequest("GET", requestURI); + request.setContextPath(contextPath); MockFilterChain chain = new MockFilterChain(); filter.doFilterInternal(request, new MockHttpServletResponse(), chain); @@ -109,6 +112,7 @@ public class UrlHandlerFilterTests { filter = UrlHandlerFilter.trailingSlashHandler(pattern).redirect(status).build(); request = new MockHttpServletRequest("GET", requestURI); + request.setContextPath(contextPath); MockHttpServletResponse response = new MockHttpServletResponse(); chain = new MockFilterChain(); diff --git a/spring-web/src/test/java/org/springframework/web/filter/reactive/UrlHandlerFilterTests.java b/spring-web/src/test/java/org/springframework/web/filter/reactive/UrlHandlerFilterTests.java index a92f2194af..d3288302f7 100644 --- a/spring-web/src/test/java/org/springframework/web/filter/reactive/UrlHandlerFilterTests.java +++ b/spring-web/src/test/java/org/springframework/web/filter/reactive/UrlHandlerFilterTests.java @@ -76,17 +76,18 @@ public class UrlHandlerFilterTests { @Test void noUrlHandling() { - testNoUrlHandling("/path/**", "/path/123"); - testNoUrlHandling("/path/*", "/path/123"); - testNoUrlHandling("/**", "/"); // gh-33444 + testNoUrlHandling("/path/**", "", "/path/123"); + testNoUrlHandling("/path/*", "", "/path/123"); + testNoUrlHandling("/**", "", "/"); // gh-33444 + testNoUrlHandling("/**", "/myApp", "/myApp/"); // gh-33565 } - private static void testNoUrlHandling(String pattern, String path) { + private static void testNoUrlHandling(String pattern, String contextPath, String path) { // No request mutation UrlHandlerFilter filter = UrlHandlerFilter.trailingSlashHandler(pattern).mutateRequest().build(); - MockServerHttpRequest request = MockServerHttpRequest.get(path).build(); + MockServerHttpRequest request = MockServerHttpRequest.get(path).contextPath(contextPath).build(); ServerWebExchange exchange = MockServerWebExchange.from(request); ServerHttpRequest actual = invokeFilter(filter, exchange); @@ -98,7 +99,7 @@ public class UrlHandlerFilterTests { HttpStatus status = HttpStatus.PERMANENT_REDIRECT; filter = UrlHandlerFilter.trailingSlashHandler(pattern).redirect(status).build(); - request = MockServerHttpRequest.get(path).build(); + request = MockServerHttpRequest.get(path).contextPath(contextPath).build(); exchange = MockServerWebExchange.from(request); actual = invokeFilter(filter, exchange);