在 Ubuntu 上部署 Kafka 时,鉴权(Authentication)通常配合 授权(Authorization) 一起做。Kafka 常见的鉴权方式有:
下面以 Ubuntu + Kafka 3.x + SASL/PLAIN + ACL 为例,给你一套可落地的配置步骤。
# 安装 Java
sudo apt update
sudo apt install openjdk-11-jdk -y
# 下载 Kafka
wget https://archive.apache.org/dist/kafka/3.6.1/kafka_2.13-3.6.1.tgz
tar -xzf kafka_2.13-3.6.1.tgz
cd kafka_2.13-3.6.1
sudo mkdir -p /opt/kafka/config/security
sudo vim /opt/kafka/config/security/kafka_server_jaas.conf
内容如下:
KafkaServer {
org.apache.kafka.common.security.plain.PlainLoginModule required
username="admin"
password="admin-secret"
user_admin="admin-secret"
user_producer="producer-secret"
user_consumer="consumer-secret";
};
server.properties编辑:
vim config/server.properties
关键配置:
listeners=SASL_PLAINTEXT://:9092
advertised.listeners=SASL_PLAINTEXT://localhost:9092
security.inter.broker.protocol=SASL_PLAINTEXT
sasl.mechanism.inter.broker.protocol=PLAIN
sasl.enabled.mechanisms=PLAIN
# 开启 ACL
authorizer.class.name=kafka.security.authorizer.AclAuthorizer
allow.everyone.if.no.acl.found=false
super.users=User:admin
export KAFKA_OPTS="-Djava.security.auth.login.config=/opt/kafka/config/security/kafka_server_jaas.conf"
bin/kafka-server-start.sh config/server.properties
producer.properties
security.protocol=SASL_PLAINTEXT
sasl.mechanism=PLAIN
sasl.jaas.config=org.apache.kafka.common.security.plain.PlainLoginModule required \
username="producer" \
password="producer-secret";
consumer.properties
security.protocol=SASL_PLAINTEXT
sasl.mechanism=PLAIN
sasl.jaas.config=org.apache.kafka.common.security.plain.PlainLoginModule required \
username="consumer" \
password="consumer-secret";
# producer 写权限
bin/kafka-acls.sh --bootstrap-server localhost:9092 \
--command-config admin.properties \
--add --allow-principal User:producer \
--operation Write --topic test
# consumer 读权限
bin/kafka-acls.sh --bootstrap-server localhost:9092 \
--command-config admin.properties \
--add --allow-principal User:consumer \
--operation Read --topic test
# consumer group
bin/kafka-acls.sh --bootstrap-server localhost:9092 \
--command-config admin.properties \
--add --allow-principal User:consumer \
--operation Read --group test-group
bin/kafka-console-producer.sh \
--bootstrap-server localhost:9092 \
--topic test \
--producer.config producer.properties
bin/kafka-console-consumer.sh \
--bootstrap-server localhost:9092 \
--topic test \
--group test-group \
--consumer.config consumer.properties
✅ 不要用 PLAINTEXT(明文)
→ 用 SASL_SSL
✅ 优先 SCRAM + SSL
✅ Kafka + Kerberos(企业级)
✅ 使用 KRaft 模式(Kafka 3.x 新架构)
SASL authentication failed
✅ 检查 JAAS 用户名密码是否一致
Not authorized to access topics
✅ ACL 没配或 user 写错
如果你愿意,我可以:
你现在是 测试环境还是生产环境?