Skip to content

基于 HTTP 的授权 ​

基于 HTTP 的授权是 EMQX Edge 中管理客户端访问的另一种方式。当 MQTT 客户端发送 CONNECT 报文时,EMQX Edge 可配置为向指定服务器发起 HTTP POST 请求进行认证。外部 HTTP 服务的响应会决定客户端是否被允许连接。

这种方式可将 EMQX Edge 与自定义或集中式认证系统集成。

配置示例 ​

要使用 HOCON 格式启用 http_auth,请在配置文件的 auth {} 配置段中加入以下配置块。变更会在重启 EMQX Edge 后生效。

完整配置项列表请参见访问控制配置。

bash
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", access = "%A", topic = "%t"}
    }

    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
    cache_ttl = 30s
  }
  ...
}

使用配置启动 EMQX Edge ​

要使用自定义配置文件启动 EMQX Edge,请运行:

bash
emqx-edge start --conf /path/to/nanomq.conf

请将 path/to/nanomq.conf 替换为你的配置文件实际路径。