简体   繁体   English

Spring Boot中的Kafka配置类找不到密钥库或信任库

[英]Kafka Configuration class in Spring Boot not finding keystore or truststore

I'm setting up a Kafka consumer configuration and the configuration cannot find the keystore or truststore on the classpath: 我正在设置Kafka使用者配置,但该配置在类路径上找不到密钥库或信任库:

@EnableKafka
@Configuration
public class KafkaConfig {

    @Value("${kafka.ssl.keystore}")
    private String keyStorePath;
    @Value("${kafka.ssl.truststore}")
    private String trustStorePath;

    @Bean
    public ConsumerFactory<String, String> getConsumerFactory() {

        Map<String, Object> properties = new HashMap<>();
        properties.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG,"my-bootstrap.mydomain.com:443");
        properties.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
        properties.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
        properties.put(ConsumerConfig.GROUP_ID_CONFIG, "group1");
        properties.put(ConsumerConfig.CLIENT_ID_CONFIG, "client1");
        properties.put("enable.auto.commit", "true");
        properties.put("auto.commit.interval.ms", "500");
        properties.put("session.timeout.ms", "30000");
        properties.put(CommonClientConfigs.SECURITY_PROTOCOL_CONFIG, "SSL");
        properties.put(SslConfigs.SSL_KEYSTORE_LOCATION_CONFIG, keyStorePath);
        properties.put(SslConfigs.SSL_KEYSTORE_PASSWORD_CONFIG, "password");
        properties.put(SslConfigs.SSL_TRUSTSTORE_LOCATION_CONFIG, trustStorePath);
        properties.put(SslConfigs.SSL_TRUSTSTORE_PASSWORD_CONFIG, "password");
        properties.put(SslConfigs.SSL_KEY_PASSWORD_CONFIG, "password");

        return new DefaultKafkaConsumerFactory<>(properties);
    }

    @Bean
    public ConcurrentKafkaListenerContainerFactory<String, String> kafkaListenerContainerFactory() {
        ConcurrentKafkaListenerContainerFactory<String, String> factory
                = new ConcurrentKafkaListenerContainerFactory<>();
        factory.setConsumerFactory(getConsumerFactory());
        return factory;
    }
}

The keystore and truststore are both located in the directory src/main/resources/ssl in the same maven module as the configuration class. 密钥库和信任库都位于与配置类相同的maven模块中的目录src/main/resources/ssl中。

I set up the placeholders in the application.yml as follows: 我在application.yml中设置占位符,如下所示:

kafka:
  ssl:
    keystore: classpath:ssl/kafka-keystore.jks
    truststore: classpath:ssl/kafka-truststore.jks

However, the application fails to start with the following exception: 但是,应用程序无法启动,但出现以下异常:

"org.apache.kafka.common.KafkaException: java.io.FileNotFoundException: classpath:ssl/kafka-keystore.jks (No such file or directory)"

My understanding is that using @Value enables the use of the classpath: prefix to resolve the classpath (see this link) https://www.baeldung.com/spring-classpath-file-access 我的理解是,使用@Value可以使用classpath:前缀来解析类路径(请参阅此链接) https://www.baeldung.com/spring-classpath-file-access

Moreover, the @Value technique works just fine to resolve the keystores and truststores for the reactive WebClient configuration in the same application. 此外, @Value技术可以很好地解决同一应用程序中反应式WebClient配置的密钥库和信任库。

What do I need to do to resolve the classpath for the Kafka configuration? 我需要怎么做才能解决Kafka配置的类路径? Am I missing something here? 我在这里想念什么吗?

Your injecting into a String which is going to keep the "classpath:" within the String value and provide it as a property to DefaultKafkaConsumerFactory, try injecting into a spring Resource like: 您注入一个String,它将“ classpath:”保留在String值内,并将其作为属性提供给DefaultKafkaConsumerFactory,请尝试注入spring资源,例如:

import org.springframework.core.io.Resource;

@Value("classpath:path/to/file/in/classpath")
Resource resourceFile;

Then you can access the file and you could get the absolute path like: 然后,您可以访问文件,并且可以获取绝对路径,例如:

resourceFile.getFile().getAbsolutePath()

The idea being you could provide the absolute path to DefaultKafkaConsumerFactory 这个想法是您可以提供DefaultKafkaConsumerFactory的绝对路径

But you could also try removing the "classpath:" and inject as String like your current code which might work depending on how DefaultKafkaConsumerFactory treats that property. 但是您也可以尝试删除“ classpath:”,并像当前代码一样将其作为String注入,这可能取决于DefaultKafkaConsumerFactory处理该属性的方式。 But I can't see why absolute path above wouldn't work. 但是我看不到为什么上面的绝对路径不起作用。

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

相关问题 Spring 开机 | 使用动态密钥库/信任库 - Spring Boot | Use dynamic keystore/truststore 如何为相互SSL覆盖Spring Boot密钥库/信任库 - How to override Spring boot keystore/truststore for mutual SSL 在Java中配置Keystore和TrustStore时出错 - Error in configuration of Keystore , TrustStore in java AWS Kafka (MSK) - 如何生成密钥库和信任库并在我的 Spring Cloud Stream 应用程序中使用它们? - AWS Kafka (MSK) - How to generate Keystore and truststore and use the same in my Spring Cloud Stream application? spring kafka ssl 类路径信任库 - spring kafka ssl classpath truststore 带有 Spring Boot 的 Heroku Apache Kafka 配置 - Heroku Apache Kafka Configuration with Spring Boot 如何将信任库和密钥库密码安全地应用于JConsole配置 - How to securely apply truststore and keystore password to JConsole configuration Spring Boot Kafka类反序列化-不在受信任的程序包中 - Spring boot Kafka class deserialization - not in the trusted package spring 引导应用程序无法检测信任库 - spring boot app unable to detect truststore Spring 引导容器在 Azure 应用服务中具有信任库 - Spring boot container With truststore in Azure App Service
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM