首页 > 解决方案 > 使用负载均衡器限制对 azure vm 中 tomcat 应用程序的 Internet 访问

问题描述

我的设置是:面向公众的 LB-Linux VM-Apache tomcat:2 个应用程序-https://example.comhttps://example.com/api/xxx. 现在所有安全组和规则都已到位,并且能够完美地访问所有内容。

需要:需要限制https://example.com从互联网访问 url。它只能从客户端的内部网络访问。

到目前为止完成:由于 LB 不支持基于 url 的限制,因此考虑在 tomcat 中使用RemoteCIDRValve. 在各自的上下文中提供以下内容。

<Valve className="org.apache.catalina.valves.RemoteCIDRValve" allow="111.11.111.0/22,222.22.222.0/22, ::1"/>

但它也允许所有其他 IP 地址。这是因为当请求进来时,它是通过负载均衡器进来的,所以 IP 在允许的 CIDR 范围内。我最初的想法是,LB 会从请求发起的地方发送客户端的 ip。

请为解决此问题提供一些帮助。这需要纠正什么?或任何其他方法来解决它...

我的完整配置如下。这是在 HOST 里面:

<Valve className="org.apache.catalina.valves.RemoteIpValve"  />

<Host name="example.com" appBase="xxxapps"
    unpackWARs="true"  autoDeploy="true" deployOnStartup="true">

    <Context name="API" path="/yyy" docBase="yyy.war"></Context>

    <Context name="Portal" path="" docBase="zzz.war">
        <Valve className="org.apache.catalina.valves.RemoteCIDRValve"
        allow="xxx.yyy.zz.d/y, ::1"/>
    </Context>

    <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs/xxxlogs"
        prefix="xxx_access_log" suffix=".txt"
        pattern="%h %l %u %t &quot;%r&quot; %s %b %{x-forwarded-for}i %{x-forwarded-by}i"
        requestAttributesEnabled="true" />
</Host>

标签: azuretomcat9azure-load-balancerip-restrictions

解决方案


如果负载均衡器添加了X-Forwarded-For标头(很可能),您只需要在RemoteIpValve引擎中添加一个:

<Engine name="Catalina">
    <Valve className="org.apache.catalina.valves.RemoteIpValve" />
    ...
</Engine>

在最近的 Tomcat 版本中,所有阀门属性都有合理的默认值。

编辑:正如您在评论中所说,您的域名还指向 Cloudflare 服务器。我不知道他们如何为客户分配 IP,但如果 IP 数量有限(例如172.70.95.0/24网络),您可以使用:

<Engine name="Catalina">
    <Valve className="org.apache.catalina.valves.RemoteIpValve"
           trustedProxies="172\.70\.95\.\d+" />
    ...
</Engine>

推荐阅读