首页 > 解决方案 > 使用 ResponseBuilder 强制位置标头中的相对 URI

问题描述

我正在使用 ResponseBuilder 类发送重定向响应

responseBuilder.header("Location", "/foo");

我的问题是 URI 解析为绝对 URI。根据文档:

如果提供了相对 URI,它将通过相对于应用程序的基本 URI 解析它来转换为绝对 URI(请参阅 UriInfo.getBaseUri())。如果为 null,任何现有的位置值都将被删除。

因为我的服务在反向代理后面运行,所以位置值的主机名错误。返回位置标头时,有什么方法可以强制使用相对 URI?

标签: java

解决方案


You can use X-Forwarded-For and X-Forwarded-Host to specify the original client or host behind a load-balancer or a reverse proxy.

Host names and ports of reverse proxies (load balancers, CDNs) may differ from the origin server handling the request, in that case the X-Forwarded-Host header is useful to determine which Host was originally used.

from Mozilla HTTP Headers documentation.


推荐阅读