在 Java Web 应用程序中处理 X-FORWARDED-PROTO 标头

发布于 2024-11-02 19:17:55 字数 637 浏览 8 评论 0原文

任何人都可以指导我使用 X-部署到 Apache Tomcat 的 Java Web 应用程序中的 FORWARDED-PROTO 标头。

应用程序设置的方式是,tomcat 与 Apache Web 服务器通信,Apache Web 服务器又与 Cisco 负载均衡器通信,最后均衡器将页面发布到客户端(tomcat -> apache2 -> 负载均衡器 -> 客户端)。

SSL 证书安装在负载均衡器中,并且正在处理 HTTPS 请求。我的要求是让应用程序以使用 X-FORWARDED-PROTO 的方式运行,并将页面更改为 HTTP 或 HTTPS。

检查我网页的标头文件,我找不到 X-FORWARDED-PROTO 标头。我也无权访问负载均衡器配置,IT 建议我们使用 X-FORWARDED-PROTO 来区分 HTTP 和 HTTPS 请求。

是否需要在 Tomcat 或 Apache 级别进行任何配置,以便返回 X-FORWARDED-PROTO 标头?或者配置应该在负载均衡器中处理?

Can any one guide me in working with X-FORWARDED-PROTO header in Java web application deployed to Apache Tomcat.

The application setup is in such a way that tomcat talks with Apache webserver, which in turn talks with Cisco Load Balancer, finally the balancer publishes the pages to the client (tomcat -> apache2 -> load balancer -> client).

The SSL Certificate is installed in Load Balancer and it's handling HTTPS requests. My requirement is to make the application behave in such a way that it uses the X-FORWARDED-PROTO and change the pages as HTTP or HTTPS.

Checking on the header files of my webpages I could not find the X-FORWARDED-PROTO header. I don't have access to the Load Balancer configuration either, and the IT has suggested us to use the X-FORWARDED-PROTO to differentiate between HTTP and HTTPS request.

Is there any configuration to be done in Tomcat or Apache level so that it will return the X-FORWARDED-PROTO header? Or is it that the configuration should be handled in Load Balancer?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

恋竹姑娘 2024-11-09 19:17:55

我很确定您现在已经弄清楚了,但我仍然会添加答案。

您可以在tomcat的conf/server.xml中的引擎标签中使用类org.apache.catalina.valves.RemoteIpValve

    <Valve className="org.apache.catalina.valves.RemoteIpValve"
           internalProxies="192.168.1.XXX"
           remoteIpHeader="x-forwarded-for"
           remoteIpProxiesHeader="x-forwarded-by"
           protocolHeader="x-forwarded-proto"
    />

需要注意的是,设置 internalProxies 值非常重要。如果未设置此选项并且您使用非标准网络设置,则可能会导致一些问题,其中 tomcat 不会检查 x-forwarded 标头,并且默认为“http”。出于安全原因,我建议设置它,即使它适用于默认值。

请参阅此处了解更多信息。

I am pretty sure you have it all figured out by now but I will add the answer nonetheless.

You can use the class org.apache.catalina.valves.RemoteIpValve in the engine tag in conf/server.xml of tomcat.

    <Valve className="org.apache.catalina.valves.RemoteIpValve"
           internalProxies="192.168.1.XXX"
           remoteIpHeader="x-forwarded-for"
           remoteIpProxiesHeader="x-forwarded-by"
           protocolHeader="x-forwarded-proto"
    />

Something to note that is very important is to set the internalProxies value. If this is not set and you are you using a non-standard network setup it could cause some issues where tomcat will not check for x-forwarded headers and it will default to "http". For security reasons I'd recommend to set it even if it works with the defaults.

Look here for more information.

江南烟雨〆相思醉 2024-11-09 19:17:55

将其添加到您的 apache vhost 管理连接中,

<VirtualHost *:80>
  ...
  RewriteEngine On
  RewriteCond %{HTTP:X-Forwarded-Proto} !https
  RewriteRule !/status https://%{SERVER_NAME}%{REQUEST_URI} [L,R]
</VirtualHost>

假设您的健康检查是 /status,这不需要 https

Add this to your apache vhost managing connections

<VirtualHost *:80>
  ...
  RewriteEngine On
  RewriteCond %{HTTP:X-Forwarded-Proto} !https
  RewriteRule !/status https://%{SERVER_NAME}%{REQUEST_URI} [L,R]
</VirtualHost>

this assumes your health check is /status, which doesn’t require https

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文