如何禁用Tomcat JARScanner

问题描述 投票:0回答:4

如何:禁用 Tomcat JARScanner?
原因: 停止 Tomcat 扫描 LIB 文件夹中的每个 .jar。

根据 documentation 它说可以在 context.xml 中禁用它。但它似乎不起作用。 (可能是我遗漏了一些东西) 我在论坛中进行了详尽的搜索,但找不到解决方案。

这是在 context.xml 中(尚未工作):

<JarScanner scanClassPath="false" scanAllFiles="false" scanAllDirectories="false"></JarScanner>

提前致谢。

java tomcat tomcat7 tomcat8
4个回答
14
投票

您应该将 JarScanner 元素添加为 context.xml 文件中根 Context 元素的子元素。

我在 war 文件中有这种 META-INF/context.xml 文件用于禁用 JarScanner:

<?xml version="1.0" encoding="UTF-8"?>
<Context>
    <JarScanner scanClassPath="false" scanAllFiles="false" scanAllDirectories="false"/>
</Context>

3
投票

您可以通过打开以下位置的文件来全局禁用用户定义模式的 JarScanner

%TOMCAT_HOME%/conf/catalina.properties

并将文件名模式添加到

tomcat.util.scan.StandardJarScanFilter.jarsToSkip
列表中。 例如,如果您想完全禁用 jar 扫描,您可以添加:

tomcat.util.scan.StandardJarScanFilter.jarsToSkip=\
*.jar,\

注意:如果您使用 JSTL,这当然可能会导致问题,因为扫描仪无法找到模板


1
投票

在你的java应用程序中添加这个:

@Bean
public TomcatServletWebServerFactory tomcatServletFactory() {
    return new TomcatServletWebServerFactory() {
        @Override
        protected void postProcessContext(final Context context) {
            ((StandardJarScanner) context.getJarScanner()).setScanManifest(false);
        }
    };
}

1
投票

这就是我为 Spring Boot 所做的。

基本上,将新的忽略的 jar 文件附加到要忽略的现有 jar 列表中。这样,您就不会完全禁用扫描仪,从而影响谁知道其他什么。

@Configuration
public class Config {

    @Bean
    public ServletWebServerFactory servletContainer() {
        return new TomcatServletWebServerFactory() {
            @Override
            protected void postProcessContext(Context context) {
                // db2 puts a ref to pdq.jar in the manifest, and tomcat then tries to find it, but it doesn't exist.
                // The jar isn't needed, so we just disable looking for it. You could also remove it from the manifest,
                // but that prob needs to be done after the build process.
                JarScanFilter jarScanFilter = context.getJarScanner().getJarScanFilter();
                if (jarScanFilter instanceof StandardJarScanFilter) {
                    StandardJarScanFilter filter = (StandardJarScanFilter) jarScanFilter;
                    String oldTldSkip = filter.getTldSkip();
                    String newTldSkip = oldTldSkip == null || oldTldSkip.trim().isEmpty() ? "pdq.jar" : oldTldSkip + ",pdq.jar";
                    filter.setTldSkip(newTldSkip);
                } else {
                    logger.warn("Unable to disable the tomcat jar scanner for pdq.jar. You may see a FileNotFound exception complaining of not finding a db2 pdq.jar file. You can probably ignore the error. Ref: https://stackoverflow.com/questions/11656596/how-to-disable-tomcat-jarscanner");
                }
            }
        };
    }

}
© www.soinside.com 2019 - 2024. All rights reserved.