优化 baseURL 的获取逻辑 (#376)

pom
kl 2 years ago committed by GitHub
parent 026656711d
commit 106036d989
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -36,24 +36,26 @@ public class BaseUrlFilter implements Filter {
public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException { public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException {
String baseUrl; String baseUrl;
String baseUrlTmp = ConfigConstants.getBaseUrl(); String configBaseUrl = ConfigConstants.getBaseUrl();
final HttpServletRequest servletRequest = (HttpServletRequest) request; final HttpServletRequest servletRequest = (HttpServletRequest) request;
// 支持通过 http header 中 X-Base-Url 来动态设置 baseUrl 以支持多个域名/项目的共享使用 //1、支持通过 http header 中 X-Base-Url 来动态设置 baseUrl 以支持多个域名/项目的共享使用
final String urlInHeader = servletRequest.getHeader("X-Base-Url"); final String urlInHeader = servletRequest.getHeader("X-Base-Url");
if (StringUtils.isNotEmpty(urlInHeader)) { if (StringUtils.isNotEmpty(urlInHeader)) {
baseUrl = urlInHeader; baseUrl = urlInHeader;
} else if (baseUrlTmp != null && !ConfigConstants.DEFAULT_BASE_URL.equalsIgnoreCase(baseUrlTmp)) { } else if (configBaseUrl != null && !ConfigConstants.DEFAULT_BASE_URL.equalsIgnoreCase(configBaseUrl)) {
// 如果配置文件中配置了 baseUrl 且不为 default 则以配置文件为准 //2、如果配置文件中配置了 baseUrl 且不为 default 则以配置文件为准
if (!baseUrlTmp.endsWith("/")) { baseUrl = configBaseUrl;
baseUrlTmp = baseUrlTmp.concat("/");
}
baseUrl = baseUrlTmp;
} else { } else {
// 动态拼接 baseUrl //3、默认动态拼接 baseUrl
baseUrl = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() baseUrl = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
+ servletRequest.getContextPath() + "/"; + servletRequest.getContextPath() + "/";
} }
if (!baseUrl.endsWith("/")) {
baseUrl = baseUrl.concat("/");
}
BASE_URL = baseUrl; BASE_URL = baseUrl;
request.setAttribute("baseUrl", baseUrl); request.setAttribute("baseUrl", baseUrl);
filterChain.doFilter(request, response); filterChain.doFilter(request, response);

Loading…
Cancel
Save