访问URL时,阻止Tomcat 7上的IP地址范围访问

时间:2017-01-18 10:54:01

标签: java tomcat

如何在tomcat / application上配置web.xml文件,以便实现以下目标:

允许

  • 本地主机/应用/富/酒吧/ *

不允许:

  • 本地主机/应用/富

有没有办法在不更改源代码的情况下执行此操作?更改web.xml文件是可以的。

我已阅读http://www.jvmhost.com/articles/block-ip-address-apache-tomcat-filter但仅此一项并未解决问题。

我尝试过以下方法:

<filter>
    <filter-name>Remote IP Filter</filter-name>
    <filter-class>org.apache.catalina.filters.RemoteAddrFilter</filter-class>
    <init-param>
        <param-name>deny</param-name>
        <param-value>^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$</param-value>
    </init-param>

    <init-param>
        <param-name>denyStatus</param-name>
        <param-value>403</param-value>
    </init-param>
</filter>
<filter>
    <filter-name>Allow quickview</filter-name>
    <filter-class>org.apache.catalina.filters.RemoteAddrFilter</filter-class>
    <init-param>
        <param-name>allow</param-name>
        <param-value>^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$</param-value>
    </init-param>

    <init-param>
        <param-name>denyStatus</param-name>
        <param-value>403</param-value>
    </init-param>
</filter>

<filter-mapping>
    <filter-name>Allow quickview</filter-name>
    <url-pattern>/foo/*</url-pattern>
</filter-mapping>
<filter-mapping>
    <filter-name>Remote IP Filter</filter-name>
    <url-pattern>/</url-pattern>
</filter-mapping>

谢谢!

2 个答案:

答案 0 :(得分:0)

您的<url-pattern>与问题中陈述的要求不符。你说你想禁止/app/foo/*但允许/app/foo。你想要这个,然后:

<filter-mapping>
    <filter-name>Allow quickview</filter-name>
    <url-pattern>/foo/*</url-pattern>
</filter-mapping>
<filter-mapping>
    <filter-name>Remote IP Filter</filter-name>
    <url-pattern>/foo</url-pattern>
</filter-mapping>

答案 1 :(得分:0)

最后,我通过以下方式解决了这个问题:

<filter-mapping>
    <filter-name>Allow quickview</filter-name>
    <url-pattern>/foo/*</url-pattern>
</filter-mapping>

<!-- disallowed, will throw 403 -->
<filter-mapping>
    <filter-name>Remote IP Filter</filter-name>
    <url-pattern>/foo/index.jsp</url-pattern>
</filter-mapping>

关键是将index.jsp添加为&#34; /&#34;单独不起作用。