windows版kafka设置密码

1.新增配置文件

1.1.在config下新建kafka_server_jaas.conf文件,内容如下:

KafkaServer {
    org.apache.kafka.common.security.plain.PlainLoginModule required
        username="admin"
        password="admin"
        user_admin="admin"
        user_test="test";
};

注意事项

	注册账号:user_admin="admin"
			user_test="test"
			表示注册用户test,密码为test
	登录超级管理员账号:username="admin"
                     password="admin"
    下文会将admin设置为超级管理员,test用户是消费者以及生产者使用的账号即client

1.2.在config下新建kafka_client_jaas.conf文件,内容如下:

登录test用户:

KafkaClient {  
	org.apache.kafka.common.security.plain.PlainLoginModule required  
    	username="test"  
    	password="test";  
};

2.修改bat文件

2.1.修改kafka-server-start.bat文件

注意:windows版本应该在bin/windows下修改

添加如下内容:

IF ["%KAFKA_OPTS%"] EQU [""] (
    set KAFKA_OPTS=-Djava.security.auth.login.config=file:%~dp0../../config/kafka_server_jaas.conf
)

2.2.修改kafka-console-producer.bat文件

添加如下内容:

IF ["%KAFKA_OPTS%"] EQU [""] (
    set KAFKA_OPTS=-Djava.security.auth.login.config=file:%~dp0../../config/kafka_client_jaas.conf
)

2.3.修改kafka-console-consumer.bat文件

添加如下内容:

IF ["%KAFKA_OPTS%"] EQU [""] (
    set KAFKA_OPTS=-Djava.security.auth.login.config=file:%~dp0../../config/kafka_client_jaas.conf
)

3.添加认证信息

注意:在config下

3.1.consumer.propertiesproducer.properties中添加命令

security.protocol=SASL_PLAINTEXT
sasl.mechanism=PLAIN

3.2.修改server.properties

添加设置如下

listeners=SASL_PLAINTEXT://127.0.0.1:9092

#使用的认证协议
security.inter.broker.protocol=SASL_PLAINTEXT
#SASL机制
sasl.enabled.mechanisms=PLAIN
sasl.mechanism.inter.broker.protocol=PLAIN
#完成身份验证的类
authorizer.class.name=kafka.security.auth.SimpleAclAuthorizer
#如果没有找到ACL(访问控制列表)配置,则允许任何操作。
#allow.everyone.if.no.acl.found=true
#需要开启设置超级管理员, 开启以下命令
super.users=User:admin
#delete.topic.enable=true
#auto.create.topics.enable=false

4.启动

4.1.zookeeper正常启动即可

4.2.启动kafka

kafka-server-start.bat ..\..\config\server.properties

4.3.创建主题test

kafka-topics.bat --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic test

4.4.账号授权

4.4.1.给test账户赋予topic=[topic_name]的写权限
kafka-acls.bat --authorizer kafka.security.auth.SimpleAclAuthorizer --authorizer-properties zookeeper.connect=127.0.0.1:2181 --add --allow-principal User:test --operation Write --topic test
4.4.2.给test账户赋予topic=[topic_name]的读权限
kafka-acls.bat --authorizer kafka.security.auth.SimpleAclAuthorizer --authorizer-properties zookeeper.connect=127.0.0.1:2181 --add --allow-principal User:test --operation Read --topic test
4.4.3给test账户赋予group=[group_id]读授权

默认消费组:test-consumer-group

kafka-acls.bat --authorizer kafka.security.auth.SimpleAclAuthorizer --authorizer-properties zookeeper.connect=127.0.0.1:2181 --add --allow-principal User:test --operation Read --group test-consumer-group

4.3.启动生产者

注意:config必须加上,否则会连接失败

kafka-console-producer.bat --bootstrap-server localhost:9092 --topic test --producer.config ../../config/producer.properties

4.5.启动消费者

注意:config必须加上,否则会连接失败

kafka-console-consumer.bat --bootstrap-server localhost:9092  --from-beginning --topic test --consumer.config ../../config/consumer.properties

4.6.kafkaTool连接

security:

Type: SASL Planintext

Advanced

Bootstrap servers: localhost:9092
SASL Mechanism: PLAIN

JAAS Config

org.apache.kafka.common.security.plain.PlainLoginModule required
        username="test"
        password="test";

5.springboot连接kafka

yml配置如下:

spring:
  kafka:
    bootstrap-servers: localhost:9092
    producer:
      properties:
        sasl.mechanism: PLAIN
        sasl.jaas.config: org.apache.kafka.common.security.plain.PlainLoginModule required username=test password=test;
        security.protocol: SASL_PLAINTEXT
    consumer:
      properties:
        sasl.mechanism: PLAIN
        sasl.jaas.config: org.apache.kafka.common.security.plain.PlainLoginModule required username=test password=test;
        security.protocol: SASL_PLAINTEXT

只需要将用户名和密码修改成自己设置的即可,该配置可对比KafkaTool连接

参考: https://www.jianshu.com/p/806545fc0a28

​ https://www.jianshu.com/p/ef9dad8f6a9b

​ https://blog.csdn.net/weixin_39587278/article/details/103965766

​ https://blog.csdn.net/qq_36389344/article/details/107233992

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐