Java Spring Kafka客户端SSL设置

Java Spring Kafka客户端SSL设置,java,spring,spring-boot,ssl,apache-kafka,Java,Spring,Spring Boot,Ssl,Apache Kafka,我的设置: JDK 11.0.6 弹簧防尘套2.2.4.1释放 春季卡夫卡2.4.1 我已经用明文验证了Zookeeper/Kafka/client应用程序,一切正常。我还使用Kafka客户端工具验证了我的密钥库/信任存储 我正在使用KafkaAdmin bean配置我的主题,它似乎在SSL连接上失败: @Bean public KafkaAdmin kafkaAdmin() { Map<String, Object> configs = new HashMap<&

我的设置:

  • JDK 11.0.6
  • 弹簧防尘套2.2.4.1释放
  • 春季卡夫卡2.4.1
我已经用明文验证了Zookeeper/Kafka/client应用程序,一切正常。我还使用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);
}
@Bean
公共卡夫卡德明卡夫卡德明(){
Map 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”);
返回新的卡夫卡管理员(配置);
}
我的两个JKS文件位于项目的src/main/resources/keystore中<代码>/keystore不起作用。。。如果我指定
/src/main/resources/keystore/client.keystore.jks
,它们似乎被选中了。。。这在现实世界中有用吗?在独立的tomcat中运行时,将有一个
/src/main/resources/keystore


据我所知,路径是相对于
目标/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
目录中