refactor: Update RequestUrlUtils to use default public URL for internal requests and clean up unused browser origin logic
This commit is contained in:
@@ -54,14 +54,13 @@ class OutdoorFairControllerTest
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void companyQrCodeUrlUsesBrowserRefererWhenProxyRewritesHost() throws Exception
|
void companyQrCodeUrlUsesDefaultPublicUrlWhenRequestHostIsInternal() throws Exception
|
||||||
{
|
{
|
||||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||||
request.setScheme("http");
|
request.setScheme("http");
|
||||||
request.setServerName("127.0.0.1");
|
request.setServerName("127.0.0.1");
|
||||||
request.setServerPort(9091);
|
request.setServerPort(9091);
|
||||||
request.addHeader("Host", "127.0.0.1:9091");
|
request.addHeader("Host", "127.0.0.1:9091");
|
||||||
request.addHeader("Referer", "http://39.98.44.136:6024/shihezi/jobfair/outdoor-fair/detail?id=8");
|
|
||||||
|
|
||||||
assertEquals(
|
assertEquals(
|
||||||
"http://39.98.44.136:6024/h5/outdoor-fair/company.html?fairId=12&companyId=34",
|
"http://39.98.44.136:6024/h5/outdoor-fair/company.html?fairId=12&companyId=34",
|
||||||
|
|||||||
@@ -1,8 +1,6 @@
|
|||||||
package com.ruoyi.common.utils;
|
package com.ruoyi.common.utils;
|
||||||
|
|
||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
import java.net.URI;
|
|
||||||
import java.net.URISyntaxException;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 请求公开地址工具。
|
* 请求公开地址工具。
|
||||||
@@ -12,6 +10,9 @@ import java.net.URISyntaxException;
|
|||||||
*/
|
*/
|
||||||
public final class RequestUrlUtils
|
public final class RequestUrlUtils
|
||||||
{
|
{
|
||||||
|
/** 反向代理未透传公开地址时使用的默认访问地址(不带末尾斜杠,便于拼接路径)。 */
|
||||||
|
private static final String DEFAULT_PUBLIC_BASE_URL = "http://39.98.44.136:6024";
|
||||||
|
|
||||||
private RequestUrlUtils()
|
private RequestUrlUtils()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
@@ -27,16 +28,16 @@ public final class RequestUrlUtils
|
|||||||
String forwardedHost = firstHeaderValue(request.getHeader("X-Forwarded-Host"));
|
String forwardedHost = firstHeaderValue(request.getHeader("X-Forwarded-Host"));
|
||||||
String hostHeader = request.getHeader("Host");
|
String hostHeader = request.getHeader("Host");
|
||||||
String requestHost = firstNonBlank(hostHeader, request.getServerName());
|
String requestHost = firstNonBlank(hostHeader, request.getServerName());
|
||||||
URI browserOrigin = isInternalHost(requestHost)
|
String forwardedPublicHost = firstNonBlank(forwarded, forwardedHost);
|
||||||
? firstBrowserOrigin(request)
|
if (isInternalHost(requestHost) && isInternalHost(forwardedPublicHost)) {
|
||||||
: null;
|
String contextPath = request.getContextPath();
|
||||||
String browserHost = browserOrigin == null ? null : getAuthority(browserOrigin);
|
return DEFAULT_PUBLIC_BASE_URL + (contextPath == null ? "" : contextPath);
|
||||||
|
}
|
||||||
boolean hasPublicHost = !isBlank(forwarded) || !isBlank(forwardedHost)
|
boolean hasPublicHost = !isBlank(forwarded) || !isBlank(forwardedHost)
|
||||||
|| !isBlank(hostHeader) || browserOrigin != null;
|
|| !isBlank(hostHeader);
|
||||||
String host = firstNonBlank(
|
String host = firstNonBlank(
|
||||||
forwarded,
|
forwarded,
|
||||||
forwardedHost,
|
forwardedHost,
|
||||||
browserHost,
|
|
||||||
hostHeader,
|
hostHeader,
|
||||||
request.getServerName());
|
request.getServerName());
|
||||||
host = normalizeHost(host);
|
host = normalizeHost(host);
|
||||||
@@ -44,17 +45,12 @@ public final class RequestUrlUtils
|
|||||||
String scheme = firstNonBlank(
|
String scheme = firstNonBlank(
|
||||||
firstForwardedValue(request.getHeader("Forwarded"), "proto"),
|
firstForwardedValue(request.getHeader("Forwarded"), "proto"),
|
||||||
firstHeaderValue(request.getHeader("X-Forwarded-Proto")),
|
firstHeaderValue(request.getHeader("X-Forwarded-Proto")),
|
||||||
browserOrigin == null ? null : browserOrigin.getScheme(),
|
|
||||||
request.getScheme());
|
request.getScheme());
|
||||||
scheme = normalizeScheme(scheme, request.getScheme());
|
scheme = normalizeScheme(scheme, request.getScheme());
|
||||||
|
|
||||||
String forwardedPort = firstNonBlank(
|
String forwardedPort = firstNonBlank(
|
||||||
extractPort(forwarded),
|
extractPort(forwarded),
|
||||||
firstHeaderValue(request.getHeader("X-Forwarded-Port")),
|
firstHeaderValue(request.getHeader("X-Forwarded-Port")));
|
||||||
browserOrigin == null ? null : String.valueOf(browserOrigin.getPort()));
|
|
||||||
if ("-1".equals(forwardedPort)) {
|
|
||||||
forwardedPort = null;
|
|
||||||
}
|
|
||||||
if (!hasPort(host) && shouldIncludePort(forwardedPort, scheme)) {
|
if (!hasPort(host) && shouldIncludePort(forwardedPort, scheme)) {
|
||||||
host += ":" + forwardedPort;
|
host += ":" + forwardedPort;
|
||||||
}
|
}
|
||||||
@@ -67,46 +63,6 @@ public final class RequestUrlUtils
|
|||||||
return scheme + "://" + host + (contextPath == null ? "" : contextPath);
|
return scheme + "://" + host + (contextPath == null ? "" : contextPath);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 反向代理未透传 Host 时,Servlet 可能只能看到 127.0.0.1:9091。
|
|
||||||
* 对浏览器请求,Origin/Referer 仍然包含用户实际访问的公开地址,作为兜底来源。
|
|
||||||
*/
|
|
||||||
private static URI firstBrowserOrigin(HttpServletRequest request)
|
|
||||||
{
|
|
||||||
URI origin = parseHttpUri(firstHeaderValue(request.getHeader("Origin")));
|
|
||||||
return origin != null
|
|
||||||
? origin
|
|
||||||
: parseHttpUri(firstHeaderValue(request.getHeader("Referer")));
|
|
||||||
}
|
|
||||||
|
|
||||||
private static URI parseHttpUri(String value)
|
|
||||||
{
|
|
||||||
if (isBlank(value)) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
URI uri = new URI(value.trim());
|
|
||||||
String scheme = uri.getScheme();
|
|
||||||
return uri.getHost() != null && ("http".equalsIgnoreCase(scheme)
|
|
||||||
|| "https".equalsIgnoreCase(scheme))
|
|
||||||
? uri : null;
|
|
||||||
} catch (URISyntaxException e) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static String getAuthority(URI uri)
|
|
||||||
{
|
|
||||||
String host = uri.getHost();
|
|
||||||
if (host == null) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
if (host.indexOf(':') >= 0 && !host.startsWith("[")) {
|
|
||||||
host = "[" + host + "]";
|
|
||||||
}
|
|
||||||
return uri.getPort() > 0 ? host + ":" + uri.getPort() : host;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static boolean isInternalHost(String host)
|
private static boolean isInternalHost(String host)
|
||||||
{
|
{
|
||||||
if (isBlank(host)) {
|
if (isBlank(host)) {
|
||||||
|
|||||||
Reference in New Issue
Block a user