mqtt(emq)HTTP的ACL认证配置及业务场景测试

https://www.jianshu.com/p/88acccf43931
上篇中已经实践了与pg库的ACL验证配置
这次试试HTTP方式

1、首先修改配置文件,配置pgsql数据库的相关信息,路径/emqx/etc/plugins/emqx_auth_pgsql.conf

## 请求地址
auth.http.auth_req = http://127.0.0.1:80/mqtt/auth

## HTTP 请求方法
## Value: post | get | put
auth.http.auth_req.method = post

## 认证请求的 HTTP 请求头部,默认情况下配置 Content-Type 头部。
## Content-Type 头部目前支持以下值:application/x-www-form-urlencoded,application/json
auth.http.auth_req.headers.content-type = application/x-www-form-urlencoded

## 请求参数
auth.http.auth_req.params = clientid=%c,username=%u,password=%P



## 请求地址
auth.http.super_req = http://127.0.0.1:8991/mqtt/superuser

## HTTP 请求方法
## Value: post | get | put
auth.http.super_req.method = post

## 请求参数
auth.http.super_req.params = clientid=%c,username=%u


## 请求地址
auth.http.acl_req = http://127.0.0.1:8991/mqtt/acl

## HTTP 请求方法
## Value: post | get | put
auth.http.acl_req.method = get

## 请求参数
auth.http.acl_req.params = access=%A,username=%u,clientid=%c,ipaddr=%a,topic=%t,mountpoint=%m

参数说明
%A:操作类型,'1' 订阅;'2' 发布
%u:客户端用户名
%c:Client ID
%a:客户端 IP 地址
%r:客户端接入协议
%m:Mountpoint
%t:主题

有三个请求地址需要配置
auth:身份认证
superuser:超级管理员认证
acl:权限认证

rest测试类

package com.test.emq;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;

/**
 * @Author: chengsh05
 * @Date: 2019/4/9 19:40
 */
@Controller
@RequestMapping("/mqtt")
public class EmqAuthHttpController {

    private Logger logger = Logger.getLogger(EmqAuthHttpController.class);

    @PostMapping("/auth")
    public void mqttAuth(String clientid, String username, String password, HttpServletResponse response) {
        //auth.http.auth_req.params = clientid=%c,username=%u,password=%P
        logger.info("普通用户;clientid:" + clientid + ";username:" + username + ";password:" + password);
        /**
         * TODO 添加认证的逻辑,控制http的返回码, 这里的用户是否存在,通常是基于数据库做的。
         * HTTP 认证/鉴权 API
         * 认证/ACL 成功,API 返回200
         * 认证/ACL 失败,API 返回4xx
         */
        response.setStatus(200);
    }
    @PostMapping("/superuser")
    public void mqttSuperuser(String clientid, String username, HttpServletResponse response) {
        //auth.http.super_req.params = clientid=%c,username=%u
        logger.info("超级用户;clientid:" + clientid + ";username:" + username);
        response.setStatus(401);
    }

    @PostMapping("/acl")
    public void mqttAcl(String access, String username, String clientid, String ipaddr, String topic, HttpServletResponse response) {
        //auth.http.acl_req.params = access=%A,username=%u,clientid=%c,ipaddr=%a,topic=%t
        logger.info("access: " + access + ";username: " + username + ";clientid: " + clientid + "; ipaddr: " + ipaddr + ";topic: " + topic);
        response.setStatus(401);
    }
}

验证流程
emq首先调用auth验证身份,根据状态码判断认证是否成功,200为成功,4xx为认证失败(其中401会抛出异常密码错误,其他状态码会抛出服务不可用的异常)
如果auth验证成功,会继续调用superuser接口,验证是否为超级管理员。返回结果依旧使用状态码处理。如果为200,则不会调用后续acl验证权限。如果为4XX状态码emq无处理,并在客户端订阅或发布时调用acl接口。
acl接口验证失败则抛出 MqttException (128) 异常。

补充
如果pgsql和http都开启会怎样?
如果pgsql用户身份认证不通过,则会访问http验证。
如果pgsql用户身份认证通过,则不会访问http验证。

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容