簡體   English   中英

如何在 Spring Kafka 配置中設置 ssl 文件的位置?

[英]How to set up ssl file's location in Spring Kafka configuration?

我正在嘗試在 Spring 客戶端和 Kafka 之間建立 ssl(mTLS) 連接。 我得到了兩個文件:

  -app.keystore;
  -client.truststore.

當我使用/usr/bin/keytool -list -rfc -keystore app.keystore |grep "Keystore type"檢查文件時,我發現類型是 PKCS12。

我手動配置 Kafka,所以我有一個帶有 Kafka 屬性的配置 class。 在配置中,我將屬性“ssl.keystore.type”和“ssl.truststore.typ”設置為“PKCS12”。

文件位置在 application.properties 中設置,並使用 @Value 注釋粘貼到配置中(請參閱下面的配置代碼):

kafka.trust-store-location=classpath:ssl/client.truststore
kafka.key-store-location=classpath:ssl/app.keystore

物理文件放在文件夾中:maven_module_name/src/main/resources/ssl/client.truststore maven_module_name/src/main/resources/ssl/app.keystore

當我啟動應用程序時出現錯誤:

Caused by: org.apache.kafka.common.KafkaException: Failed to load SSL keystore classpath:ssl/app.keystore of type PKCS12
Caused by: java.nio.file.NoSuchFileException: classpath:ssl/app.keystore

這是我的配置 class:

@Configuration
public class KafkaProducerConfiguration {

...

    @Value("${kafka.key-store-location}")
    private String keyStoreLocation;

    @Value("${kafka.truststore-location}")
    private String trustStoreLocation;

    @Bean
    public Map<String, Object> producerConfigs() {
        Map<String, Object> props = new HashMap<>();
        ...
        ...
        props.put("security.protocol", "SSL");
        props.put("ssl.key.password", "password");
        props.put("ssl.keystore.location", keyStoreLocation);
        props.put("ssl.keystore.password", "password");
        props.put("ssl.keystore.type", "PKCS12");
        props.put("ssl.truststore.location", trustStoreLocation);
        props.put("ssl.truststore.password", "password");
        props.put("ssl.truststore.type", "PKCS12");
        return props;
    }

    @Bean
    public <T> ProducerFactory<Long, T> someProducerFactory() {
        return new DefaultKafkaProducerFactory<>(producerConfigs());
    }

    @Bean
    public <T> KafkaTemplate<Long, T> someKafkaTemplate() {
        ...
        return template;
    }


}

我還嘗試按照此答案中的建議使用 Resourse 接口:

    @Value("${kafka.key-store-location}")
    private Resource keyStoreLocation;

    @Value("${kafka.trust-store-location}")
    private Resource trustStoreLocation;

當我嘗試以這種方式獲得絕對路徑時:

props.put("ssl.keystore.location", keyStoreLocation.getFile().getAbsolutePath());

該應用程序拋出異常:

java.io.FileNotFoundException: class path resource [ssl/app.keystore] cannot be resolved to absolute file path because it does not reside in the file system: jar:file:/app.jar!/BOOT-INF/classes!/ssl/app.keystore

請向我建議如何解決問題。 謝謝。

看起來這是 Kafka 客戶端中的一個已知問題

它不能從類路徑加載文件,只能從文件系統加載文件。 因此,當您構建應用程序時,src/main/resources 下的所有內容都打包在 jar 文件中,無法直接從磁盤讀取。

Spring Boot 建議解決方法如下:

FileCopyUtils.copy(new ClassPathResource("client.ks").getInputStream(),
            new FileOutputStream("/tmp/client.ks"));

您的配置文件可能如下所示:

ssl.keystore.location=/tmp/client.ks

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM