繁体   English   中英

Spring Kafka 客户端 SSL 设置

[英]Spring Kafka client SSL setup

我的设置:

  • JDK 11.0.6
  • Spring 启动 2.2.4.RELEASE
  • 春天卡夫卡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文件夹,不是吗?

即使您提供绝对路径,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目录中。

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM