mqtt(emq)HTTP的ACL認(rèn)證配置及業(yè)務(wù)場景測試

http://www.itdecent.cn/p/88acccf43931
上篇中已經(jīng)實(shí)踐了與pg庫的ACL驗(yàn)證配置
這次試試HTTP方式

1、首先修改配置文件,配置pgsql數(shù)據(jù)庫的相關(guān)信息,路徑/emqx/etc/plugins/emqx_auth_pgsql.conf

## 請(qǐng)求地址
auth.http.auth_req = http://127.0.0.1:80/mqtt/auth

## HTTP 請(qǐng)求方法
## Value: post | get | put
auth.http.auth_req.method = post

## 認(rèn)證請(qǐng)求的 HTTP 請(qǐng)求頭部,默認(rèn)情況下配置 Content-Type 頭部。
## Content-Type 頭部目前支持以下值:application/x-www-form-urlencoded,application/json
auth.http.auth_req.headers.content-type = application/x-www-form-urlencoded

## 請(qǐng)求參數(shù)
auth.http.auth_req.params = clientid=%c,username=%u,password=%P



## 請(qǐng)求地址
auth.http.super_req = http://127.0.0.1:8991/mqtt/superuser

## HTTP 請(qǐng)求方法
## Value: post | get | put
auth.http.super_req.method = post

## 請(qǐng)求參數(shù)
auth.http.super_req.params = clientid=%c,username=%u


## 請(qǐng)求地址
auth.http.acl_req = http://127.0.0.1:8991/mqtt/acl

## HTTP 請(qǐng)求方法
## Value: post | get | put
auth.http.acl_req.method = get

## 請(qǐng)求參數(shù)
auth.http.acl_req.params = access=%A,username=%u,clientid=%c,ipaddr=%a,topic=%t,mountpoint=%m

參數(shù)說明
%A:操作類型,'1' 訂閱;'2' 發(fā)布
%u:客戶端用戶名
%c:Client ID
%a:客戶端 IP 地址
%r:客戶端接入?yún)f(xié)議
%m:Mountpoint
%t:主題

有三個(gè)請(qǐng)求地址需要配置
auth:身份認(rèn)證
superuser:超級(jí)管理員認(rèn)證
acl:權(quán)限認(rèn)證

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 添加認(rèn)證的邏輯,控制http的返回碼, 這里的用戶是否存在,通常是基于數(shù)據(jù)庫做的。
         * HTTP 認(rèn)證/鑒權(quán) API
         * 認(rèn)證/ACL 成功,API 返回200
         * 認(rèn)證/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("超級(jí)用戶;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);
    }
}

驗(yàn)證流程
emq首先調(diào)用auth驗(yàn)證身份,根據(jù)狀態(tài)碼判斷認(rèn)證是否成功,200為成功,4xx為認(rèn)證失敗(其中401會(huì)拋出異常密碼錯(cuò)誤,其他狀態(tài)碼會(huì)拋出服務(wù)不可用的異常)
如果auth驗(yàn)證成功,會(huì)繼續(xù)調(diào)用superuser接口,驗(yàn)證是否為超級(jí)管理員。返回結(jié)果依舊使用狀態(tài)碼處理。如果為200,則不會(huì)調(diào)用后續(xù)acl驗(yàn)證權(quán)限。如果為4XX狀態(tài)碼emq無處理,并在客戶端訂閱或發(fā)布時(shí)調(diào)用acl接口。
acl接口驗(yàn)證失敗則拋出 MqttException (128) 異常。

補(bǔ)充
如果pgsql和http都開啟會(huì)怎樣?
如果pgsql用戶身份認(rèn)證不通過,則會(huì)訪問http驗(yàn)證。
如果pgsql用戶身份認(rèn)證通過,則不會(huì)訪問http驗(yàn)證。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容