云服务器(Centos7)设置kafka认证密码

云服务器(Centos7)设置kafka认证密码

以kafak3.2.0版本为例,先解压到某个目录,修改配置文件完成。

安装kafka教程传送门:https://blog.csdn.net/wssc555/article/details/127857626

测试正常启动后开始设置认证密码。

1.修改kafka配置文件 server.properties

listeners=SASL_PLAINTEXT://内网ip:9092

# Listener name, hostname and port the broker will advertise to clients.
# If not set, it uses the value for "listeners".
advertised.listeners=SASL_PLAINTEXT://外网ip:9092

# Maps listener names to security protocols, the default is for them to be the same. See the config documentation for more details
#listener.security.protocol.map=PLAINTEXT:PLAINTEXT,SSL:SSL,SASL_PLAINTEXT:SASL_PLAINTEXT,SASL_SSL:SASL_SSL
#使用的认证协议
security.inter.broker.protocol=SASL_PLAINTEXT
#SASL机制
sasl.enabled.mechanisms=PLAIN
sasl.mechanism.inter.broker.protocol=PLAIN
#完成身份验证的类
authorizer.class.name=kafka.security.authorizer.AclAuthorizer
#如果没有找到ACL(访问控制列表)配置,则允许任何操作。
allow.everyone.if.no.acl.found=false
#需要开启设置超级管理员,设置admin用户为超级管理员
super.users=User:admin

2.添加SASL配置文件

在kafka安装目录下创建一个 kafka_server_jaas.conf 文件
vi /config/kafka_server_jaas.conf:

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


说明:该配置通过org.apache.org.apache.kafka.common.security.plain.PlainLoginModule由指定采用PLAIN机制,定义了用户。

usemame和password指定该代理与集群其他代理初始化连接的用户名和密码
"user_"为前缀后接用户名方式创建连接代理的用户名和密码,例如,user_producer=“producerpwd” 是指用户名为producer,密码为producerpwd
username为admin的用户,和user为admin的用户,密码要保持一致,否则会认证失败
上述配置中,创建了三个用户,分别为admin、producer和consumer(创建多少个用户,可根据业务需要配置,用户名和密码可自定义设置)

3.修改启动脚本

vi /bin/kafka-server-start.sh ,加入一行:

export KAFKA_OPTS="-Djava.security.auth.login.config=/usr/kafka_2.12-3.2.0/config/kafka_server_jaas.conf"

如图:

4.启动kafka服务

# 进入kafka_2.12-3.2.0目录下
[root@VM-20-8-centos kafka_2.12-3.2.0]#
# 先启动zookeeper
bin/zookeeper-server-start.sh -daemon ./config/zookeeper.properties  

# 启动kafka
bin/kafka-server-start.sh -daemon ./config/server.properties 

5.测试连接

5.1使用kafkatool测试

在JAAS Config中配置用户名和密码

org.apache.kafka.common.security.plain.PlainLoginModule required
        username="admin"
        password="admin@123";

在这里插入图片描述
在这里插入图片描述

点击Connect,若能连接成功,左侧红灯变为绿色。

5.2使用Springboot连接

引入kafka依赖

 <dependency>
            <groupId>org.springframework.kafka</groupId>
            <artifactId>spring-kafka</artifactId>
            <version>2.9.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.kafka</groupId>
            <artifactId>kafka-clients</artifactId>
            <version>3.2.0</version>
            <exclusions>
                <exclusion>
                    <artifactId>slf4j-api</artifactId>
                    <groupId>org.slf4j</groupId>
                </exclusion>
            </exclusions>
        </dependency>

在application.yml中配置

spring:
  kafka:
    # 消费者
    consumer:
      group-id: 消费组id
      auto-offset-reset: earliest
      bootstrap-servers: 云服务器外网ip
    # 生产者
    producer:
      bootstrap-servers: 云服务器外网ip
      key-serializer: org.apache.kafka.common.serialization.StringSerializer
      value-serializer: org.apache.kafka.common.serialization.StringSerializer
    properties:
      security:
        protocol: SASL_PLAINTEXT
      sasl:
        mechanism: PLAIN
        jaas:
          config: 'org.apache.kafka.common.security.scram.ScramLoginModule required username="admin" password="admin@123";'

启动项目

在这里插入图片描述

连接成功。

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

chaond

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

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

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

打赏作者

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

抵扣说明:

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

余额充值