本文将讲解 Mosquitto 用户机制、如果还没有搭建 Mosquitto 服务的可以参考我的另外两篇文章<< 一、Mosquitto 介绍&安装>> << 二、 Mosquitto 的使用说明 >> 来搭建 Mosquitto 服务。
mosquitto中可以添加多个用户,只有使用用户名和密码登陆服务器才允许用户进行订阅与发布操作。可以说用户机制是mosquitto重要的安全机制,增强服务器的安全性。
用户与权限配置需要修改3处地方:
1、mosquitto中最最最重要的配置文件mosquitto.conf(配置文件的位置请看<< 一、Mosquitto 介绍&安装>> 文章中 1.5节 默认安装配置文件位置 中会有说明 )。
2、pwfile.example (保存用户名与密码)
3、aclfile.example (保存权限配置)
首先我们来新增两个用户 1: admin/admin 2: mosquitto/mosquitto 具体步骤:
1: 打开mosquitto.conf文件,找到allow_anonymous节点,这个节点作用是,是否开启匿名用户登录,默认是true。打开此项配置(将前面的 # 号去掉)之后将其值改为true
修改前:#allow_anonymous
修改后:allow_anonymous false
2: 找到password_file节点,这个节点是告诉服务器你要配置的用户将存放在哪里。打开此配置并指定pwfile.example文件路劲(注意是绝对路劲)
修改前:#password_file
修改后:password_file /etc/mosquitto/pwfile.example (这里的地址根据自己文件实际位置填写)
3: 创建用户名和密码、打开命令窗口 键入如下命令:
mosquitto_passwd -c /etc/mosquitto/pwfile.example admin
提示连续两次输入密码、创建成功。命令解释: -c 创建一个用户、/etc/mosquitto/pwfile.example 是将用户创建到 pwfile.example 文件中、admin 是用户名。
4: 创建mosquitto用户。在命令窗口键入如下命令:
mosquitto_passwd /etc/mosquitto/pwfile.example mosquitto
同样连续会提示连续输入两次密码。注意第二次创建用户时不用加 -c 如果加 -c 会把第一次创建的用户覆盖。
至此两个用户创建成功,此时如果查看 pwfile.example 文件会发现其中多了两个用户。
此时所有客户端连接 Mosquitto 服务都需要输入用户名密码、测试请用上篇文章<<三、Mosquitto Java 客户端实现>> 中测试代码测试。
1524729248: New connection from 192.168.72.103 on port 1883.
1524729248: New client connected from 192.168.72.103 as mosqsub|2205-centoshost (c1, k60, u'mosquitto').
1524729286: New connection from 192.168.72.103 on port 1883.
1524729286: New client connected from 192.168.72.103 as mosqpub|2207-centoshost (c1, k60, u'admin').
1524729286: Client mosqpub|2207-centoshost disconnected.
1524729494: New connection from 192.168.72.1 on port 1883.
1524729494: New client connected from 192.168.72.1 as client11 (c1, k20, u'admin').
1524729506: New connection from 192.168.72.1 on port 1883.
1524729506: New client connected from 192.168.72.1 as server11 (c0, k20, u'mosquitto').
1524729521: New connection from 192.168.72.1 on port 1883.
1524729522: Client server11 already connected, closing old connection.
1524729522: Client server11 disconnected.
1524729522: New client connected from 192.168.72.1 as server11 (c0, k20, u'mosquitto').
1524729549: New connection from 192.168.72.1 on port 1883.
1524729549: Client client11 already connected, closing old connection.
1524729549: Client client11 disconnected.
1524729549: New client connected from 192.168.72.1 as client11 (c1, k20, u'admin').
1524729560: New connection from 192.168.72.1 on port 1883.
1524729560: Client server11 already connected, closing old connection.
1524729560: Client server11 disconnected.
1524729560: New client connected from 192.168.72.1 as server11 (c0, k20, u'mosquitto').
1524729569: New connection from 192.168.72.1 on port 1883.
1524729569: Client server11 already connected, closing old connection.
1524729569: Client server11 disconnected.
1524729569: New client connected from 192.168.72.1 as server11 (c0, k20, u'mosquitto').
1524729599: New connection from 192.168.72.1 on port 1883.
1524729599: Client server11 already connected, closing old connection.
1524729599: Client server11 disconnected.
1524729599: New client connected from 192.168.72.1 as server11 (c0, k20, u'mosquitto').
1524729680: Socket error on client server11, disconnecting.
1524729704: New connection from 192.168.72.1 on port 1883.
1524729704: Socket error on client
1524729704: New connection from 192.168.72.1 on port 1883.
1524729704: Socket error on client
1524729737: Socket error on client client11, disconnecting.
^C1524729745: mosquitto version 1.4.14 terminating
[wangshumin@centoshostnameKL3 mosquitto]$ su root
密码:
[root@centoshostnameKL3 mosquitto]# ll
总用量 100
-rw-r--r--. 1 root root 280 4月 25 17:07 aclfile
-rw-r--r--. 1 root root 230 4月 25 16:46 aclfile.example
-rw-r--r--. 1 root root 38064 4月 26 15:51 mosquitto.conf
-rw-r--r--. 1 root root 37730 4月 25 16:46 mosquitto.conf.example
-rw-r--r--. 1 root root 23 4月 25 16:46 pskfile.example
-rw-r--r--. 1 root root 234 4月 26 15:52 pwfile
-rw-r--r--. 1 root root 355 4月 25 16:46 pwfile.example
[root@centoshostnameKL3 mosquitto]# cat pwfile
admin:$6$y1gIy8QixvxlVqH2$Jzf+f7A4vTcuKpceVciwi8qL07Q8wkCPGZaFlTqWqHl6Tz4OySA/jNjDHDMiLez3R04s79teDcrFIJBmR4XBgQ==
mosquitto:$6$vc2qZdyCBKl1mroB$tLyPqKO9nshKkYfrj8F7lzzAr60x6n9cx4i2Fuvq1gh8dFCqsoXKNrIxQdKXU/E+YNhK0PG6ffAGeQ1jVVBwbQ==
[root@centoshostnameKL3 mosquitto]# vim mosquitto.conf
[root@centoshostnameKL3 mosquitto]# vim mosquitto.conf
[wangshumin@centoshostnameKL3 ~]$ mosquitto_pub -t mqtt -h centoshostnameKL3 -m "new locatio" -u admin -P 123456
[wangshumin@centoshostnameKL3 ~]$
[wangshumin@centoshostnameKL3 ~]$ mosquitto_sub -t mqtt -h centoshostnameKL3
new locatio
new locatio
new locatio
new locatio
new locatio
new locatio
Connection Refused: not authorised.
Connection Refused: not authorised.
Connection Refused: not authorised.
Connection Refused: not authorised.
Connection Refused: not authorised.
Connection Refused: not authorised.
Connection Refused: not authorised.
Connection Refused: not authorised.
Connection Refused: not authorised.
^C
[wangshumin@centoshostnameKL3 ~]$ mosquitto_sub -t mqtt -h centoshostnameKL3 -u mosquitto -P 123456
new locatio
^C