azure - 使用负载均衡器限制对 azure vm 中 tomcat 应用程序的 Internet 访问
问题描述
我的设置是:面向公众的 LB-Linux VM-Apache tomcat:2 个应用程序-https://example.com
和https://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 "%r" %s %b %{x-forwarded-for}i %{x-forwarded-by}i"
requestAttributesEnabled="true" />
</Host>
解决方案
如果负载均衡器添加了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>
推荐阅读
- bash - 如何提取简单文本并存储到文件中?
- javascript - Typescript,通用可变参数工厂函数返回元组
- python - 在 Pandas 中加入两列字典
- python-3.x - 将鼠标悬停在 Tkinter 中的按钮上时如何停止颜色变化?
- css - 如何跟踪和查找开发工具中所做更改的源代码?
- react-bootstrap - React.Children.only 期望接收单个 React 元素子元素。但文件中只有独生子女?
- c++ - 如何将 QDockWidgets 添加为选项卡?
- java - 当我尝试从 Zuul 代理验证 JWT 时,为什么会出现 403 被禁止?
- gitlab - 如何使用 Gitlab API 触发 Gitlab-CI 管道以获取最新标签?
- css - 在Bootstrap中将8张相同大小的卡片放在一行中