http://www.reibang.com/p/88acccf43931
上篇中已經(jīng)實(shí)踐了與pg庫的ACL驗(yàn)證配置
這次試試HTTP方式
1贮竟、首先修改配置文件,配置pgsql數(shù)據(jù)庫的相關(guān)信息,路徑/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
## 認(rèn)證請求的 HTTP 請求頭部,默認(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
## 請求參數(shù)
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
## 請求參數(shù)
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
## 請求參數(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:主題
有三個請求地址需要配置
auth:身份認(rèn)證
superuser:超級管理員認(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("超級用戶钉疫;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會拋出異常密碼錯誤,其他狀態(tài)碼會拋出服務(wù)不可用的異常)
如果auth驗(yàn)證成功城菊,會繼續(xù)調(diào)用superuser接口备燃,驗(yàn)證是否為超級管理員。返回結(jié)果依舊使用狀態(tài)碼處理凌唬。如果為200并齐,則不會調(diào)用后續(xù)acl驗(yàn)證權(quán)限。如果為4XX狀態(tài)碼emq無處理客税,并在客戶端訂閱或發(fā)布時調(diào)用acl接口况褪。
acl接口驗(yàn)證失敗則拋出 MqttException (128) 異常。
補(bǔ)充
如果pgsql和http都開啟會怎樣更耻?
如果pgsql用戶身份認(rèn)證不通過测垛,則會訪問http驗(yàn)證。
如果pgsql用戶身份認(rèn)證通過秧均,則不會訪問http驗(yàn)證食侮。