项目中用到一个过滤器,因为和tomcat静态资源路径配置上的冲突,导致过滤器失效。
相关代码如下:
server.xml
<!-- 静态资源路径配置 -->
<Context crossContext="true" docBase="/var/docs/pdf/" path="/pdf" reloadable="true"/>
通过此配置,可以由 localhost:8080/pdf/aaa.pdf
访问到静态资源。
web.xml:
<filter>
<filter-name>PDFFilter</filter-name>
<filter-class>com.plt.filter.PDFFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>PDFFilter</filter-name>
<url-pattern>*.pdf</url-pattern>
</filter-mapping>
通过此配置,所有 .pdf
的静态资源请求都会先进入过滤器。
当web.xml加入过滤器配置后,由 localhost:8080/pdf/aaa.pdf
发生的访问静态资源请求不再经过过滤器;
得从 localhost:8080/aaa.pdf
或者 localhost:8080/pdfx/aaa.pdf
进入才能匹配到过滤器(即/pdf这一路由输入不正确),可是这样又无法访问到静态资源;
将过滤器匹配规则改为 /pdf/*
的话,无论如何也都无法进入过滤器;
希望能实现形似 /pdf/*.pdf
的匹配逻辑,可是该逻辑不被规则允许。
此问题如何解决?
web.xml再配置下面看看