如何在 Java Web 应用程序中获取服务器名称 [英] How to get the server name in a Java Web Application

查看:126
本文介绍了如何在 Java Web 应用程序中获取服务器名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Web 应用程序,用户可以将其部署在他们自己的 Java Web 服务器(例如 Tomcat)上.Web Application 的 Java 端需要报告 Web Application 本身的 URL(例如 http://aServer.com:8080/MyApphttps://blah.blahSever/MyApp).然而,由于许多用户使用端口转发和其他网络技术,Web 应用程序经常报告错误的名称.

I have a Web Application that users deploy on their own Java Web Servers (e.g. Tomcat). The Java side of Web Application needs to report the URL of the Web Application itself (e.g. http://aServer.com:8080/MyApp or https://blah.blahSever/MyApp). However since a number of users use port-forwarding and other network techniques, the Web Application often reports the wrong name.

我尝试了以下技术,但通常它们实际上并不能产生用户需要的东西.

I have tried the following techniques but often they don't actually produce what the user requires.

注意(请求是一个HttpServletRequest)

Note (request is an HttpServletRequest)

request.getLocalAddr();    // Returns: 127.0.0.1
request.getLocalName();    // Returns: localhost
request.getServerName();   // Returns: localhost
request.getServerPort();   // Returns: 8080
request.getContextPath();  // Returns: /MyApp

request.getScheme();       // Returns: http

InetAddress.getLocalHost().getHostName();           // Returns: serverXXX-XXX-XXX-XX.xxxx-xxxxxxx.net
InetAddress.getLocalHost().getHostAddress();        // Returns: xxx.xxx.xxx.xxx (ip address)
InetAddress.getLocalHost().getCanonicalHostName();  // Returns: serverXXX-XXX-XXX-XX.xxxx-xxxxxxx.net

InetAddress 的使用接近我想要的,但由于我们在 Apache2 服务器中使用服务器别名和 ProxyPass,来自 InetAddress 的值是服务器的实际值,而不是别名.

The use of InetAddress gets close to what I want but since we are using Server Aliases and ProxyPass in our Apache2 server, the value from InetAddress is the actual values of the server rather than the Alias.

我能想到的唯一解决方法是,用户在属性文件中提供一个属性,Web 应用程序在启动时读取该属性.如果设置了该属性,则该值用于返回完整的 Web 应用程序路径(例如 serverUrl = https://blah.blahServer/我的应用程序).这种技术可行,但需要为我的客户做更多的部署工作.

The only technique I can think of to get round this, is that the user provides a property in a properties file, which the Web Application reads on startup. If the property is set, this value is used to return the full web application path (e.g. serverUrl = https://blah.blahServer/MyApp) . This technique would work, but involves more deployment work for my customers.

有人对我如何实现更优雅的解决方案有任何想法吗?

Does anyone have any ideas as to how I can achieve a more elegant solution?

谢谢,菲尔

推荐答案

检查主机"请求标头.大多数客户端添加该标头以指示他们尝试联系的主机.

Check the 'host' request header. Most client add that header to indicate the host they're trying to contact.

这与用于 VirtualHosts 的标头相同,因此它非常标准.这在 HTTP1.1 请求中也是必须的.

This is the same header that's used for VirtualHosts, so it's pretty standard. It's also obligatory in HTTP1.1 requests.

实际上,您无法完全从服务器端获取该值,因为用户可能已经为您的服务器定义了自己的主机名.

In fact, you cannot get the value purely from the server side, since it's possible that the user has defined its own hostname for your server.

request.getHeader("Host")

如果您的服务器位于 apache 保留代理之后,请使用 ProxyPreserveHost 指令以确保 Host 标头保持完整.我相信其他反向代理也有类似的选择.

If your server is behind an apache reserve proxy, use the ProxyPreserveHost directive to make sure the Host header is kept intact. I'm sure other reverse proxies have similar options.

这篇关于如何在 Java Web 应用程序中获取服务器名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆