spring kafka ssl 类路径信任库
spring kafka ssl classpath truststore
如何使用 spring kafka(无 sboot)进行 SSL 配置,例如 relative/classpath 指定信任库位置的路径?
仅在提供绝对路径时有效
令人惊讶的是,当使用 spring 引导运行时,它与相对路径(在 jar 内)一起工作..
文件由 Kafka 读取,而不是 Spring。
Kafka 不了解 Spring 的类路径资源抽象。
我认为你对启动有误;它只有在罐子爆炸时才有效;引导程序在 KafkaProperties
...
中有此代码
map.from(this::getTrustStoreLocation).as(this::resourceToPath)
.to(properties.in(SslConfigs.SSL_TRUSTSTORE_LOCATION_CONFIG));
...
private String resourceToPath(Resource resource) {
try {
return resource.getFile().getAbsolutePath();
}
catch (IOException ex) {
throw new IllegalStateException(
"Resource '" + resource + "' must be on a file system", ex);
}
}
要在 jar 中使用信任库,您需要先将其复制到文件系统(例如 /tmp
),然后再启动应用程序上下文。
我的补丁是在 spring 启动时将 key-store 复制到文件系统。 (与其拥有来自 KafkaAutoConfiguration 的 ConsumerFactory,不如拥有自己的 ConsumerFactory)
- 从您的属性中获取 key-store 的名称(因此 key-store 可以在您的阶段 DEV/INT/PROD 中命名)
- 将它复制到您的 k8n pod 的文件系统(如果您的 运行 在 Windows 本地,请使用不同于 '/tmp' 的目标)
- 使用文件系统中的新位置更新 KafkaProperties。
@Configuration
@RequiredArgsConstructor
public class KafkaConfig {
private final KafkaProperties kafkaProps;
@Bean
@ConditionalOnProperty(value = "spring.kafka.enabled", havingValue = "true")
public ConsumerFactory<?, ?> consumerFactory() throws IOException {
Path fileSystemKeyStorePath = Paths.get("/tmp", "keystore.pfx");
Resource keyStoreLocationFromProperties = kafkaProps.getSsl().getKeyStoreLocation();
Files.copy(keyStoreLocationFromProperties.getInputStream(),
fileSystemKeyStorePath, StandardCopyOption.REPLACE_EXISTING);
Ssl ssl = kafkaProps.getSsl();
ssl.setKeyStoreLocation(new FileSystemResource(fileSystemKeyStorePath));
return new DefaultKafkaConsumerFactory(kafkaProps.buildConsumerProperties());
}
}
如何使用 spring kafka(无 sboot)进行 SSL 配置,例如 relative/classpath 指定信任库位置的路径?
仅在提供绝对路径时有效
令人惊讶的是,当使用 spring 引导运行时,它与相对路径(在 jar 内)一起工作..
文件由 Kafka 读取,而不是 Spring。
Kafka 不了解 Spring 的类路径资源抽象。
我认为你对启动有误;它只有在罐子爆炸时才有效;引导程序在 KafkaProperties
...
map.from(this::getTrustStoreLocation).as(this::resourceToPath)
.to(properties.in(SslConfigs.SSL_TRUSTSTORE_LOCATION_CONFIG));
...
private String resourceToPath(Resource resource) {
try {
return resource.getFile().getAbsolutePath();
}
catch (IOException ex) {
throw new IllegalStateException(
"Resource '" + resource + "' must be on a file system", ex);
}
}
要在 jar 中使用信任库,您需要先将其复制到文件系统(例如 /tmp
),然后再启动应用程序上下文。
我的补丁是在 spring 启动时将 key-store 复制到文件系统。 (与其拥有来自 KafkaAutoConfiguration 的 ConsumerFactory,不如拥有自己的 ConsumerFactory)
- 从您的属性中获取 key-store 的名称(因此 key-store 可以在您的阶段 DEV/INT/PROD 中命名)
- 将它复制到您的 k8n pod 的文件系统(如果您的 运行 在 Windows 本地,请使用不同于 '/tmp' 的目标)
- 使用文件系统中的新位置更新 KafkaProperties。
@Configuration
@RequiredArgsConstructor
public class KafkaConfig {
private final KafkaProperties kafkaProps;
@Bean
@ConditionalOnProperty(value = "spring.kafka.enabled", havingValue = "true")
public ConsumerFactory<?, ?> consumerFactory() throws IOException {
Path fileSystemKeyStorePath = Paths.get("/tmp", "keystore.pfx");
Resource keyStoreLocationFromProperties = kafkaProps.getSsl().getKeyStoreLocation();
Files.copy(keyStoreLocationFromProperties.getInputStream(),
fileSystemKeyStorePath, StandardCopyOption.REPLACE_EXISTING);
Ssl ssl = kafkaProps.getSsl();
ssl.setKeyStoreLocation(new FileSystemResource(fileSystemKeyStorePath));
return new DefaultKafkaConsumerFactory(kafkaProps.buildConsumerProperties());
}
}