通过IP限制Tomcat Webapp外部访问

我成功地能够使用我的服务器的外部IP访问我的Web应用程序。 不过,我现在想限制通过IP访问服务器。

我在conf / server.xml中添加了以下内容:

<Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true"> <!-- SingleSignOn valve, share authentication between web applications Documentation at: /docs/config/valve.html --> <!-- <Valve className="org.apache.catalina.authenticator.SingleSignOn" /> --> <!-- Access log processes all example. Documentation at: /docs/config/valve.html Note: The pattern used is equivalent to using pattern="common" --> <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" resolveHosts="true"/> <Valve className="org.apache.catalina.valves.RemoteAddrValve" allow="127\\.0\\.0\\.1|192\\.*\\.*\\.*|0\\.*\\.*\\.*|<my external machine ip>" deny=""/> 

但是,我仍然无法访问从我的Web应用程序。 如果我删除了RemoveAddrValue块,那么我可以从外部机器访问webapp。

我正在使用apache-tomcat-7.0.47

我如何限制通过IP的外部访问。 这是正确的方法吗?

我会用不同的方法。 要么使用防火墙(这是最好的方法,除非您需要Web服务器可访问,但应用服务器不可用),或使用Apache自己的控制允许和拒绝。

Envite是对的,防火墙方法更简单:

 iptables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT iptables -A INPUT -s $SOME_IP1 --dport 8080 -j ACCEPT iptables -A INPUT -s $SOME_IP2 --dport 8080 -j ACCEPT iptables -A INPUT --dport 8080 -j DROP 

很有可能您没有正确configuration您的防火墙,或者在您插入的防火墙之前有另一个规则允许到该端口/主机的stream量

你有没有检查过Tomcat日志? 这应该是您查找解决这些问题的第一个地方。 该文件通常称为catalina.out。 重现问题并用相应的日志条目更新您的问题。

但是,有一些事情是突出的:

尝试用单个\replace双\\ \ 。 例如,在引擎,主机或上下文容器中将允许从127.0.0.111.22.33.44 (从远程地址filter )进行访问:

  <Valve className="org.apache.catalina.valves.RemoteAddrValve" allow="127\.\d+\.\d+\.\d+|11\.22\.33\.44"/> 

你也可以尝试删除deny=""