无法通过IP地址限制对Tomcat的访问

问题描述 投票:0回答:1

我在 Ubuntu 20.04 上运行 Tomcat 10.1.26,并且尝试通过 IP 地址限制对特定 Web 应用程序的访问。我已经看过这些文章,按照他们的说明进行操作,但无法使其工作:

在 /opt/tomcat/conf/server.xml 中,我有以下条目:

<Host name="localhost"  appBase="webapps"
       unpackWARs="true" autoDeploy="true">

       <Valve className="org.apache.catalina.valves.RemoteAddrValve"
              allow="192\.168\.\d+\.\d+"
              deny=".*" />

       <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
              prefix="localhost_access_log" suffix=".txt"
              pattern="%h %l %u %t &quot;%r&quot; %s %b" />

</Host>

这应该只允许访问 192.168.x.x IP 地址并拒绝所有其他 IP 地址的访问,但来自任何 IP 地址的访问都会受到限制。我尝试过以下方法:

  1. 使用此 RemoteAddrValve:

    <Valve className="org.apache.catalina.valves.RemoteAddrValve"
           allow="192\.168\.\d+\.\d+" />
    

    在这种情况下,可以从任何 IP 地址访问 Web 应用程序。 “允许”条目似乎被忽略。

  2. 使用此 RemoteAddrValve:

    <Valve className="org.apache.catalina.valves.RemoteAddrValve"
           deny=".*" />
    

    在这种情况下,无法从任何 IP 地址访问网络应用程序,这告诉我阀门正在工作。

  3. 我使用此 RemoteAddrValve 使用工作站的特定内部 IP 地址,认为 IP 地址范围的格式可能是错误的。我还尝试使用特定的外部 IP 地址:

    <Valve className="org.apache.catalina.valves.RemoteAddrValve"
           allow="192\.168\.10\.109" />
    

    在这种情况下,任何IP地址的访问都是被允许的。同样,“允许”条目似乎被忽略了。

  4. 将阀门添加到 /opt/tomcat/conf/content.xml 而不是 server.xml,如上述文章中的一些注释建议的那样。这失败了。

  5. 按照一些评论的建议将阀门添加到 //META-INF/content.xml 中。这导致 web 应用程序无法加载。

我没有主意,希望这里有人能找出问题所在。

tomcat
1个回答
0
投票

服务器.xml

只需添加

allow=""

我只允许两个ip访问Tomcat服务器:

  • 192.168.56.1 是我的 Virtual Box 主机网络 IP
  • 192.168.56.108是我的VM 001主机网络IP

附加信息:

  • 192.168.56.109 是我的 VM 002 主机网络 IP
...
<Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true">
<Valve className="org.apache.catalina.valves.RemoteAddrValve"
           allow="192.168.56.108|192.168.56.1" 
           />
...

测试_001

curl http://192.168.56.1:8080

reutrn:可以访问 - 成功

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <title>Apache Tomcat/10.1.24</title>
        <link href="favicon.ico" rel="icon" type="image/x-icon" />
        <link href="tomcat.css" rel="stylesheet" type="text/css" />
    </head>
...

Test_002 - 模拟源自 127.0.0.1 的请求

成功,Tomcat拒绝客户端127.0.0.1访问。

curl --interface 127.0.0.1 http://192.168.56.1:8080/
...Return Nothing...

测试_003

成功,Tomcat拒绝客户端127.0.0.1访问。

curl http://192.168.56.1:8080/hello/index3.jsp

结果:

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Show Client IP</title>
</head>
<body>
    <h2>Client IP Address:</h2>
    
    <p>192.168.56.1</p>
</body>
</html>

测试 4 - 模拟源自 127.0.0.1 的请求

成功,Tomcat拒绝客户端127.0.0.1访问。

curl --interface 127.0.0.1 http://192.168.56.1:8080/hello/index3.jsp

结果:

不返回任何内容...

index3.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Show Client IP</title>
</head>
<body>
    <h2>Client IP Address:</h2>
    <%
        String ipAddress = request.getHeader("X-Forwarded-For");
        if (ipAddress == null || ipAddress.isEmpty()) {
            ipAddress = request.getRemoteAddr();
        }
    %>
    <p><%= ipAddress %></p>
</body>
</html>

测试_005 VM001 192.168.56.108

curl http://192.168.56.1:8080/hello/index3.jsp

结果:

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Show Client IP</title>
</head>
<body>
    <h2>Client IP Address:</h2>
    
    <p>192.168.56.108</p>
</body>
</html>

测试_006 VM002 192.168.56.109

curl http://192.168.56.1:8080/hello/index3.jsp

不返回任何内容。

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.