CentOS 上 Kafka 权限设置实操指南
一 架构与准备
二 快速落地方案 SASL SCRAM + ACL(推荐)
KafkaServer {
org.apache.kafka.common.security.scram.ScramLoginModule required
username="admin"
password="Admin@123"
user_admin="Admin@123"
user_producer="Prod@123"
user_consumer="Cons@123";
};
listeners=SASL_PLAINTEXT://:9092
advertised.listeners=SASL_PLAINTEXT://<your-hostname-or-ip>:9092
security.inter.broker.protocol=SASL_PLAINTEXT
sasl.enabled.mechanisms=SCRAM-SHA-256
sasl.mechanism.inter.broker.protocol=SCRAM-SHA-256
listener.name.sasl_plaintext.scram-sha-256.sasl.jaas.config=file:/opt/kafka/config/kafka_server_jaas.conf
authorizer.class.name=kafka.security.authorizer.AclAuthorizer
allow.everyone.if.no.acl.found=false
super.users=User:admin
[Service]
Environment="KAFKA_OPTS=-Djava.security.auth.login.config=/opt/kafka/config/kafka_server_jaas.conf"
ExecStart=/opt/kafka/bin/kafka-server-start.sh /opt/kafka/config/server.properties
bin/kafka-configs.sh --zookeeper localhost:2181 \
--alter --add-config 'SCRAM-SHA-256=[password=Admin@123]' \
--entity-type users --entity-name admin
bin/kafka-configs.sh --zookeeper localhost:2181 \
--alter --add-config 'SCRAM-SHA-256=[password=Prod@123]' \
--entity-type users --entity-name producer
bin/kafka-configs.sh --zookeeper localhost:2181 \
--alter --add-config 'SCRAM-SHA-256=[password=Cons@123]' \
--entity-type users --entity-name consumer
# 主题级别:producer 写,consumer 读,admin 全权
bin/kafka-acls.sh --bootstrap-server localhost:9092 \
--add --allow-principal User:producer --operation Write --operation Describe --topic test-topic
bin/kafka-acls.sh --bootstrap-server localhost:9092 \
--add --allow-principal User:consumer --operation Read --operation Describe --topic test-topic
bin/kafka-acls.sh --bootstrap-server localhost:9092 \
--add --allow-principal User:admin --operation All --topic test-topic
# 消费者组(Group)权限
bin/kafka-acls.sh --bootstrap-server localhost:9092 \
--add --allow-principal User:consumer --operation Read --group test-group
# 集群级运维权限(可选)
bin/kafka-acls.sh --bootstrap-server localhost:9092 \
--add --allow-principal User:admin --operation ClusterAction --cluster
# 生产
bin/kafka-console-producer.sh --bootstrap-server localhost:9092 --topic test-topic \
--producer-property security.protocol=SASL_PLAINTEXT \
--producer-property sasl.mechanism=SCRAM-SHA-256 \
--producer-property sasl.jaas.config='org.apache.kafka.common.security.scram.ScramLoginModule required username="producer" password="Prod@123";'
# 消费
bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic test-topic \
--from-beginning --consumer-property security.protocol=SASL_PLAINTEXT \
--consumer-property sasl.mechanism=SCRAM-SHA-256 \
--consumer-property sasl.jaas.config='org.apache.kafka.common.security.scram.ScramLoginModule required username="consumer" password="Cons@123";' \
--group test-group
三 进阶方案 SASL PLAINTEXT + ACL(简单但不便动态增删用户)
KafkaServer {
org.apache.kafka.common.security.plain.PlainLoginModule required
username="admin"
password="Admin@123"
user_admin="Admin@123"
user_producer="Prod@123"
user_consumer="Cons@123";
};
四 高安全方案 SASL + SSL + ACL(生产推荐)
五 常见问题与运维要点