提交 95818609 编写于 作者: R Rossen Stoyanchev

Minor refactoring

Consolidate into one method potentially re-using UriComponentsBuilder
for the location. Also use StringUtils#applyRelativePath.

Issue: SPR-15020
上级 db2ebd30
...@@ -23,7 +23,6 @@ import java.util.List; ...@@ -23,7 +23,6 @@ import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import javax.servlet.FilterChain; import javax.servlet.FilterChain;
import javax.servlet.ServletException; import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
...@@ -35,6 +34,7 @@ import org.springframework.http.HttpRequest; ...@@ -35,6 +34,7 @@ import org.springframework.http.HttpRequest;
import org.springframework.http.server.ServletServerHttpRequest; import org.springframework.http.server.ServletServerHttpRequest;
import org.springframework.util.CollectionUtils; import org.springframework.util.CollectionUtils;
import org.springframework.util.LinkedCaseInsensitiveMap; import org.springframework.util.LinkedCaseInsensitiveMap;
import org.springframework.util.StringUtils;
import org.springframework.web.util.UriComponents; import org.springframework.web.util.UriComponents;
import org.springframework.web.util.UriComponentsBuilder; import org.springframework.web.util.UriComponentsBuilder;
import org.springframework.web.util.UrlPathHelper; import org.springframework.web.util.UrlPathHelper;
...@@ -230,10 +230,12 @@ public class ForwardedHeaderFilter extends OncePerRequestFilter { ...@@ -230,10 +230,12 @@ public class ForwardedHeaderFilter extends OncePerRequestFilter {
} }
private static class ForwardedHeaderResponseWrapper extends HttpServletResponseWrapper { private static class ForwardedHeaderResponseWrapper extends HttpServletResponseWrapper {
private static final String FOLDER_SEPARATOR = "/"; private static final String FOLDER_SEPARATOR = "/";
private final HttpServletRequest request; private final HttpServletRequest request;
public ForwardedHeaderResponseWrapper(HttpServletResponse response, HttpServletRequest request) { public ForwardedHeaderResponseWrapper(HttpServletResponse response, HttpServletRequest request) {
super(response); super(response);
this.request = request; this.request = request;
...@@ -241,46 +243,37 @@ public class ForwardedHeaderFilter extends OncePerRequestFilter { ...@@ -241,46 +243,37 @@ public class ForwardedHeaderFilter extends OncePerRequestFilter {
@Override @Override
public void sendRedirect(String location) throws IOException { public void sendRedirect(String location) throws IOException {
String forwardedLocation = forwardedLocation(location);
super.sendRedirect(forwardedLocation); UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(location);
}
private String forwardedLocation(String location) { // Absolute location
if(hasScheme(location)) { if (builder.build().getScheme() != null) {
return location; super.sendRedirect(location);
return;
} }
return createForwardedLocation(location); // Network-path reference
} if(location.startsWith("//")) {
String scheme = this.request.getScheme();
private String createForwardedLocation(String location) { super.sendRedirect(builder.scheme(scheme).toUriString());
boolean isNetworkPathReference = location.startsWith("//"); return;
if(isNetworkPathReference) {
UriComponentsBuilder schemeForwardedLocation = UriComponentsBuilder.fromUriString(location).scheme(request.getScheme());
return schemeForwardedLocation.toUriString();
} }
HttpRequest httpRequest = new ServletServerHttpRequest(request); // Relative to Servlet container root or to current request
UriComponentsBuilder forwardedLocation = UriComponentsBuilder.fromHttpRequest(httpRequest); String path;
boolean isRelativeToContextPath = location.startsWith(FOLDER_SEPARATOR); if (location.startsWith(FOLDER_SEPARATOR)) {
if(isRelativeToContextPath) { path = this.request.getContextPath() + location;
forwardedLocation.replacePath(request.getContextPath()); }
} else if(endsWithFileSpecificPart(forwardedLocation)) { else {
// remove a file specific part from existing request path = StringUtils.applyRelativePath(this.request.getRequestURI(), location);
forwardedLocation.path("/../");
} }
forwardedLocation.path(location);
return forwardedLocation.build().normalize().toUriString();
}
private boolean endsWithFileSpecificPart(UriComponentsBuilder forwardedLocation) { String result = UriComponentsBuilder
return !forwardedLocation.build().getPath().endsWith(FOLDER_SEPARATOR); .fromHttpRequest(new ServletServerHttpRequest(this.request))
} .replacePath(path)
.build().normalize().toUriString();
private boolean hasScheme(String location) { super.sendRedirect(result);
String locationScheme = UriComponentsBuilder.fromUriString(location).build().getScheme();
return locationScheme != null;
} }
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册