【基本介绍】
这里介绍给mosquitto服务端,客户端,topic添加用户和密码等
【基本配置】
配置mosquitto.conf配置文件
Linux代码 - allow_anonymous false #不允许匿名
-
- password_file /etc/mosquitto/passwd #配置用户密码文件
-
- acl_file /etc/mosquitto/acl #配置topic和用户 (这个就是配置特权用户访问某主题,如果不配置则使用password_file文件中的用户密码进行访问)
用htpasswd配置passwd文件
mosquitto_passwd -c /etc/mosquitto/passwd pub_client
mosquitto_passwd /etc/mosquitto/passwd sub_client
Linux代码 - [root@sparkVM mosquitto]# cat /etc/mosquitto/passwd
- sub_client:$6$lHiPm6dLpaqsdfQb$SETYv2TthcgK388atPA7jNTSQYlWZzz8HxRzOVeZMx5iVNAAViuHhIgYzayl5BmzjNo8C0Cf4CH6ss6LdWtW8Q==
- pub_client:$6$NDYKXj+h1wb5rIsz$Mf1Hq+EEsmXXy1Y377Rt8S4oVfm3S06R6Km3rqzzOQYIKCIDz8z5vVFh8CHGx4zPnBRMWObNnFvOYVjnOe2Sdw==
配置acl,topic和用户的关系
Linux代码 - [root@sparkVM mosquitto]# cat /etc/mosquitto/acl
- #This only affects clients with username "pub_client".
- user pub_client
- topic write mtopic/#
-
- # This only affects clients with username "sub_client".
- user sub_client
- topic read mtopic/#
【测试验证】
服务端:
Linux代码 - [root@sparkVM mosquitto]# mosquitto_pub -t mtopic -m "test"
- 1416301592: New connection from ::1 on port 1883.
- Connection Refused: not authorised.
-
- [root@sparkVM mosquitto]# mosquitto_pub -t mtopic -u pub_client -P test -m "test"
- 1416301643: New connection from ::1 on port 1883.
- 1416301643: New client connected from ::1 as mosqpub/4113-sparkVM (c1, k60, upub_client).
客户端:
Linux代码 - [root@pandaVM html]# mosquitto_sub -h 192.168.197.128 -t mtopic
- Connection Refused: not authorised.
-
- [root@pandaVM html]# mosquitto_sub -h 192.168.197.128 -t mtopic -u sub_client -P sub_client
- test
|