「这是我参与11月更文挑战的第7天,活动详情查看:2021最后一次更文挑战」。
往前文章:
1. Client ID 认证
Client ID 认证使用配置文件预设客户端Client ID 与密码,支持通过 HTTP API 管理认证数据。
Client ID 认证不依赖外部数据源,使用上足够简单轻量,使用该种认证方式时需要开启 emqx_auth_clientid 插件,直接在DashBoard中开启即可,
-
哈希方法 Client ID 认证默认使用 sha256 进行密码哈希加密,可在 etc/plugins/emqx_auth_clientid.conf 中更改:
etc/plugins/emqx_auth_clientid.conf
Value: plain | md5 | sha | sha256
auth.client.password_hash = sha256
配置哈希方法后,新增的预设认证数据与通过 HTTP API 添加的认证数据将以哈希密文存储在 EMQ X 内置数 据库中。
1.预设认证数据
可以通过配置文件预设认证数据,编辑配置文件: etc/plugins/emqx_auth_clientid.conf
# etc/plugins/emqx_auth_clientid.conf
##--------------------------------------------------------------------
## ClientId Authentication Plugin
##--------------------------------------------------------------------
## Examples
##auth.client.1.clientid = id
##auth.client.1.password = passwd
##auth.client.2.clientid = dev:devid
##auth.client.2.password = passwd2
##auth.client.3.clientid = app:appid
##auth.client.3.password = passwd3
##auth.client.4.clientid = client~!@#$%^&*()_+
##auth.client.4.password = passwd~!@#$%^&*()_+
## Password hash.
##
## Value: plain | md5 | sha | sha256
auth.client.password_hash = sha256
插件启动时将读取预设认证数据并加载到 EMQ X 内置数据库中,节点上的认证数据会在此阶段同步至集群中。
预设认证数据在配置文件中使用了明文密码,出于安全性与可维护性考虑应当避免使用该功能。
2. HTTP API 管理认证数据
我们使用VSCode来通过EMQ X的API来添加和查看Client ID的认证数据。
1:添加认证数据API 定义: POST api/v4/auth_clientid{ "clientid": "emqx_c", "password": "emqx_p"}
####添加clientId和密码#####
POST http://{{hostname}}:{{port}}/api/v4/auth_clientid HTTP/1.1
Content-Type: {{contentType}}
Authorization: Basic {{userName}}:{{password}}
{
"clientid": "emq-client1",
"password": "123456"
}
使用 POST 请求添加 clientid 为 emq-client1 password 为 123456 的认证信息,返回信息中 code = 0 即 为成功。
返回:
HTTP/1.1 200 OK
connection: close
content-length: 10
content-type: application/json
{
"code": 0
}
2:查看已经添加的认证数据API 定义: GET api/v4/auth_clientid
#############获取所有详细信息########
GET http://{{hostname}}:{{port}}/api/v4/auth_clientid HTTP/1.1
Content-Type: {{contentType}}
Authorization: Basic {{userName}}:{{password}}
返回:
HTTP/1.1 200 OK
connection: close
content-length: 33
content-type: application/json
{
"data": [
"emq-client1"
],
"code": 0
}
3:更改指定 Client ID 的密码API 定义: PUT api/v4/auth_clientid/${clientid}{ "password":"emqx_new_p"}
指定 Client ID,传递新密码进行更改,再次连接时需要使用新密码进行连接:
#############更改指定 Client ID 的密码########
PUT http://{{hostname}}:{{port}}/api/v4/auth_clientid/emq-client1 HTTP/1.1
Content-Type: {{contentType}}
Authorization: Basic {{userName}}:{{password}}
{
"password": "654321"
}
返回:
HTTP/1.1 200 OK
connection: close
content-length: 10
content-type: application/json
{
"code": 0
}
4:查看指定 Client ID 信息API 定义: GET api/v4/auth_clientid/${clientid}
指定 Client ID,查看相关 Client ID、密码信息,注意此处返回的密码是使用配置文件指定哈希方式加密后的密码:
#############获取指定ClientId详细信息########
GET http://{{hostname}}:{{port}}/api/v4/auth_clientid/emq-client1 HTTP/1.1
Content-Type: {{contentType}}
Authorization: Basic {{userName}}:{{password}}
返回:
HTTP/1.1 200 OK
connection: close
content-length: 122
content-type: application/json
{
"data": {
"password": "cab4af60ae344f27da3de35b8f031286468f7983553f5cd2ac1e6f960b6d76b6",
"clientid": "emq-client1"
},
"code": 0
}
5:删除认证数据API 定义: DELETE api/v4/auth_clientid/${clientid}
删除指定 Client ID:
#############删除指定的client信息########
DELETE http://{{hostname}}:{{port}}/api/v4/auth_clientid/emq-client1 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客户端工具验证使用Client ID连接登录的功能
此时用户名字段需要输入一个,但是可以随便填写