Tomcat如何限制可以访问的文件类型

在网上找了只知道如何限制某个目录下的所有文件都不能访问
如下:限制了webapps/testFile目录下的所有文件都不能访问
问题1:
我想知道只限制testFile目录下的.txt类型的文件怎么做?
问题2:
或者我想只允许testFile目录下的png文件可以访问,要怎么做?

<security-constraint>
          <display-name>Tomcat Server Configuration Security Constraint</display-name>
        <web-resource-collection>
            <web-resource-name>testFile</web-resource-name>
            <url-pattern>/testFile/*</url-pattern>
        </web-resource-collection>
        <auth-constraint>
        </auth-constraint>
 </security-constraint>

该回答引用chatgpt:
要限制testFile目录下的.txt类型的文件,你可以在 标签内添加一个 标签来指定要限制的文件类型。以下是修改后的示例代码:


<security-constraint>
    <display-name>Tomcat Server Configuration Security Constraint</display-name>
    <web-resource-collection>
        <web-resource-name>testFile</web-resource-name>
        <url-pattern>/testFile/*</url-pattern>
        <http-method>GET</http-method> <!-- 可选,只限制特定的HTTP方法 -->
        <mime-type>text/plain</mime-type> <!-- 只限制.txt类型的文件 -->
    </web-resource-collection>
    <auth-constraint>
    </auth-constraint>
</security-constraint>

<security-constraint>
    <display-name>Tomcat Server Configuration Security Constraint</display-name>
    <web-resource-collection>
        <web-resource-name>testFile</web-resource-name>
        <url-pattern>/testFile/*</url-pattern>
        <http-method>GET</http-method> <!-- 可选,只限制特定的HTTP方法 -->
        <mime-type>image/png</mime-type> <!-- 只允许.png类型的文件 -->
    </web-resource-collection>
    <auth-constraint>
    </auth-constraint>
</security-constraint>


参考GPT和自己的思路:对于问题1,可以在security-constraint标签内添加以下内容:

<web-resource-collection>
    <web-resource-name>testFile txt files</web-resource-name>
    <url-pattern>/testFile/*.txt</url-pattern>
</web-resource-collection>
<http-method>GET</http-method>
<http-method>POST</http-method>

这段代码可以限制只有testFile目录下的txt类型的文件不能被访问。

对于问题2,可以在security-constraint标签内添加以下内容:

<web-resource-collection>
    <web-resource-name>testFile png files</web-resource-name>
    <url-pattern>/testFile/*.png</url-pattern>
</web-resource-collection>
<http-method>GET</http-method>
<http-method>POST</http-method>
<http-method>HEAD</http-method>

这段代码可以限制只有testFile目录下的png类型的文件可以被访问。