Use parsed path for checks in UrlHandlerFilter

See gh-32830
This commit is contained in:
rstoyanchev 2024-07-02 11:01:27 +01:00
parent 4054dc7088
commit 194b4cedfb
1 changed files with 6 additions and 4 deletions

View File

@ -32,6 +32,7 @@ import org.apache.commons.logging.LogFactory;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.server.PathContainer;
import org.springframework.http.server.RequestPath;
import org.springframework.lang.Nullable;
import org.springframework.util.LinkedMultiValueMap;
@ -94,7 +95,7 @@ public final class UrlHandlerFilter extends OncePerRequestFilter {
path = ServletRequestPathUtils.parseAndCache(request);
}
for (Map.Entry<Handler, List<PathPattern>> entry : this.handlers.entrySet()) {
if (!entry.getKey().canHandle(request)) {
if (!entry.getKey().canHandle(request, path)) {
continue;
}
for (PathPattern pattern : entry.getValue()) {
@ -247,7 +248,7 @@ public final class UrlHandlerFilter extends OncePerRequestFilter {
/**
* Whether the handler handles the given request.
*/
boolean canHandle(HttpServletRequest request);
boolean canHandle(HttpServletRequest request, RequestPath path);
/**
* Handle the request, possibly delegating to the rest of the filter chain.
@ -276,8 +277,9 @@ public final class UrlHandlerFilter extends OncePerRequestFilter {
}
@Override
public boolean canHandle(HttpServletRequest request) {
return request.getRequestURI().endsWith("/");
public boolean canHandle(HttpServletRequest request, RequestPath path) {
List<PathContainer.Element> elements = path.elements();
return (!elements.isEmpty() && elements.get(elements.size() - 1).value().equals("/"));
}
@Override