使用 NodeJS 的 EMQX 5.x HTTP 密码认证服务器
注意:如果你还想通过 HTTP 实现 ACL 授权,请查看我们对此示例的扩展:使用 NodeJS 的 EMQX 5.x HTTP ACL 服务器
此服务器实现最小 HTTP 认证服务器。在此最小示例中,它将始终允许认证 - 你需要实现自己的逻辑来验证密码。**注意:此服务器是为 EMQX 版本 5.0 编写的,不修改将无法用于 EMQX 4.x。**有关更多信息,请参阅EMQX 5.x HTTP 认证的官方文档。
emqx_http_auth_server.js
#!/usr/bin/env node
const router = require('koa-router')();
const koaBody = require('koa-body');
const Koa = require('koa');
const app = new Koa();
app.use(koaBody());
router.post('/emqx/auth', async ctx => {
const body = ctx.request.body;
const username = body.username;
const password = body.password;
// TODO: 此示例始终返回 "allow"
// 你需要实现你的认证逻辑
ctx.body = {
result: "allow",
is_superuser: false
};
});
app.use(router.routes());
if (!module.parent) app.listen(19261);此脚本基于我们之前的文章带有路由器和 Body 解析器的最小 Koa.JS 示例,因此你可以使用以下命令安装依赖项
install_emqx_http_server_deps.sh
npm i --save koa koa-router koa-body你可以在 EMQX 仪表板中设置的请求主体配置是
emqx_request_body.json
{
"client_id": "${clientid}",
"password": "${password}",
"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