Mosquitto MQTT Broker 服务器的配置,增加登录鉴权设置

本文的重点是从匿名访问mqtt broker(只需要Ip和port),到需要登录(用户名和密码)和鉴权(发布和订阅那些topics)的过度的 过程。

 安装 Mosquitto MQTT Broker 1

sudo apt-add-repository ppa:mosquitto-dev/mosquitto-ppa
sudo apt-get update
sudo apt-get install mosquitto
sudo apt-get install mosquitto-clients

配置Mosquitto MQTT Broker

1. 打开 mosquitto.conf

sudo vim /etc/mosquitto/mosquitto.conf

2. 添加 

# 禁止匿名访问
allow_anonymous false
# 认证配置
password_file /etc/mosquitto/pwfile
# 权限配置
acl_file /etc/mosquitto/aclfile

3. 添加pwfile

touch /etc/mosquitto/pwfile
mosquitto_passwd /etc/mosquitto/pwfile 用户名

  根据提示输入两遍密码
4. 添加aclfile (必须添加,否者无法启动Mosquitto,而且这个是白名单,只有列出的topics 才能被访问 

touch /etc/mosquitto/aclfile
vim /etc/mosquitto/aclfile
user A
topic write test/#
topic read $SYS/#

如何使用通配符,请参考MQTT Topics & Best Practices2

5. 启动Mosquitto broker

sudo service mosquitto start

可选参数 {start|stop|reload|force-reload|restart|try-restart|status}

6. 测试 Mosquitto broker

订阅

mosquitto_sub -h localhost -p 1883 -t "test/#" -u username -P 123456 -i "clientID"

发送

mosquitto_pub -h localhost -p port -t "test/abc" -u user name -P 123456 -i "client3" -m "How are you?"

 

请参考

1. https://www.jianshu.com/p/9e3cb7042a2e

2. https://www.hivemq.com/blog/mqtt-essentials-part-5-mqtt-topics-best-practices/

 

你可能感兴趣的:(IOT)