You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WebLogic中Spring应用:HttpServletRequest getRequestURL返回机器名而非域名

解决WebLogic下Spring应用获取正确子域名的问题

出现这个问题的核心原因是WebLogic默认使用自身配置的机器名生成请求URL,而非客户端实际请求的域名。以下是几种可行的解决方式:

1. 直接从请求头获取真实域名

客户端请求时的域名会包含在Host请求头里,直接读取这个值是最可靠的方式,代码示例:

HttpServletRequest httpRequest = (HttpServletRequest) request;
// 获取完整域名(含端口,如secure.asd.com或www.asd.com:8080)
String realDomain = httpRequest.getHeader("Host");

// 拆分获取子域名执行对应逻辑
if (realDomain != null) {
    // 处理带端口的情况,先去掉端口部分
    String domainWithoutPort = realDomain.split(":")[0];
    String[] domainParts = domainWithoutPort.split("\\.");
    if (domainParts.length >= 2) {
        String subdomain = domainParts[0];
        if ("secure".equals(subdomain)) {
            // 执行secure子域专属逻辑
        } else if ("www".equals(subdomain)) {
            // 执行www子域专属逻辑
        }
    }
}

2. 配置WebLogic保留客户端Host头

如果需要让getRequestURL()返回正确域名,需确保WebLogic接收并使用客户端的Host头:

  • 登录WebLogic控制台,找到目标服务器的「HTTP」配置项,设置「前端主机」为对应的域名(如secure.asd.com或统一配置负载均衡的域名),同时设置「前端端口」(HTTP用80,HTTPS用443)。
  • 若前端使用反向代理(如Nginx、Apache),需确保代理转发时传递Host头(比如Nginx添加proxy_set_header Host $host;),同时在WebLogic启动参数中添加:-Dweblogic.servlet.proxyMode=true。

3. 通过web.xml配置代理参数

在Spring应用的web.xml中添加以下参数,让WebLogic识别代理传递的真实请求信息:

<context-param>
    <param-name>Proxy-Preserve-Host</param-name>
    <param-value>true</param-value>
</context-param>
<context-param>
    <param-name>WLProxySSL</param-name>
    <param-value>true</param-value> <!-- 若使用HTTPS需开启 -->
</context-param>
<context-param>
    <param-name>WLProxyPort</param-name>
    <param-value>443</param-value> <!-- HTTPS端口,HTTP则用80 -->
</context-param>

内容的提问来源于stack exchange,提问作者Vanan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.14 19:15:10