分享

给mosquitto服务端,客户端,topic添加用户和密码

 和帅书馆 2016-12-26
【基本介绍】
这里介绍给mosquitto服务端,客户端,topic添加用户和密码等

【基本配置】
配置mosquitto.conf配置文件
Linux代码  收藏代码
  1. allow_anonymous false   #不允许匿名  
  2.   
  3. password_file /etc/mosquitto/passwd   #配置用户密码文件  
  4.   
  5. 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代码  收藏代码
  1. [root@sparkVM mosquitto]# cat /etc/mosquitto/passwd  
  2. sub_client:$6$lHiPm6dLpaqsdfQb$SETYv2TthcgK388atPA7jNTSQYlWZzz8HxRzOVeZMx5iVNAAViuHhIgYzayl5BmzjNo8C0Cf4CH6ss6LdWtW8Q==  
  3. pub_client:$6$NDYKXj+h1wb5rIsz$Mf1Hq+EEsmXXy1Y377Rt8S4oVfm3S06R6Km3rqzzOQYIKCIDz8z5vVFh8CHGx4zPnBRMWObNnFvOYVjnOe2Sdw==  


配置acl,topic和用户的关系
Linux代码  收藏代码
  1. [root@sparkVM mosquitto]# cat /etc/mosquitto/acl  
  2. #This only affects clients with username "pub_client".  
  3. user pub_client  
  4. topic write mtopic/#  
  5.   
  6. # This only affects clients with username "sub_client".  
  7. user sub_client  
  8. topic read mtopic/#  


【测试验证】
服务端:
Linux代码  收藏代码
  1. [root@sparkVM mosquitto]# mosquitto_pub -t mtopic -m "test"                       
  2. 1416301592: New connection from ::1 on port 1883.  
  3. Connection Refused: not authorised.  
  4.   
  5. [root@sparkVM mosquitto]# mosquitto_pub -t mtopic -u pub_client -P test -m "test"  
  6. 1416301643: New connection from ::1 on port 1883.  
  7. 1416301643: New client connected from ::1 as mosqpub/4113-sparkVM (c1, k60, upub_client).  


客户端:
Linux代码  收藏代码
  1. [root@pandaVM html]# mosquitto_sub -h 192.168.197.128 -t mtopic  
  2. Connection Refused: not authorised.  
  3.   
  4. [root@pandaVM html]# mosquitto_sub -h 192.168.197.128 -t mtopic -u sub_client -P sub_client  
  5. test 

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多