mosquitto -- 权限配置

Mosquitto 权限是根据 topic 控制的、类似与目录管理。您可以设定每个用户订阅/发布权限、也可以设定每个用户可访问的topic范围、从而达到权限控制的目的。

这里我们需要我另外一个帖子(用户配置)创建的2个用户(admin/mosquitto)。

1: 给这两个用户配置不同的权限

    admin 设置为订阅权限,并且只能访问的主题为"root/topic/#"

    mosquitto 设置为发布权限,并且只能访问的主题为"root/topic/#"

    如果用 admin 进行发布是不会成功的、反过来用 mosquitto 进行订阅同样不会接受到任何信息。因为他们的权限不同。

  2: 增加权限配置

    打开配置文件 aclfile.example 在其中添加如下配信息。

user admin
topic read root/topic/#
user mosquitto
topic write root/topic/#

aclfile.example内容如下:

# This affects access control for clients with no username.
topic read $SYS/#


# This only affects clients with username "roger".
user roger
topic foo/bar


# This affects all clients.
pattern write $SYS/broker/connection/%c/state




# This is a bridge user.
user hthl_bridge
topic/#



user admin
topic read root/topic/#
user mosquitto
topic write root/topic/#

mosquitto.conf中对应内容

# 服务进程的PID
pid_file /var/run/mosquitto.pid
# 服务进程的系统用户
user mosquitto
# 服务绑定的IP地址
bind_address centoshostnameKL3
# 服务绑定的端口号
port 1883
# 允许的最大连接数,-1表示没有限制
max_connections -1
# 允许匿名用户true
allow_anonymous false
# # 用户/密码文件,默认格式:username:password
password_file /etc/mosquitto/pwfile
# # 配置用户访问控制
acl_file /etc/mosquitto/aclfile

  

/etc/mosquitto/pwfile对应内容
admin:$6$y1gIy8QixvxlVqH2$Jzf+f7A4vTcuKpceVciwi8qL07Q8wkCPGZaFlTqWqHl6Tz4OySA/jNjDHDMiLez3R04s79teDcrFIJBmR4XBgQ==
mosquitto:$6$vc2qZdyCBKl1mroB$tLyPqKO9nshKkYfrj8F7lzzAr60x6n9cx4i2Fuvq1gh8dFCqsoXKNrIxQdKXU/E+YNhK0PG6ffAGeQ1jVVBwbQ==
hthl_bridge:$6$/tsqpcivgBm0mo3r$kaC4xrLLhUYXE6mU28ZymgQ7HEqblY67CN2dn15rnuR7DCYs9w5jeVCuuCIPIW2+kb6ELgflTrLdD2Bhntf9bg==

  

read 订阅权限 、write 发布权限、# 通配符表示所有的、保存退出。

  3:修改 Mosquitto.conf 配置选项

    打开mosquitto.conf文件,找到acl_file节点。打开配置做如下修改:
    修改前:#acl_file
    修改后:acl_file /etc/mosquitto/aclfile.example 根据自己文件实际位置填写

原文地址:https://www.cnblogs.com/saryli/p/9820517.html