跳到主要内容

安全配置

DeepSeek V3 中英对照 Security Configuration

安全配置

Apache Kafka 支持客户端与代理之间的安全连接。要利用这一特性,请遵循 Apache Kafka 文档 中的指南以及 Kafka 0.9 的 Confluent 文档安全指南。使用 spring.cloud.stream.kafka.binder.configuration 选项为绑定器创建的所有客户端设置安全属性。

例如,要将 security.protocol 设置为 SASL_SSL,请设置以下属性:

spring.cloud.stream.kafka.binder.configuration.security.protocol=SASL_SSL
none

所有其他安全属性都可以以类似的方式进行设置。

在使用 Kerberos 时,请按照 参考文档 中的说明创建和引用 JAAS 配置。

Spring Cloud Stream 支持通过使用 JAAS 配置文件和使用 Spring Boot 属性将 JAAS 配置信息传递给应用程序。

使用 JAAS 配置文件

可以通过使用系统属性为 Spring Cloud Stream 应用程序设置 JAAS 和(可选的)krb5 文件位置。以下示例展示了如何使用 JAAS 配置文件启动带有 SASL 和 Kerberos 的 Spring Cloud Stream 应用程序:

java -Djava.security.auth.login.config=/path.to/kafka_client_jaas.conf -jar log.jar \
--spring.cloud.stream.kafka.binder.brokers=secure.server:9092 \
--spring.cloud.stream.bindings.input.destination=stream.ticktock \
--spring.cloud.stream.kafka.binder.configuration.security.protocol=SASL_PLAINTEXT
bash

使用 Spring Boot 属性

作为 JAAS 配置文件的替代方案,Spring Cloud Stream 提供了一种机制,通过使用 Spring Boot 属性来为 Spring Cloud Stream 应用程序设置 JAAS 配置。

以下属性可用于配置 Kafka 客户端的登录上下文:

spring.cloud.stream.kafka.binder.jaas.loginModule

登录模块名称。在正常情况下不需要设置。

默认值:com.sun.security.auth.module.Krb5LoginModule

spring.cloud.stream.kafka.binder.jaas.controlFlag

登录模块的控制标志。

默认值:required

spring.cloud.stream.kafka.binder.jaas.options

包含登录模块选项的键/值对的映射。

默认值:空映射。

以下示例展示了如何使用 Spring Boot 配置属性启动带有 SASL 和 Kerberos 的 Spring Cloud Stream 应用程序:

java --spring.cloud.stream.kafka.binder.brokers=secure.server:9092 \
--spring.cloud.stream.bindings.input.destination=stream.ticktock \
--spring.cloud.stream.kafka.binder.autoCreateTopics=false \
--spring.cloud.stream.kafka.binder.configuration.security.protocol=SASL_PLAINTEXT \
--spring.cloud.stream.kafka.binder.jaas.options.useKeyTab=true \
--spring.cloud.stream.kafka.binder.jaas.options.storeKey=true \
--spring.cloud.stream.kafka.binder.jaas.options.keyTab=/etc/security/keytabs/kafka_client.keytab \
--spring.cloud.stream.kafka.binder.jaas.options.principal=kafka-client-1@EXAMPLE.COM
bash

前面的示例相当于以下 JAAS 文件:

KafkaClient {
com.sun.security.auth.module.Krb5LoginModule required
useKeyTab=true
storeKey=true
keyTab="/etc/security/keytabs/kafka_client.keytab"
principal="kafka-client-1@EXAMPLE.COM";
};
none

如果所需的主题已经存在于代理上或将由管理员创建,可以关闭自动创建功能,只需发送客户端的 JAAS 属性即可。

备注

不要在同一个应用程序中混合使用 JAAS 配置文件 和 Spring Boot 属性。如果 -Djava.security.auth.login.config 系统属性已经存在,Spring Cloud Stream 将忽略 Spring Boot 属性。

备注

在使用 Kerberos 时,请谨慎使用 autoCreateTopicsautoAddPartitions。通常情况下,应用程序可能使用的 Kafka 和 Zookeeper 主体没有管理权限。因此,依赖 Spring Cloud Stream 来创建/修改主题可能会失败。在安全环境中,我们强烈建议使用 Kafka 工具来创建主题并管理 ACL。

多绑定器配置与 JAAS

当连接到多个集群时,每个集群都需要单独的 JAAS 配置,此时可以使用属性 sasl.jaas.config 来设置 JAAS 配置。当应用程序中存在此属性时,它将优先于上述其他策略。有关更多详细信息,请参阅 KIP-85

例如,如果你的应用程序中有两个集群,并且它们有独立的 JAAS 配置,那么你可以使用以下模板:

spring.cloud.stream:
binders:
kafka1:
type: kafka
environment:
spring:
cloud:
stream:
kafka:
binder:
brokers: localhost:9092
configuration.sasl.jaas.config: "org.apache.kafka.common.security.plain.PlainLoginModule required username=\"admin\" password=\"admin-secret\";"
kafka2:
type: kafka
environment:
spring:
cloud:
stream:
kafka:
binder:
brokers: localhost:9093
configuration.sasl.jaas.config: "org.apache.kafka.common.security.plain.PlainLoginModule required username=\"user1\" password=\"user1-secret\";"
kafka.binder:
configuration:
security.protocol: SASL_PLAINTEXT
sasl.mechanism: PLAIN
none

请注意,在上述配置中,两个 Kafka 集群以及它们的 sasl.jaas.config 值都是不同的。

请参阅此示例应用程序,以获取有关如何设置和运行此类应用程序的更多详细信息。