![](/img/trans.png)
[英]Spring Kafka SSL setup in Spring boot application.yml
[英]Spring Kafka client SSL setup
我的设置:
我已经在 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());
如您所见, Keystore
和Truststore
文件应该在classpath
中。 就我而言,它们分别位于src/resources/certs/keystore
和src/resources/certs/truststore
目录中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.