使用 NodeJS 的 EMQX 5.x HTTP ACL 服务器
在我们之前的文章使用 NodeJS 的 EMQX 5.x HTTP 密码认证服务器最小示例中,我们提供了如何实现 EMQX HTTP 认证的完整示例。
本文通过添加 Koa 路由器(即 HTTP 端点/URL)来提供之前 HTTP 认证的扩展,以提供 ACL 认证,即使用自定义逻辑允许或拒绝主题级别访问。
emqx-5-x-http-acl-server-using-nodejs.js
router.post('/emqx/acl', async ctx => {
const body = ctx.request.body;
console.log(body)
// TODO: 此示例始终返回 true
// 你需要实现你的认证逻辑
ctx.body = {
result: "allow",
};
});在 使用 NodeJS 的 EMQX 5.x HTTP 密码认证服务器最小示例中的脚本中的 app.use(router.routes()); 之前添加该代码。
我推荐的授权配置主体,你可以在 EMQX 仪表板中设置,是
emqx_acl_request_body.json
{
"action": "${action}",
"client_id": "${clientid}",
"ip": "${peerhost}",
"topic": "${topic}",
"username": "${username}"
}If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow