Jar文件无法找到密钥库 [英] Jar file not able to find the keystore

查看:139
本文介绍了Jar文件无法找到密钥库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行一个SpringKafka生产者.我已经在yml文件中配置了密钥库的位置,并且在Eclipse中运行时被提取了.但是当代码作为jar运行时,无法找到密钥库位置.如何解决此问题.

I am running a SpringKafka producer.I have configured the keystore location in the yml file and its being picked up when ran in eclipse. But when the code is run as a jar its unable to find the keystore location.How to resolve this.

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:类路径资源[es-cert.jks]无法解析为绝对文件路径,因为它不驻留在文件系统中:jar:file:/u01/home/app/user-docker /kafka2.jar!/BOOT-INF/classes!/es-cert.jks

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中.

But the es-cert.jks is present inside jar.

推荐答案

Kafka不了解文件系统上仅Spring资源文件.

Kafka doesn't understand Spring resources only files on the file system.

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

You can't use classpath: to reference the key store from a jar, since Spring Boot has to convert the resource to an absolute path to pass it to 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之前执行此操作.

One solution is to copy the keystore yourself from the jar resource to a file on the file system. Do this before loading the SpringApplication.

例如使用FileCopyUtils.

这篇关于Jar文件无法找到密钥库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆