HTTP 认证
NanoMQ 同时支持 HTTP 认证。HTTP 认证功能支持用户使用外部 HTTP 服务进行客户端行为的授权。当 NanoMQ 从 MQTT 客户端接收 CONNECT
数据包时,NanoMQ 将按照配置为目标 HTTP 服务器的格式发送 HTTP POST 请求,并依靠 HTTP POST 的返回码进行客户端授权决定是否允许其连接。
提示
目前,HTTP Authorization 仅支持 MQTT CONNECT
,将来将添加对 PUBLISH
和 SUBSCRIB
的支持。如果您急需进一步的支持,请在 Github 发布 Feature Request。
配置示例
如果需要使用 http_auth
,可按着下面示例的格式修改,然后将 http_auth
的配置放到配置文件 nanomq.conf
文件的 auth {}
内。相关设置将在 NanoMQ 重启后生效。
auth {
...
http_auth = {
auth_req {
url = "http://127.0.0.1:80/mqtt/auth"
method = post
headers.content-type = "application/x-www-form-urlencoded"
params = {clientid = "%c", username = "%u", password = "%P"}
}
super_req {
url = "http://127.0.0.1:80/mqtt/superuser"
method = "post"
headers.content-type = "application/x-www-form-urlencoded"
params = {clientid = "%c", username = "%u", password = "%P"}
}
acl_req {
url = "http://127.0.0.1:8991/mqtt/acl"
method = "post"
headers.content-type = "application/x-www-form-urlencoded"
params = {clientid = "%c", username = "%u", access = "%A", ipaddr = "%a", topic = "%t", mountpoint = "%m"}
}
timeout = 5s
connect_timeout = 5s
pool_size = 32
}
...
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
如希望使用 KV 配置格式,可参考以下格式,将配置写入 nanomq_old.conf
文件,相关设置将在 NanoMQ 重启后生效。
完整的配置项列表,可参考经典 KV 格式配置说明
auth.http.enable=<Your Value>
auth.http.auth_req.url="http://127.0.0.1:80/mqtt/auth"
auth.http.auth_req.method="post"
auth.http.auth_req.headers.<Any>=<Your Value>
auth.http.auth_req.params.clientid="%c"
auth.http.auth_req.params.username="%u"
auth.http.auth_req.params.password="%p"
auth.http.super_req.url="http://127.0.0.1:80/mqtt/superuser"
auth.http.super_req.method="post"
auth.http.super_req.headers.<Any>=<Your Value>
auth.http.super_req.params.clientid="%c"
auth.http.super_req.params.username="%u"
auth.http.super_req.params.password="%p"
auth.http.acl_req.url="http://127.0.0.1:8991/mqtt/acl"
auth.http.acl_req.method="post"
auth.http.acl_req.headers.<Any>=<Your Value>
auth.http.acl_req.params.clientid="%c"
auth.http.acl_req.params.username="%u"
auth.http.acl_req.params.access="%A"
auth.http.acl_req.params.ipaddr="%a"
auth.http.acl_req.params.topic="%t"
auth.http.acl_req.params.mountpoint="%m"
auth.http.timeout="5s"
auth.http.connect_timeout="5s"
auth.http.ssl.cacertfile=<Your Value>
auth.http.ssl.certfile=<Your Value>
auth.http.ssl.keyfile=<Your Value>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
启动 NanoMQ
启动 NanoMQ 并指定配置文件
$ nanomq start --conf path/to/nanomq.conf
1
如使用 KV 格式配置文件,可通过如下格式启动 NanoMQ
$ nanomq start --old_conf path/to/nanomq_old.conf
1