kafka快速配置启用ACL示例

前言

kafka支持基于SSL和SASL两种认证机制,本文以SASL说明。

kafka支持的SASL机制有5种:GSSAPI、PLAIN、SCRAM、OAUTHBEARER、Delegation Token。

本文使用SCRAM认证方式:一种通过用户名/密码的认证机制,可以动态增加和删除(PLAIN也是用户名/密码认证,但是写死在配置文件,增加新配置需要重启)

kafka版本我现在用的是从github trunk分支拉的最新代码,启动的版本信息如下:

Starting build with version 3.1.0-SNAPSHOT using Gradle 7.1.1, Java 1.8 and Scala 2.13.6

一定要注意版本,有些是进行的调整,直接复制不一定对的上。

认证

1. 新增管理员用户

sh bin/kafka-configs.sh --zookeeper localhost:2181 --alter --add-config 'SCRAM-SHA-256=[password=admin],SCRAM-SHA-512=[password=admin]' --entity-type users --entity-name admin

新增管理员:admin/admin。此时已经启动zk,但是没有启动broker,因为这些用户信息在zk上保存。

在新版本中大多命令都是建议--bootstrap-server代替--zookeeper,这条命令可以不用,因为还没启动broker,当然也可以先启动broker,使用--bootstrap-server来增加这个管理员用户,然后再把broker停掉。

2. 启用ACL配置

在server.properties增加如下配置:

listeners=SASL_PLAINTEXT://localhost:9092
sasl.enabled.mechanisms=SCRAM-SHA-256
sasl.mechanism.inter.broker.protocol=SCRAM-SHA-256
security.inter.broker.protocol=SASL_PLAINTEXT
authorizer.class.name=kafka.security.authorizer.AclAuthorizer
super.users=User:admin;User:xuxiaodong

最后一项配置super.users是配置超级管理员,示例是配置了两个用户:admin和xuxiaodong,前面新增管理员的命令只增加admin这个用户,增加xuxiaodong请再执行一次命令,这里配置2个只是示例,多个用户就这样配置(User:用户名,然后以分号分隔)。

3. 配置jaas

在config目录下创建:kafka-broker.jaas文件,内容如下:

KafkaServer {
org.apache.kafka.common.security.scram.ScramLoginModule required
username="admin"
password="admin";
};

如果有多个broker,第2、3步,每个broker都需要这样配置。

4. 启动broker

修改启动脚本bin/kafka-server-start.sh,在前面几行找个地方增加如下启动参数:

export KAFKA_OPTS=" $KAFKA_OPTS -Djava.security.auth.login.config=/Users/xuxd/SourceCode/github/kafka/kafka/config/kafka-broker.jaas

配置里需要指定第3步的jaas文件。

服务器端的配置已经结束,启动broker。

5. 新增用户

增加两个用户,一个叫writer,后面用来给topic发送消息,一个叫reader,后面用来消费消息。

sh bin/kafka-configs.sh --bootstrap-server localhost:9092 --alter --add-config 'SCRAM-SHA-256=[password=writer],SCRAM-SHA-512=[password=writer]' --entity-type users --entity-name writer --command-config cmd-config


sh bin/kafka-configs.sh --bootstrap-server localhost:9092 --alter --add-config 'SCRAM-SHA-256=[password=reader],SCRAM-SHA-512=[password=reader]' --entity-type users --entity-name reader --command-config cmd-config 

注意命令最后有个--command-config cmd-config,这是因为启用权限认证后,执行命令配置一下相关认证信息,否则授权失败,命令是不会正常处理的,cmd-config内容如下:

security.protocol=SASL_PLAINTEXT
sasl.mechanism=SCRAM-SHA-256
sasl.jaas.config=org.apache.kafka.common.security.scram.ScramLoginModule required username="admin" password="admin";

授权

这里新增用户和授权是分开的,需要单独再配置,admin用户就不用了,因为在前面已经作为超级管理员配置过了。

但是前面增加了writer和reader两个用户,是没有权限发送或消费消息的,需要单独给他们配置权限。

1. 给writer用户配置发送到test_topic这个主题的消息发送权限

sh bin/kafka-acls.sh --bootstrap-server 'localhost:9092' --add --allow-principal User:"writer" --producer --topic 'test_topic' --command-config cmd-config

2. 给reader用户配置使用消费组test_topic_consumer消费test_topic消息的权限

sh bin/kafka-acls.sh --bootstrap-server 'localhost:9092' --add --allow-principal User:"reader" --consumer --topic 'test_topic' --group 'test_topic_consumer' --command-config cmd-config

配置完之后,可以看到输出的权限信息如下:

客户端使用验证

这里就不用自带的脚本发送或消费消息测试了,直接用客户端代码验证一下,代码示例就不提供了,这东西你随便一搜都能找到:

消息发送如果没有配置权限,消息发送失败:

 客户端日志如下:

服务端的日志如下:

 给producer配置sasl认证信息,增加下面几项配置:

        props.put(CommonClientConfigs.SECURITY_PROTOCOL_CONFIG, "SASL_PLAINTEXT");
        props.put(SaslConfigs.SASL_MECHANISM, "SCRAM-SHA-256");
        props.put(SaslConfigs.SASL_JAAS_CONFIG, "org.apache.kafka.common.security.scram.ScramLoginModule required username=\"writer\" password=\"writer\";");

再发送消息就正常发送了。

消费端也是和发送端一样增加上面几项配置即可,注意修改为对应的用户名和密码。

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
在Spring Boot中使用Kafka需要进行一些配置才能启用。以下是配置启用Kafka的步骤: 1. 添加Kafka依赖:在`pom.xml`文件中添加Kafka的依赖项,例如: ```xml <dependency> <groupId>org.springframework.kafka</groupId> <artifactId>spring-kafka</artifactId> </dependency> ``` 2. 配置Kafka连接信息:在`application.properties`或`application.yml`文件中配置Kafka的连接信息,例如: ```properties spring.kafka.bootstrap-servers=localhost:9092 spring.kafka.consumer.group-id=my-group ``` 其中,`bootstrap-servers`指定了Kafka服务器的地址和端口,`consumer.group-id`指定了消费者组的ID。 3. 创建Kafka生产者和消费者:在Spring Boot应用程序中创建Kafka生产者和消费者的实例。可以使用`@EnableKafka`注解启用Kafka支持,并使用`@KafkaListener`注解定义消费者监听方法。 下面是一个简单的示例代码: ```java import org.springframework.kafka.annotation.EnableKafka; import org.springframework.kafka.annotation.KafkaListener; import org.springframework.kafka.core.KafkaTemplate; import org.springframework.stereotype.Component; @Component @EnableKafka public class KafkaExample { private final KafkaTemplate<String, String> kafkaTemplate; public KafkaExample(KafkaTemplate<String, String> kafkaTemplate) { this.kafkaTemplate = kafkaTemplate; } public void sendMessage(String topic, String message) { kafkaTemplate.send(topic, message); } @KafkaListener(topics = "my-topic") public void receiveMessage(String message) { System.out.println("Received message: " + message); } } ``` 以上代码示例中,`KafkaExample`类中的`sendMessage`方法用于发送消息,`receiveMessage`方法用于接收消息。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不识君的荒漠

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值