物联网系列 - EMQ X Username 认证

311 阅读4分钟

「这是我参与11月更文挑战的第6天,活动详情查看:2021最后一次更文挑战」。

往前文章:

物联网系列 - 初识MQTT

物联网系列 - MQTT协议原理与数据包结构

物联网系列 - EMQ X简介与安装

物联网系列 - EMQ X Dashboard

物联网系列 - EMQ X 认证介绍

Username 认证使用配置文件预设客户端用户名与密码,支持通过 HTTP API 管理认证数据。

Username 认证不依赖外部数据源,使用上足够简单轻量。使用这种认证方式前需要开启插件,我们可以在Dashboard里找到这个插件并开启。

  • 插件:

      emqx_auth_username
    

image.png

  • 哈希方法 Username 认证默认使用 sha256 进行密码哈希加密,可在 etc/plugins/emqx_auth_username.conf 中更改:

    etc/plugins/emqx_auth_username.conf

    Value: plain | md5 | sha | sha256

    auth.user.password_hash = sha256

配置哈希方法后,新增的预设认证数据与通过 HTTP API 添加的认证数据将以哈希密文存储在 EMQ X 内置数据库中。

1. 预设认证数据

可以通过配置文件预设认证数据,编辑配置文件: etc/plugins/emqx_auth_username.conf

# etc/plugins/emqx_auth_username.conf
##--------------------------------------------------------------------
## Username Authentication Plugin
##--------------------------------------------------------------------
## Examples:
##auth.user.1.username = admin
##auth.user.1.password = public
##auth.user.2.username = feng@emqtt.io
##auth.user.2.password = public
##auth.user.3.username = name~!@#$%^&*()_+
##auth.user.3.password = pwsswd~!@#$%^&*()_+
## Password hash.
##
## Value: plain | md5 | sha | sha256
auth.user.password_hash = sha256

插件启动时将读取预设认证数据并加载到 EMQ X 内置数据库中,节点上的认证数据会在此阶段同步至集群中。

预设认证数据在配置文件中使用了明文密码,出于安全性与可维护性考虑应当避免使用该功能。

2. HTTP API 管理认证数据

EMQ X提供了对应的HTTP API用以维护内置数据源中的认证信息,我们可以添加/查看/取消/更改认证数据 我们通过VSCode来访问EMQ X的API /auth_username 完成认证数据的相关操作

1:查看已有认证用户数据: GET api/v4/auth_username

@hostname = 192.168.10.200
@port=18083
@contentType=application/json
@userName=admin
@password=public
#############查看已有用户认证数据##############
GET http://{{hostname}}:{{port}}/api/v4/auth_username HTTP/1.1
Content-Type: {{contentType}}
Authorization: Basic {{userName}}:{{password}

返回:

 HTTP/1.1 200 OK
connection: close
content-length: 20
content-type: application/json

{
"data": [],
"code": 0
}

2:添加认证数据API 定义: POST api/v4/auth_username{ "username": "emqx_u", "password": "emqx_p"}

########添加用户认证数据##############
POST http://{{hostname}}:{{port}}/api/v4/auth_username HTTP/1.1
Content-Type: {{contentType}}
Authorization: Basic {{userName}}:{{password}}
{
"username": "user",
"password": "123456"
}

使用 POST 请求添加 username 为 user password 为 123456 的认证信息,返回信息中 code = 0 即为成 功。

返回:

HTTP/1.1 200 OK
connection: close
content-length: 10
content-type: application/json

{
"code": 0
}
3:更改指定用户名的密码API 定义: PUT api/v4/auth_username/${username}{ "password": "emqx_new_p"}

指定用户名,传递新密码进行更改,再次连接时需要使用新密码进行连接:

###########更改指定用户名的密码#############
PUT http://{{hostname}}:{{port}}/api/v4/auth_username/user HTTP/1.1
Content-Type: {{contentType}}
Authorization: Basic {{userName}}:{{password}}
{
"password": "user"
}

返回:

 HTTP/1.1 200 OK
connection: close
content-length: 10
content-type: application/json

{
"code": 0
}

4: 查看指定用户名信息API 定义: GET api/v4/auth_username/${username}

指定用户名,查看相关用户名、密码信息,注意此处返回的密码是使用配置文件指定哈希方式加密后的密码:

###########查看指定用户名信息#############
GET http://{{hostname}}:{{port}}/api/v4/auth_username/user HTTP/1.1
Content-Type: {{contentType}}
Authorization: Basic {{userName}}:{{password}}

返回:

HTTP/1.1 200 OK
connection: close
content-length: 115
content-type: application/json
date: Thu, 04 Jun 2020 08:14:47 GMT
server: Cowboy
{
"data": {
"username": "user",
"password": "687f54edc0779f4e26314d9cc957eba27a078a39997069d3c52c9dda3db4aa07"
},
"code": 0
}

5:删除认证数据API 定义: DELETE api/v4/auth_username/${username}

用以删除指定认证数据

###########删除指定的用户信息#############
DELETE http://{{hostname}}:{{port}}/api/v4/auth_username/user HTTP/1.1
Content-Type: {{contentType}}
Authorization: Basic {{userName}}:{{password}}

返回:

 HTTP/1.1 200 OK
connection: close
content-length: 10
content-type: application/json

{
"code": 0
}

3. MQTTX客户端验证

使用mqtt客户端工具验证使用username连接登录的功能。从 github.com/emqx/MQTTX 这个地址下载 对应操作系统的mqtt客户端工具。

1:新建连接,参数配置如下

user.jpg

在对应的输入框内输入username和password,clientId这里目前可以随便输入(因为基于clientId的认证功能 还没有启用),之后点连接,连接成功。用户名和密码如果输入错误的话是连接不成功的。

2:再次创建一个客户端连接,可作为消息的订阅者,上一个连接作为发布者,如下

user.jpg

3:订阅者添加订阅

image.png

订阅完成后:

image.png

4:上一个客户端连接作为消息的发布者来进行消息的发布:

image.png

5:查看订阅者是否已经接收到消息:

image.png

下期我们来讲 Client ID 认证和 HTTP 请求信息