繁体   English   中英

Jar 文件找不到密钥库

[英]Jar file not able to find the keystore

我正在运行一个 SpringKafka 生产者。我已经在 yml 文件中配置了密钥库位置,并且在 eclipse 中运行时将其拾取。 但是当代码作为 jar 运行时,它无法找到密钥库位置。如何解决这个问题。

spring:
  kafka:
    producer:
      ssl:
        key-password:
        key-store-location: classpath:es-cert.jks
        key-store-password: password
        key-store-type: jks

这是我的 yml 文件。

我收到以下错误:

java.io.FileNotFoundException: class path resource [es-cert.jks] cannot be resolved to absolute file path because it does not reside in the file system: jar:file:/u01/home/app/user-docker/kafka2. jar./BOOT-INF/classes!/es-cert.jks

但是 es-cert.jks 存在于 jar 中。

Kafka 不理解 Spring 资源仅文件系统上的文件。

您不能使用classpath:从 jar 引用密钥库,因为 Spring 引导必须将资源转换为绝对路径才能将其传递给 Kafka。

    private String resourceToPath(Resource resource) {
        try {
            return resource.getFile().getAbsolutePath();
        }
        catch (IOException ex) {
            throw new IllegalStateException("Resource '" + resource + "' must be on a file system", ex);
        }

一种解决方案是自己将密钥库从 jar 资源复制到文件系统上的文件中。 在加载SpringApplication之前执行此操作。

例如使用FileCopyUtils

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM