如何从http或https请求获取带端口的主机名 [英] How to get host name with port from a http or https request

查看:1826
本文介绍了如何从http或https请求获取带端口的主机名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在jboss容器中部署了两个应用程序(同一个unix框)。如果我从app1收到请求,我需要为app2设置相应的请求。

I have two applications deployed in jboss container(same unix box). If i get a request from app1, i need to frame a corresponding request for app2.

例如:

if app1请求是: http://example.com/context?param1=123

然后我需要提取 http://example.com/ ,以便我可以框架请求第二个应用程序。

eg:
if app1 request is: http://example.com/context?param1=123
then I need to extract "http://example.com/", so that I can frame request for second app.

我尝试使用:

  HttpServletRequest.getServerName() & 
  HttpServletRequest.getServerPort() & \
  HttpServletRequest.getHeader("host") 

方法,但请求可能是http或https。

methods, but the request may be of http or https.

如果还有其他更好的方法,请告诉我。
谢谢!

Please let me know if there is any other better way. Thanks!

推荐答案

您可以使用 HttpServletRequest.getScheme()检索http或https。

You can use HttpServletRequest.getScheme() to retrieve either "http" or "https".

将它与一起使用HttpServletRequest.getServerName()应该足以重建您需要的URL部分。

Using it along with HttpServletRequest.getServerName() should be enough to rebuild the portion of the URL you need.

如果您使用标准端口,则无需在URL中明确放置端口( http为80,https为443。

You don't need to explicitly put the port in the URL if you're using the standard ones (80 for http and 443 for https).

编辑:如果您的servlet容器位于终止SSL的反向代理或负载均衡器后面,它有点棘手,因为请求以普通的http转发到servlet容器。你有几个选择:

Edit: If your servlet container is behind a reverse proxy or load balancer that terminates the SSL, it's a bit trickier because the requests are forwarded to the servlet container as plain http. You have a few options:

1)使用 HttpServletRequest.getHeader(x-forwarded-proto)代替;这只适用于您的负载均衡器正确设置标头(Apache应该是afaik)。

1) Use HttpServletRequest.getHeader("x-forwarded-proto") instead; this only works if your load balancer sets the header correctly (Apache should afaik).

2)配置 RemoteIpValve 将使 getScheme()工作正如所料。同样,这仅在负载均衡器设置正确的标头时才有效。

2) Configure a RemoteIpValve in JBoss/Tomcat that will make getScheme() work as expected. Again, this will only work if the load balancer sets the correct headers.

3)如果上述方法不起作用,您可以在Tomcat / JBoss中配置两个不同的连接器,一个用于http,一个用于https,如中所述这篇文章

3) If the above don't work, you could configure two different connectors in Tomcat/JBoss, one for http and one for https, as described in this article.

这篇关于如何从http或https请求获取带端口的主机名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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