我在 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 "%r" %s %b" />
</Host>
这应该只允许访问 192.168.x.x IP 地址并拒绝所有其他 IP 地址的访问,但来自任何 IP 地址的访问都会受到限制。我尝试过以下方法:
使用此 RemoteAddrValve:
<Valve className="org.apache.catalina.valves.RemoteAddrValve"
allow="192\.168\.\d+\.\d+" />
在这种情况下,可以从任何 IP 地址访问 Web 应用程序。 “允许”条目似乎被忽略。
使用此 RemoteAddrValve:
<Valve className="org.apache.catalina.valves.RemoteAddrValve"
deny=".*" />
在这种情况下,无法从任何 IP 地址访问网络应用程序,这告诉我阀门正在工作。
我使用此 RemoteAddrValve 使用工作站的特定内部 IP 地址,认为 IP 地址范围的格式可能是错误的。我还尝试使用特定的外部 IP 地址:
<Valve className="org.apache.catalina.valves.RemoteAddrValve"
allow="192\.168\.10\.109" />
在这种情况下,任何IP地址的访问都是被允许的。同样,“允许”条目似乎被忽略了。
将阀门添加到 /opt/tomcat/conf/content.xml 而不是 server.xml,如上述文章中的一些注释建议的那样。这失败了。
按照一些评论的建议将阀门添加到 /
我没有主意,希望这里有人能找出问题所在。
只需添加
allow=""
我只允许两个ip访问Tomcat服务器:
附加信息:
...
<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"
/>
...
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>
...
成功,Tomcat拒绝客户端127.0.0.1访问。
curl --interface 127.0.0.1 http://192.168.56.1:8080/
...Return Nothing...
成功,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>
成功,Tomcat拒绝客户端127.0.0.1访问。
curl --interface 127.0.0.1 http://192.168.56.1:8080/hello/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>
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>
curl http://192.168.56.1:8080/hello/index3.jsp
不返回任何内容。