EMQTT
来自Jack's Lab
目录 |
1 开启认证
1.1 关闭匿名认证
默认开启,需要手动关闭匿名认证
1. 进入安装目录下 /etc,打开 emqx.conf文件
cd /opt/emqtt/etc vim emqx.conf
2. 找到这一行: allow_anonymous = true
将 true 改为 false,保存、退出,就关闭了匿名认证
1.2 添加 Username 插件 emq_auth_username
1. bin 目录下执行下面命令,加载 emq_auth_username 插件
./emqttd_ctl plugins load emq_auth_username
2. 执行下面命令,添加一个用户名和密码
./emqttd_ctl users add test 123456
3 使用下面命令可以执行所需的操作,大功告成
./emqx_ctl users users list # List users users add <Username> <Password> # Add User users update <Username> <NewPassword> # Update User users del <Username> # Delete User
2 ACL
支持基于 etc/acl.conf 或 MySQL,PostgreSQL 等插件的访问控制
## ACL nomatch. Enum: allow, deny acl_nomatch = allow ## Default ACL File acl_file = etc/acl.conf ## Enable ACL cache. Enum: on, off enable_acl_cache = on ## Default ACL cache size acl_cache_max_size = 32 ## Default time-to-live of cache size acl_cache_ttl = 1m
上面标黄得那个我们配置的关键,当acl规则没有匹配到的时候就会找的这个参数,如果为true,则会通过验证,为false就会不通过验证
官方说明:
%% 允许'dashboard'用户订阅 '$SYS/#' {allow, {user, "dashboard"}, subscribe, ["$SYS/#"]}. %% 允许本机用户发布订阅全部主题 {allow, {ipaddr, "127.0.0.1"}, pubsub, ["$SYS/#", "#"]}. %% 拒绝用户订阅'$SYS#'与'#'主题 {deny, all, subscribe, ["$SYS/#", {eq, "#"}]}. %% 上述规则无匹配,允许 {allow, all}.
注意:
- 注释使用 %%
- 不要忘了每个规则后面的 '.'
- subscribe 和 pubsub 配合使用
参考配置: