对于最新稳定版本,请使用spring-cloud-stream 5.0.1spring-doc.cadn.net.cn

安全配置

Apache Kafka 支持客户端和代理之间的安全连接。要利用此功能,请遵循 Apache Kafka 文档 中的指南以及 Kafka 0.9 Confluent 文档中的安全性指南。使用 spring.cloud.stream.kafka.binder.configuration 选项为绑定程序创建的所有客户端设置安全属性。spring-doc.cadn.net.cn

例如,要将security.protocol设置为SASL_SSL,请设置以下属性:spring-doc.cadn.net.cn

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

其他所有安全性属性可以采用类似的方式设置。spring-doc.cadn.net.cn

使用 Kerberos 时,请按照参考文档中的说明创建和引用 JAAS 配置。参考文档spring-doc.cadn.net.cn

Spring Cloud Stream 支持通过使用 JAAS 配置文件和使用 Spring Boot 属性来向应用程序传递 JAAS 配置信息。spring-doc.cadn.net.cn

使用 JAAS 配置文件

可以通过使用系统属性为 Spring Cloud Stream 应用程序设置 JAAS 和(可选地)krb5 文件位置。<br/>以下示例显示了如何通过使用 JAAS 配置文件来启动具有 SASL 和 Kerberos 的 Spring Cloud Stream 应用程序:<br/>spring-doc.cadn.net.cn

 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

使用 Spring Boot 属性

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

可以使用以下属性来配置Kafka客户端的登录上下文:spring-doc.cadn.net.cn

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

登录模块名称。在正常情况下无需设置。spring-doc.cadn.net.cn

默认值: com.sun.security.auth.module.Krb5LoginModulespring-doc.cadn.net.cn

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

登录模块的控制标志。spring-doc.cadn.net.cn

默认值: requiredspring-doc.cadn.net.cn

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

包含登录模块选项的键/值对映射。spring-doc.cadn.net.cn

默认值:空映射。spring-doc.cadn.net.cn

以下示例显示了如何通过使用Spring Boot配置属性来启动带有SASL和Kerberos的Spring Cloud Stream应用程序:spring-doc.cadn.net.cn

 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

前面的示例表示等效于以下JAAS文件:<br>spring-doc.cadn.net.cn

KafkaClient {
    com.sun.security.auth.module.Krb5LoginModule required
    useKeyTab=true
    storeKey=true
    keyTab="/etc/security/keytabs/kafka_client.keytab"
    principal="[email protected]";
};

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

不要在同一应用程序中混合使用 JAAS 配置文件和 Spring Boot 属性。

spring-doc.cadn.net.cn

如果 -Djava.security.auth.login.config 系统属性已经存在,Spring Cloud Stream 将忽略 Spring Boot 属性。spring-doc.cadn.net.cn

使用autoCreateTopicsautoAddPartitions时请谨慎操作Kerberos。

spring-doc.cadn.net.cn

通常,应用程序可以使用在Kafka和Zookeeper中没有管理权限的主体。spring-doc.cadn.net.cn

因此,依赖Spring Cloud Stream创建/修改主题可能会失败。spring-doc.cadn.net.cn

在安全环境中,我们强烈建议通过使用Kafka工具手动创建主题并管理ACLs。spring-doc.cadn.net.cn

多绑定器配置和JAAS

当连接到多个集群时,每个集群都需要单独的 JAAS 配置,然后使用属性 sasl.jaas.config 设置 JAAS 配置。 如果应用程序中存在此属性,则它会优先于上述其他策略。 详细了解,请参阅此KIP-85spring-doc.cadn.net.cn

例如,如果您的应用程序中有两个具有独立 JAAS 配置的集群,则可以使用以下模板:<br>spring-doc.cadn.net.cn

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

请注意,上述配置中两个 Kafka 集群以及它们各自的 sasl.jaas.config 值均有所不同。spring-doc.cadn.net.cn

有关如何设置和运行此类应用程序的更多详细信息,请参见此示例应用spring-doc.cadn.net.cn