[英]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.