如何在 Java Web 应用程序中获取服务器名称

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21482093/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 08:56:30  来源:igfitidea点击:

How to get the server name in a Java Web Application

javaweb-applications

提问by Phil

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/MyAppor 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.

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

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

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

Note (request is an HttpServletRequest)

注意(请求是一个 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

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.

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

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.

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

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

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

Thanks, Phil

谢谢,菲尔

回答by Joeri Hendrickx

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

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

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

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

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")

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

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

回答by MarcosLuna

i think your problem is not the java api to access the values you need.

我认为您的问题不是访问您需要的值的 java api。

In tomcat for example there is a server.xml file containing a pair of lines like

例如,在 tomcat 中有一个 server.xml 文件,其中包含一对类似的行

<Engine name="Catalina" defaultHost="**localhost**">

<Host name="**localhost**"  appBase="webapps"  ...

if you replace localhost with the name you want like "myhost.com" and the host where tomcat runs knows its own host name, then when you retrieve the url with the specified hostname , it will show the name you ask for

如果您将 localhost 替换为您想要的名称,例如“myhost.com”,并且 tomcat 运行的主机知道自己的主机名,那么当您检索具有指定主机名的 url 时,它将显示您要求的名称

Regards

问候

回答by Vasudev

This solved the issue for me:

这为我解决了这个问题:

InetAddress.getLocalHost().getHostName();