EMQX 5.x HTTP-Passwort-Authentifizierungsserver mit NodeJS

English Deutsch

Hinweis: Wenn Sie auch ACL-Autorisierung via HTTP implementieren möchten, siehe unsere Erweiterung dieses Beispiels: EMQX 5.x HTTP ACL-Server mit NodeJS

Dieser Server implementiert einen minimalen HTTP-Authentifizierungsserver. In diesem minimalen Beispiel wird er immer Authentifizierung erlauben – Sie müssen Ihre eigene Logik zur Passwortverifizierung implementieren. Hinweis: Dieser Server ist für EMQX-Version 5.0 geschrieben und funktioniert nicht für EMQX 4.x ohne Modifikation. Siehe die offizielle Dokumentation zur EMQX 5.x HTTP-Authentifizierung für weitere Informationen.

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: This example always returns "allow"
    // You need to implement your authentication logic
    ctx.body = {
        result: "allow",
        is_superuser: false
    };
});

app.use(router.routes());

if (!module.parent) app.listen(19261);

Dieses Skript basiert auf unserem vorherigen Post Minimales Koa.JS-Beispiel mit Router & Body-Parser, daher können Sie die Abhängigkeiten installieren mit

install_emqx_http_server_deps.sh
npm i --save koa koa-router koa-body

Die Request-Body-Konfiguration, die Sie im EMQX-Dashboard setzen können, ist

emqx_request_body.json
{
  "client_id": "${clientid}",
  "password": "${password}",
  "username": "${username}"
}

Check out similar posts by category: EMQX, MQTT, NodeJS