如何在JAX-RS/Apache CXF中设置主机头 [英] How to set the Host header in JAX-RS / Apache CXF

查看:94
本文介绍了如何在JAX-RS/Apache CXF中设置主机头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过HTTPS访问服务,但是由于网络设置的限制,我试图通过ssh隧道发出请求.

I am trying to access a service over HTTPS but due to restrictive network settings I am trying to make the request through an ssh tunnel.

我使用以下命令创建隧道:

I create the tunnel with a command like:

ssh -L 9443:my-service.com:443 sdt-jump-server

ssh -L 9443:my-service.com:443 sdt-jump-server

该服务只能通过HTTPS来使用,该服务以自签名证书为宿主,并且位于使用主机名或显式Host标头将传入请求路由到适当的后端服务的负载平衡器的后面.

The service is only available via HTTPS, its hosted with a self-signed certificate, and it is behind a load-balancer that uses either the hostname or an explicit Host header to route incoming requests to the appropriate backend service.

我能够使用curl这样的方式从本地系统调用终结点

I am able to invoke the endpoint from my local system using curl like

curl -k -H'主机:my-service.com' https://localhost:9443/path

但是,当我尝试使用JAX-RS的CXF 3.1.4实现提出相同的请求时,我似乎无法使其工作.我配置了一个hostnameVerifier来允许连接,下载了服务器的证书,并将其添加到我的信任库中.现在我可以连接了,但是负载平衡器似乎不遵守我要设置的Host标头.

However, when I try to use the CXF 3.1.4 implementation of JAX-RS to make the very same request, I can't seem to make it work. I configured a hostnameVerifier to allow the connection, downloaded the server's certificate, and added it to my truststore. Now I can connect, but it seemed like the load-balancer was not honoring the Host header that I'm trying to set.

我迷路了一段时间,直到我设置了-Djavax.net.debug,然后看到传递的Host标头实际上是localhost而不是我设置的值.如何使CXF兑现我正在设置的Host标头,而不使用WebTarget的URL中的值?!

I was lost for a bit until I set -Djavax.net.debug and saw that the Host header being passed was actually localhost and not the value I set. How to make CXF honor the Host header I'm setting instead of using the value from the URL of the WebTarget?!

推荐答案

CXF使用HttpUrlConnection,因此您需要以编程方式设置系统属性

CXF uses HttpUrlConnection, so you need to set a system property programmatically

System.setProperty("sun.net.http.allowRestrictedHeaders", "true")

或在启动时:

-Dsun.net.http.allowRestrictedHeaders=true

另请参见如何覆盖http-header";主机"在HttpURLConnection中?

这篇关于如何在JAX-RS/Apache CXF中设置主机头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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