【问题标题】:Spring boot doesn't read Components after jar signingjar签名后Spring Boot不读取组件
【发布时间】:2018-07-27 19:28:45
【问题描述】:

我正在开发一个服务于 REST HTTP(S) 请求的 Spring Boot 应用程序。 (很常见)。

它按预期工作,但是在最终(和工作)jar 被签名(通过有效证书)之后,所有 URL 映射停止工作,只返回 404 到任何请求。 (注意嵌入式Tomcat服务器启动没有问题,我没有收到任何异常)

经过一些调试,我发现 Java 的默认 ClassLoader (Laucher$AppClassLoader) 在 jar 签名时不会返回我配置的包 (@ComponentScan) 中的类。

//org.springframework.core.io.support.PathMatchingResourcePatternResolver
//Param 'path' has my valid, existing and desired package with @Controller or @Component inside
protected Set<Resource> doFindAllClassPathResources(String path) throws IOException {
            Set<Resource> result = new LinkedHashSet<Resource>(16);
            ClassLoader cl = getClassLoader(); //sun.misc.Laucher$AppClassLoader
            Enumeration<URL> resourceUrls = (cl != null ? cl.getResources(path) : ClassLoader.getSystemResources(path));
            //Empty enumeration when jar is signed
            ...
}

我尝试使用自定义类加载器但没有成功;同样的问题。

由于当我使用自签名证书对 jar 进行签名时它可以工作,所以我认为其他人完成的签名过程可能存在问题。但我找不到任何证据。

好像一签完就无法列出包裹内容了……

如果我认为有用,我会尝试更多测试并在此处添加...

更新

在自定义类加载器的帮助下调试后,我发现:

((java.net.JarURLConnection)new java.net.URL("jar:file:/home/user/my-app-with-dependencies_signed.jar!/META-INF/MANIFEST.MF").openConnection()).getJarEntry(); 

好的。有效。

((java.net.JarURLConnection)new java.net.URL("jar:file:/home/user/my-app-with-dependencies_signed.jar!/META-INF/").openConnection()).getJarEntry();

没用! >.

Exception occurred in target VM: JAR entry META-INF/ not found in /home/user/my-app-with-dependencies_signed.jar 
java.io.FileNotFoundException: JAR entry META-INF/ not found in /home/user/my-app-with-dependencies_signed.jar
    at sun.net.www.protocol.jar.JarURLConnection.connect(JarURLConnection.java:142)
    at sun.net.www.protocol.jar.JarURLConnection.getJarEntry(JarURLConnection.java:94)
...

在尝试访问未签名或自签名的 jar 时,第二个示例同样有效。

这个打开jar的操作是Spring从@ComponentScan中给定的包中读取@Controller和@Component时执行的。

同样,Java 的类加载器不读取目录内容,只读取指定文件。

this.getClass().getClassLoader(); //sun.misc.Launcher$AppClassLoader@18b4aac2
this.getClass().getClassLoader().getResources("META-INF/MANIFEST.MF").hasMoreElements(); //always true
this.getClass().getClassLoader().getResources("META-INF/").hasMoreElements(); //false when signed

更新 2

我得到了关于签名的信息。负责签名和证书的人员实际上使用了一个 Windows 应用程序,该应用程序使用来自 Windows-MY 密钥库的证书和来自 USB 令牌的私钥对 jar 进行签名。

并不是说这肯定是原因,但我认为重要的是要注意jarsigner 没有被使用。

更新 3

我用一个简单的测试用例创建了一个 github 存储库: https://github.com/jesjobom/signed-jar-class-loader-test

【问题讨论】:

  • 阅读官方文档后,似乎签名过程涉及更新 MANIFEST.MF。 docs.oracle.com/javase/tutorial/deployment/jar/intro.html 在您的第二种情况下,似乎缺少 MANIFEST.MF。你检查过你签名的 jar 的内容吗?您也可以尝试 spring-boot-maven-plugin,它可能会正确地重新打包您的 jar。 docs.spring.io/spring-boot/docs/current/reference/html/…
  • 你是如何运行它的?它是一个 jar 文件,您使用 java -jar 运行它还是一个 servlet?你如何签署你的罐子?它是maven中的插件,还是外部的?
  • 我已经用jarsigner -verify验证了签名。所以一切都应该没问题,包括 MANIFEST.MF。但我确实检查了这个文件中的一些条目。签名不是我做的,所以我不知道这个过程(但我会调查一下)。我所知道的是这个签名是有效的。在我的测试中,我使用 java -jar 运行 jar。
  • 请你比较一下两个罐子里的文件吗?一个 JAR 有一个“INDEX.LIST”文件,而另一个没有?您能否上传您的两个 JAR 或简化的复制案例供我们检查?
  • 我在两个 jar 中都找不到任何“INDEX.LIST”或类似的东西,无论是签名的还是自签名的。但我注意到一些奇怪的事情......使用未签名或自签名的 jar,我可以使用 unzip -l my-app-self_signed.jar "*/" 列出目录。但是对签名版本执行相同操作不会列出任何内容。这没有意义,因为文件夹存在,它们里面有文件,但没有列出......我的意思是,存在一个文件夹“org/springframework/util/”,因为它里面有文件“FileCopyUtils.class”,但是从签名的 jar 中列出的文件夹不会单独从签名的 jar 中列出。

标签: java spring spring-boot classloader jarsigner


【解决方案1】:

我已经找到了解决方案,但问题仍然存在。

当加载我通过@ComponentScan 指向的类时,Spring 向 ClassLoader (Laucher$AppClassLoader) 询问我通知的每个包的java.net.URL。由于某些未知原因,我无法加载包/文件夹,因此我创建了一个自定义 ClassLoader,如果包是我的,它总是返回预期的 URL。

public class CustomClassLoader extends ClassLoader {

...

@Override
public Enumeration<URL> getResources(String name) throws IOException {
    if(name.startsWith("com/my/package/")) {
        readBasePath(); //obtains path to jar (e.g. "jar:file:/home/app.jar!/")
        List<URL> resources = new ArrayList<>();
        resources.add(new URL(basePath + name));
        return Collections.enumeration(resources);
    }
    return fallback.getResources(name); //default classloader
}
...
}

即便如此,后来,Spring 尝试从包中加载“.class”,但由于同样的原因而失败......所以,我创建了一个 PathMatchingResourcePatternResolver 的自定义实现,它将列出 jar 的所有内容(这是我可以做到的!)并只选择给定包中的那些。

public class CustomPathMatchingResourceLoader extends PathMatchingResourcePatternResolver {

@Override
protected Set<Resource> doFindPathMatchingJarResources(final Resource rootDirResource, URL rootDirURL, String subPattern) throws IOException {

    try {

        String searchBase = ...; //package within jar
        String pathBase = ...; //path to jar

        URLConnection conn = new URL(pathBase).openConnection();

        Set<Resource> resources = new HashSet();
        JarFile file = ((JarURLConnection) conn).getJarFile();

        Enumeration<JarEntry> entries = file.entries();
        while (entries.hasMoreElements()) {
            JarEntry entry = entries.nextElement();
            if (entry.getName().startsWith(searchBase) && !entry.getName().endsWith("/")) {
                resources.add(new UrlResource(pathBase + entry.getName()));
            }
        }

        return resources;

    } catch (Exception e) {
        e.printStackTrace();
    }

    return super.doFindPathMatchingJarResources(rootDirResource, rootDirURL, subPattern);
}
...
}

所以它在签名过程中不受任何干扰...我非常有信心使用jarsigner 签名可以解决问题,但我认为这会很困难...

无论如何,虽然它有效,但它不是一个解决方案。因此,我不会接受这个答案是正确的......

【讨论】:

    【解决方案2】:

    问题是签名的 jar 不包含条目 META-INF 和 com/jesjobom 明确。

    使用 7zip 您可以列出 zip 条目:例如7za l signed-jar-class-loader-test_signed.jar

    • 您的签名的罐子:

         Date      Time    Attr         Size   Compressed  Name
      ------------------- ----- ------------ ------------  ------------------------
      2018-02-27 15:27:08 .....         1907          999  com\jesjobom\Main.class
      2018-02-27 15:27:08 .....         2978          944  META-INF\maven\com.jesjobom\signed-jar-class-loader-test\pom.xml
      2018-02-27 15:27:08 .....          113          111  META-INF\maven\com.jesjobom\signed-jar-class-loader-test\pom.properties
      2018-02-27 15:27:08 .....          595          361  META-INF\MANIFEST.MF
      2018-02-27 15:27:08 .....          609          386  META-INF\BANCO_DO_BRASIL_SA.SF
      2018-02-27 15:27:08 .....         4520         3251  META-INF\BANCO_DO_BRASIL_SA.RSA
      ------------------- ----- ------------ ------------  ------------------------
      2018-02-27 15:27:08              10722         6052  6 files
      
    • 编译后有一些D(目录)条目在签名版本中丢失。

          Date      Time    Attr         Size   Compressed  Name
      ------------------- ----- ------------ ------------  ------------------------
      2018-02-27 15:27:08 D....            0            0  META-INF
      2018-02-27 15:27:08 D....            0            0  com
      2018-02-27 15:27:08 D....            0            0  com\jesjobom
      2018-02-27 15:27:08 D....            0            0  META-INF\maven
      2018-02-27 15:27:08 D....            0            0  META-INF\maven\com.jesjobom
      2018-02-27 15:27:08 D....            0            0  META-INF\maven\com.jesjobom\signed-jar-class-loader-test
      2018-02-27 15:27:08 .....         1907          999  com\jesjobom\Main.class
      2018-03-09 14:15:16 .....         3082          949  META-INF\maven\com.jesjobom\signed-jar-class-loader-test\pom.xml
      2018-02-27 15:27:08 .....          117          114  META-INF\maven\com.jesjobom\signed-jar-class-loader-test\pom.properties
      ------------------- ----- ------------ ------------  ------------------------
      2018-02-27 15:27:08               5324         2221  4 files, 6 folders
      

    我不知道为什么缺少文件夹条目。我怀疑是因为它们在 \META-INF\MANIFEST.MF 中没有 SHA 签名,所以它们已从签名的 JAR 中删除。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-06-18
      • 2021-03-05
      • 1970-01-01
      • 2019-05-31
      • 2015-04-16
      • 2017-06-04
      • 2017-10-07
      • 1970-01-01
      相关资源
      最近更新 更多