MQTT 认证设置

EMQ 消息服务器认证由一系列认证插件(Plugin)提供,系统支持按用户名密码、ClientID 或匿名认证。

系统默认开启匿名认证(anonymous),通过加载认证插件可开启的多个认证模块组成认证链:

  1. ---------------- ---------------- ------------
  2. Client --> | Username认证 | -ignore-> | ClientID认证 | -ignore-> | 匿名认证 |
  3. ---------------- ---------------- ------------
  4. | | |
  5. \|/ \|/ \|/
  6. allow | deny allow | deny allow | deny

注解

EMQ 2.0 消息服务器还提供了 MySQL、PostgreSQL、Redis、MongoDB、HTTP、LDAP 认证插件。

开启匿名认证

etc/emq.conf 配置启用匿名认证:

  1. ## Allow Anonymous authentication
  2. mqtt.allow_anonymous = true

EMQ 2.0 版本提供的认证插件包括:

插件 说明
emq_auth_clientid ClientId 认证/鉴权插件
emq_auth_username 用户名密码认证/鉴权插件
emq_auth_ldap LDAP 认证/鉴权插件
emq_auth_http HTTP 认证/鉴权插件
emq_auth_mysql MySQ L认证/鉴权插件
emq_auth_pgsql Postgre 认证/鉴权插件
emq_auth_redis Redis 认证/鉴权插件
emq_auth_mongo MongoDB 认证/鉴权插件

用户名密码认证

基于 MQTT 登录用户名(username)、密码(password)认证。

etc/plugins/emq_auth_username.conf 中配置默认用户:

  1. auth.user.$N.username = admin
  2. auth.user.$N.password = public

启用 emq_auth_username 插件:

  1. ./bin/emqttd_ctl plugins load emq_auth_username

使用 ./bin/emqttd_ctl users 命令添加用户:

  1. $ ./bin/emqttd_ctl users add <Username> <Password>

ClientId 认证

基于 MQTT 客户端 ID 认证。

etc/plugins/emq_auth_clientid.conf:

  1. auth.client.$N.clientid = clientid
  2. auth.client.$N.password = passwd

启用 emq_auth_clientid 插件:

  1. ./bin/emqttd_ctl plugins load emq_auth_clientid

LDAP 插件认证

etc/plugins/emq_auth_ldap.conf 配置 LDAP 参数:

  1. auth.ldap.servers = 127.0.0.1
  2.  
  3. auth.ldap.port = 389
  4.  
  5. auth.ldap.timeout = 30
  6.  
  7. auth.ldap.user_dn = uid=%u,ou=People,dc=example,dc=com
  8.  
  9. auth.ldap.ssl = false

启用 LDAP 认证插件:

  1. ./bin/emqttd_ctl plugins load emq_auth_ldap

HTTP 插件认证

注解

开启 HTTP 认证插件后,会终结认证链

etc/plugins/emq_auth_http.conf 配置 ‘super_req’, ‘auth_req’:

  1. ## Variables: %u = username, %c = clientid, %a = ipaddress, %P = password, %t = topic
  2.  
  3. auth.http.auth_req = http://127.0.0.1:8080/mqtt/auth
  4. auth.http.auth_req.method = post
  5. auth.http.auth_req.params = clientid=%c,username=%u,password=%P
  6.  
  7. auth.http.super_req = http://127.0.0.1:8080/mqtt/superuser
  8. auth.http.super_req.method = post
  9. auth.http.super_req.params = clientid=%c,username=%u

启用 HTTP 认证插件:

  1. ./bin/emqttd_ctl plugins load emq_auth_http

MySQL 插件认证

通过 MySQL 数据库表认证,可创建如下的 ‘mqtt_user’ 表:

  1. CREATE TABLE `mqtt_user` (
  2. `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  3. `username` varchar(100) DEFAULT NULL,
  4. `password` varchar(100) DEFAULT NULL,
  5. `salt` varchar(20) DEFAULT NULL,
  6. `is_superuser` tinyint(1) DEFAULT 0,
  7. `created` datetime DEFAULT NULL,
  8. PRIMARY KEY (`id`),
  9. UNIQUE KEY `mqtt_username` (`username`)
  10. ) ENGINE=MyISAM DEFAULT CHARSET=utf8;

etc/plugins/emq_auth_mysql.conf 配置 ‘super_query’, ‘auth_query’, ‘password_hash’:

  1. ## Mysql Server
  2. auth.mysql.server = 127.0.0.1:3306
  3.  
  4. ## Mysql Pool Size
  5. auth.mysql.pool = 8
  6.  
  7. ## Mysql Username
  8. ## auth.mysql.username =
  9.  
  10. ## Mysql Password
  11. ## auth.mysql.password =
  12.  
  13. ## Mysql Database
  14. auth.mysql.database = mqtt
  15.  
  16. ## Variables: %u = username, %c = clientid
  17.  
  18. ## Authentication Query: select password only
  19. auth.mysql.auth_query = select password from mqtt_user where username = '%u' limit 1
  20.  
  21. ## Password hash: plain, md5, sha, sha256, pbkdf2
  22. auth.mysql.password_hash = sha256
  23.  
  24. ## %% Superuser Query
  25. auth.mysql.super_query = select is_superuser from mqtt_user where username = '%u' limit 1

注解

如果系统已有MQTT认证表,可通过配置’auth_query’查询语句集成。

启用 MySQL 认证插件:

  1. ./bin/emqttd_ctl plugins load emq_auth_mysql

Postgre 插件认证

通过 PostgreSQL 数据库表认证,可创建如下的 ‘mqtt_user’ 表:

  1. CREATE TABLE mqtt_user (
  2. id SERIAL primary key,
  3. is_superuser boolean,
  4. username character varying(100),
  5. password character varying(100),
  6. salt character varying(40)
  7. );

etc/plugins/emq_auth_pgsql.conf 配置 ‘auth_query’、’password_hash’:

  1. ## Postgre Server
  2. auth.pgsql.server = 127.0.0.1:5432
  3.  
  4. auth.pgsql.pool = 8
  5.  
  6. auth.pgsql.username = root
  7.  
  8. #auth.pgsql.password =
  9.  
  10. auth.pgsql.database = mqtt
  11.  
  12. auth.pgsql.encoding = utf8
  13.  
  14. auth.pgsql.ssl = false
  15.  
  16. ## Variables: %u = username, %c = clientid, %a = ipaddress
  17.  
  18. ## Authentication Query: select password only
  19. auth.pgsql.auth_query = select password from mqtt_user where username = '%u' limit 1
  20.  
  21. ## Password hash: plain, md5, sha, sha256, pbkdf2
  22. auth.pgsql.password_hash = sha256
  23.  
  24. ## sha256 with salt prefix
  25. ## auth.pgsql.password_hash = salt sha256
  26.  
  27. ## sha256 with salt suffix
  28. ## auth.pgsql.password_hash = sha256 salt
  29.  
  30. ## Superuser Query
  31. auth.pgsql.super_query = select is_superuser from mqtt_user where username = '%u' limit 1

启用 Postgre 认证插件:

  1. ./bin/emqttd_ctl plugins load emq_auth_pgsql

Redis 插件认证

Redis 认证。MQTT 用户记录存储在 Redis Hash, 键值: “mqtt_user:<Username>”

etc/plugins/emq_auth_redis.conf 设置 ‘super_cmd’、’auth_cmd’、’password_hash’:

  1. ## Redis Server
  2. auth.redis.server = 127.0.0.1:6379
  3.  
  4. ## Redis Pool Size
  5. auth.redis.pool = 8
  6.  
  7. ## Redis Database
  8. auth.redis.database = 0
  9.  
  10. ## Redis Password
  11. ## auth.redis.password =
  12.  
  13. ## Variables: %u = username, %c = clientid
  14.  
  15. ## Authentication Query Command
  16. auth.redis.auth_cmd = HGET mqtt_user:%u password
  17.  
  18. ## Password hash: plain, md5, sha, sha256, pbkdf2
  19. auth.redis.password_hash = sha256
  20.  
  21. ## Superuser Query Command
  22. auth.redis.super_cmd = HGET mqtt_user:%u is_superuser

启用 Redis 认证插件:

  1. ./bin/emqttd_ctl plugins load emq_auth_redis

MongoDB 插件认证

按 MongoDB 用户集合认证,例如创建 ‘mqtt_user’ 集合:

  1. {
  2. username: "user",
  3. password: "password hash",
  4. is_superuser: boolean (true, false),
  5. created: "datetime"
  6. }

etc/plugins/emq_auth_mongo.conf 设置 ‘super_query’、’auth_query’:

  1. ## Mongo Server
  2. auth.mongo.server = 127.0.0.1:27017
  3.  
  4. ## Mongo Pool Size
  5. auth.mongo.pool = 8
  6.  
  7. ## Mongo User
  8. ## auth.mongo.user =
  9.  
  10. ## Mongo Password
  11. ## auth.mongo.password =
  12.  
  13. ## Mongo Database
  14. auth.mongo.database = mqtt
  15.  
  16. ## auth_query
  17. auth.mongo.auth_query.collection = mqtt_user
  18.  
  19. auth.mongo.auth_query.password_field = password
  20.  
  21. auth.mongo.auth_query.password_hash = sha256
  22.  
  23. auth.mongo.auth_query.selector = username=%u
  24.  
  25. ## super_query
  26. auth.mongo.super_query.collection = mqtt_user
  27.  
  28. auth.mongo.super_query.super_field = is_superuser
  29.  
  30. auth.mongo.super_query.selector = username=%u

启用 MongoDB 认证插件:

  1. ./bin/emqttd_ctl plugins load emq_auth_mongo

emqtt emq 的用户密码认证的更多相关文章

  1. emqtt 试用(五)emq 的用户密码认证

    MQTT 认证设置 EMQ 消息服务器认证由一系列认证插件(Plugin)提供,系统支持按用户名密码.ClientID 或匿名认证. 系统默认开启匿名认证(anonymous),通过加载认证插件可开启 ...

  2. EMQ -- 用户密码认证

    emq 的用户密码认证 MQTT 认证设置 EMQ 消息服务器认证由一系列认证插件(Plugin)提供,系统支持按用户名密码.ClientID 或匿名认证. 系统默认开启匿名认证(anonymous) ...

  3. squid+stunnel+用户密码认证的三种玩法

    没办法,应用越来越深入,就会越来越多要求. squid+stunnel+用户密码认证的场景至少以下三个,我会遇到. 1,标准玩法 在服务器上建一个SQUID,加密码认证,然后,其它人通过它上网.(不要 ...

  4. elasticsearch 6.2.4添加用户密码认证

    elasticsearch 6.3版本之前的添加认证需安装x-pack插件,6.3之后貌似去掉了这个. 1.安装x-pack 先切换到elastic用户下,在执行以下命令 $cd /data/elas ...

  5. Oracle用户密码认证方式

    oracle用户有两种认证方式: 操作系统认证(要求该用户属于本地DBA组,然后通过操作系统认证登录oracle,从而启动数据库) 密码文件认证 oracle使用哪种认证方式决定在于两个参数: 1.r ...

  6. scylladb docker-compose 用户密码认证配置

    scylladb 对于用户的认证配置还是比较简单的,以下是一个docker-compose 配置的说明 环境准备 docker-compose 文件 version: "3" se ...

  7. python装饰器实现用户密码认证(简单初形)

    import timecurrent_user={'user':None}def auth(engine = 'file'): def deco(func): #func=最初始的index和最初始的 ...

  8. 搭建Docker私有仓库&用户密码认证&web可视化界面

    1.拉取镜像 docker pull hyper/docker-registry-web docker pull registry 2.安装 yum install docker-compose 3. ...

  9. ansible不配ssh连接,用户密码登录

    ansible 不配ssh免密链接,直接用ssh用户密码连接,要先装sshpass. 否则会报错: sshpass安装 sshpass下载地址:http://sourceforge.net/proje ...

随机推荐

  1. 判断页面是在移动端还是PC端打开的

    $(function () { var curWwwPath = window.document.location.href; var pathName = window.document.locat ...

  2. redis blog

    IBM 看到的blog如何 存储在redis种 var ArticleHelper = function () { this.ArticleIDSet = "AIDSet"; // ...

  3. 网络初级篇之DHCP原理与配置(原理与实验)

    一.什么是DHCP DHCP(Dynamic Host Configuration Protocol,动态主机配置协议)通常被应用在大型的局域网络环境中,主要作用是集中的管理.分配IP地址,使网络环境 ...

  4. stm32 development

    1.www.st.com st官网 2.www.stmcu.com.cn st中文网 3.www.stmcu.org.cn st中文社区

  5. GeoJson格式与转换(shapefile)Geotools

    转自:https://blog.csdn.net/cobramonkey/article/details/71124888 作为大数据分析的重要工具,Hadoop在这一领域发挥着不可或缺的作用.有些人 ...

  6. 标准C语言(2)

    字符类型名称是char,这个类型里一共包含256个不同的整数,每个整数代表一个字符(例如'a', '&'等),这些整数和字符可以互相替代,ASCII码表记录了所有整数和字符之间的对应关系 'a ...

  7. thinkphp5.11 关于数据库连接的配置

    config.php <?php// +----------------------------------------------------------------------// | Th ...

  8. 【leetcode】1232. Check If It Is a Straight Line

    题目如下: You are given an array coordinates, coordinates[i] = [x, y], where [x, y] represents the coord ...

  9. Nowcoder Sum of Maximum ( 容斥原理 && 拉格朗日插值法 )

    题目链接 题意 : 分析 : 分析就直接参考这个链接吧 ==> Click here 大体的思路就是 求和顺序不影响结果.故转化一下思路枚举每个最大值对答案的贡献最后累加就是结果 期间计数的过程 ...

  10. hdu 1051 wooden sticks (贪心+巧妙转化)

    #include <iostream>#include<stdio.h>#include<cmath>#include<algorithm>using ...