限制远程访问 tomcat webapp 文件夹内的文件夹

Restrict remote access to folder inside tomcat webapp folder

我在 Tomcat 9 上托管了 Spring 基于引导微服务的应用程序。因此,我在 {{tomcat 根目录}}/webapps 文件夹中部署了多个文件夹。我只想从外部网络(网关服务)访问一个文件夹。其他文件夹应该只能从本地主机访问。

那么,如何在不添加 context.xml 的情况下实现这一点?我知道这可以通过添加 context.xml 来实现,但是 context.xml 在每次 war 部署后都会被覆盖。

所以想检查一下这是否可以在更全局的层面上实现,比如修改 server.xml。

Valves 必须在 <Context> 部分中定义,但向您的应用程序添加 META-INF/context.xml 并不是 define a context.

的唯一方法

如果你想限制大部分应用到localhost,最简单的方法是修改$CATALINA_BASE/conf/context.xml:

<Context>
    ...
    <Valve className="org.apache.catalina.valves.RemoteAddrValve"
           allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1"" />
</Context>

$CATALINA_BASE/conf/<engine_name>/<host_name>/context.xml.default(通常是$CATALINA_BASE/conf/Catalina/localhost/context.xml.default)如果您希望更改仅应用于单个主机。

每个 Web 应用程序上下文都将继承上述文件中的 <Valve>。由于您想拥有一个没有这些限制的 Web 应用程序(假设 /pubApp),您需要创建一个文件 $CATALINA_BASE/conf/<engine_name>/<host_name>/pubApp.xml 并将 override 属性设置为 true:

<Context override="true">
    <!-- Remember to copy all configuration values from `conf/context.xml`
         and `conf/<engine_name>/<host_name>/context.xml.default`
         that you wish to apply, since they will not be read anymore.
     -->
    ...
</Context>