此版本仍在开发中,尚未被视为稳定版本。对于最新的稳定版本,请使用 spring-cloud-stream 4.1.4spring-doc.cadn.net.cn

安全配置

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

例如,要将security.protocolSASL_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 文件位置。 以下示例显示了如何使用 JAAS 配置文件启动具有 SASL 和 Kerberos 的 Spring Cloud Stream 应用程序: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.Krb5LoginModule.spring-doc.cadn.net.cn

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

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

违约:required.spring-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 文件的等效项: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 属性。 如果-Djava.security.auth.login.configsystem 属性已存在,则 Spring Cloud Stream 会忽略 Spring Boot 属性。
使用autoCreateTopicsautoAddPartitions与 Kerberos 一起。 通常,应用程序可能会使用在 Kafka 和 Zookeeper 中没有管理权限的委托人。 因此,依赖 Spring Cloud Stream 创建/修改 Topic 可能会失败。 在安全环境中,我们强烈建议使用 Kafka 工具以管理方式创建主题和管理 ACL。

Multi-binder 配置和 JAAS

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

例如,如果您的应用程序中有两个具有单独 JAAS 配置的集群,则以下是您可以使用的模板: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