MQTT服务器搭建--Mosquitto用户名密码配置

前言:基于Mosquitto服务器已经搭建成功,大部分都是采用默认的是允许匿名用户登录模式,正式上线的系统需要进行用户认证。

1.用户参数说明

Mosquitto服务器的配置文件为/etc/mosquitto/mosquitto.conf,关于用户认证的方式和读取的配置都在这个文件中进行配置。

allow_anonymous允许匿名

password_file密码文件

acl_file访问控制列表

# 不允许匿名

allow_anonymous false

# 配置用户密码文件

password_file /etc/mosquitto/pwfile

# 配置topic和用户

acl_file /etc/mosquitto/acl

2.添加用户信息

添加用户'admin'

#mosquitto_passwd -c /etc/mosquitto/pwfile admin

   

3.添加Topic和用户的关系

#cat acl

找到你要的topic ---‘a/b’

#topic a/b

 在下面添加

user admin
topic write mtopic/#

 找到#This affects all clients

添加

user admin
topic read mtopic/#

 

4.用户认证测试

重启Mosquitto

通过Ctrl+C关闭mosquitto,然后通过下面命令启动Mosquitto

# mosquitto-c /etc/mosquitto/mosquitto.conf

  

(订阅端)客户端启动:

#mosquitto_sub -h 192.168.1.100 -t mtopic -u admin -P 123456

  

(发布者)客户端启动:

#mosquitto_pub -h 192.168.1.100 -t mtopic -u admin -P 123456 -m "test"

  

原文地址:https://www.cnblogs.com/saryli/p/8962624.html