【问题标题】:Spring Kafka client SSL setupSpring Kafka 客户端 SSL 设置
【发布时间】:2020-05-19 18:49:20
【问题描述】:

我的设置:

  • JDK 11.0.6
  • Spring Boot 2.2.4.RELEASE
  • spring-kafka 2.4.1

我已经在 PLAINTEXT 中验证了我的 Zookeeper / Kafka / 客户端应用程序,一切正常。我还使用 Kafka 客户端工具验证了我的密钥库/信任库。

我正在使用 KafkaAdmin bean 来配置我的主题,它似乎在 SSL 连接上失败了:

@Bean
public KafkaAdmin kafkaAdmin() {
    Map<String, Object> configs = new HashMap<>();
    configs.put(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG, getKafkaHost());
    configs.put("security.protocol", "SSL");
    configs.put("ssl.key.password", "xxx");
    configs.put("ssl.keystore.location", "/keystore/client.keystore.jks");
    configs.put("ssl.keystore.password", "xxx");
    configs.put("ssl.truststore.location", "/keystore/kafka.truststore.jks");
    configs.put("ssl.truststore.password", "xxx");
    return new KafkaAdmin(configs);
}

我的两个 JKS 文件位于我的项目的 src/main/resources/keystore 中。 /keystore 不起作用...如果我指定/src/main/resources/keystore/client.keystore.jks,它们似乎会被捡起...这在现实世界中会起作用吗?在独立的 tomcat 运行时,会有一个 /src/main/resources/keystore?

据我了解,路径是相对于 target/classes 文件夹的,不是吗?

【问题讨论】:

    标签: java spring spring-boot ssl apache-kafka


    【解决方案1】:

    即使您提供绝对路径,Kafka 客户端或 Spring 也无法解析直接随路径提供的 .jks 文件。提供像src/main/resources/keystore 这样的相对路径不是一个好主意,因为在构建之后,您的resources 目录内容将直接复制到target/classes,正如您已经知道的那样。

    因此,使用来自classpath 的值注释将文件直接绑定到org.springframework.core.io.Resource 类型实例。

    @Value("classpath:certs/keystore/kafka.keystore.jks")
    private Resource keystore;
    
    @Value("classpath:certs/truststore/kafka.truststore.jks")
    private Resource truststore;
    

    然后像这样从该实例获取绝对路径;

    configs.put("ssl.keystore.location", keystore.getFile().getAbsolutePath());
    configs.put("ssl.truststore.location", truststore.getFile().getAbsolutePath());
    

    如您所见,KeystoreTruststore 文件应该在 classpath 中。就我而言,它们分别位于src/resources/certs/keystoresrc/resources/certs/truststore 目录中。

    【讨论】:

      猜你喜欢
      • 2020-12-09
      • 2021-06-04
      • 2015-08-26
      • 2021-06-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-12
      • 2021-07-07
      相关资源
      最近更新 更多