关于用户配置:
mosquitto可以添加多个用户,只有使用用户名和密码才能进行发布和订阅。这是加强安全性性的一种方式。
- 首先打开mosquitto.conf文件,找到allow_anonymous节点(security下面),这个节点作用是,是否开启匿名用户登录,默认是true。
- 我们需要将allow_anonymous节点前面的"#"去掉,然后后面写false。不允许匿名用户登录。如下:
修改前:#allow_anonymous 修改后:allow_anonymous false
- 找到password_file节点(security下面) ,这个节点是告诉服务器你要配置的用户将存放在哪里.将前面的“#”去掉,并且在后面写上pwfile.example文件的路径,注意:是绝对路径。例如:
修改前:#password_file 修改后:password_file /etc/mosquitto/pwfile.example(一般默认保存在etc/mosquitto/目录下)
- 然后要找到pwfile.example,开始创建用户名和密码:
//创建用户名 mosquitto_passwd -c /etc/mosquitto/pwfile.example admin 然后键入2次密码,用户创建成功。 //查看创建的用户 cat /etc/mosquitto/pwfile.example
关于权限配置:
你可以设定每个用户的发布和订阅的权限,也可以设定每个用户可以访问的topic的范围,从而就可以达到设置权限的目的。
- 首先打开mosquitto.conf文件,找到acl_file节点(security节点下面):
修改前:#acl_file 修改后:acl_file /etc/mosquitto/aclfile.example
- 打开aclfile.example,并再最下面加上如下代码:
//admin只有定于的权限,只能访问的主题是“root/topic/#” //read表示定于权限,write表示发布权限 user admin topic read root/topic/#
网友评论