Parsed RequestPath is recalculated on Forward

Closes gh-26318
This commit is contained in:
Rossen Stoyanchev
2021-01-06 18:32:26 +00:00
parent dcc8dcdff8
commit c040cd7b05
4 changed files with 76 additions and 10 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -24,6 +24,7 @@ import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import org.springframework.http.server.RequestPath;
import org.springframework.web.util.ServletRequestPathUtils;
/**
@@ -48,12 +49,13 @@ public class ServletRequestPathFilter implements Filter {
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
RequestPath previousRequestPath = (RequestPath) request.getAttribute(ServletRequestPathUtils.PATH_ATTRIBUTE);
ServletRequestPathUtils.parseAndCache((HttpServletRequest) request);
try {
chain.doFilter(request, response);
}
finally {
ServletRequestPathUtils.clearParsedRequestPath(request);
ServletRequestPathUtils.setParsedRequestPath(previousRequestPath, request);
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -20,6 +20,7 @@ import javax.servlet.http.HttpServletRequest;
import org.springframework.http.server.PathContainer;
import org.springframework.http.server.RequestPath;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
/**
@@ -71,6 +72,22 @@ public abstract class ServletRequestPathUtils {
return path;
}
/**
* Set the cached, parsed {@code RequestPath} to the given value.
* @param requestPath the value to set to, or if {@code null} the cache
* value is cleared.
* @param request the current request
* @since 5.3.3
*/
public static void setParsedRequestPath(@Nullable RequestPath requestPath, ServletRequest request) {
if (requestPath != null) {
request.setAttribute(PATH_ATTRIBUTE, requestPath);
}
else {
request.removeAttribute(PATH_ATTRIBUTE);
}
}
/**
* Check for a {@link #parseAndCache previously} parsed and cached {@code RequestPath}.
*/