我的设置:
- JDK 11.0.6
- Spring Boot 2.2.4.RELEASE
- spring-kafka 2.4.1
我已经在明文中验证了我的 Zookeeper/Kafka/客户端应用程序,一切正常。我还使用 Kafka 客户端工具验证了我的 keystore /信任库。
我正在使用 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
正如你已经知道的。
因此,将文件直接绑定(bind)到 org.springframework.core.io.Resource
使用 classpath
中的值注释键入实例.
@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
分别目录。
关于java - Spring Kafka 客户端 SSL 设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60048971/