【问题标题】:Restrict remote access to folder inside tomcat webapp folder限制对tomcat webapp文件夹内文件夹的远程访问
【发布时间】:2021-02-08 10:38:10
【问题描述】:

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

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

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

【问题讨论】:

    标签: spring-boot tomcat


    【解决方案1】:

    阀门必须在<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/&lt;engine_name&gt;/&lt;host_name&gt;/context.xml.default(通常是$CATALINA_BASE/conf/Catalina/localhost/context.xml.default)如果您希望更改仅应用于单个主机。

    每个 Web 应用程序上下文都将继承上述文件中的 &lt;Valve&gt;s。既然你想拥有一个没有这些限制的 Web 应用程序(比如说/pubApp),你需要创建一个文件$CATALINA_BASE/conf/&lt;engine_name&gt;/&lt;host_name&gt;/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>
    

    【讨论】:

    • 我更新了 $CATALINA_BASE/conf/context.xml 并添加了阀门来限制远程连接并且它起作用了。现在所有来自远程的请求都被阻止了。但是,当我在 /opt/tomcat/conf/Catalina/localhost 中创建文件 common-service.xml 并添加 。所有请求仍然被阻止。它没有被覆盖
    • common-service 是我希望远程访问的网关服务。你能检查我哪里做错了吗
    • 检查日志:当你启动 tomcat 时,你应该看到像 Deploying configuration descriptor [.../conf/Catalina/localhost/common-service.xml] 这样的行而不是 Deploying web application directory [.../webapps/common-service]。在您的评论中,您使用 &lt;/context&gt; 而不是 &lt;/Context&gt;,因此 XML 格式不正确。
    猜你喜欢
    • 2021-07-30
    • 1970-01-01
    • 1970-01-01
    • 2017-09-17
    • 2016-05-05
    • 1970-01-01
    • 2017-08-29
    • 1970-01-01
    相关资源
    最近更新 更多