emqtt mysql,mqtt(7):emqtt 配置 auth 使用 redis 进行用户,acl 校验-Go语言中文社区...

本文介绍了如何在Emqx中配置不同类型的认证,如固定文件、Redis、MySQL等,并展示了通过Java客户端实现的登录过程。展示了Emqx支持的多样化认证方式,方便用户根据需求选择。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前言

0,配置用户名密码

测试启动配置匿名,可以不用用户名密码进行登录。

mqtt.allow_anonymous = true

认证插件有很多:

认证插件配置文件说明

emqx_auth_clientid ClientId认证/鉴权插件

emqx_auth_username 用户名密码认证/鉴权插件

emqx_auth_ldap LDAP认证/鉴权插件

emqx_auth_http HTTP认证/鉴权插件

emqx_auth_mysql MySQL认证/鉴权插件

emqx_auth_pgsql Postgre认证/鉴权插件

emqx_auth_redis Redis认证/鉴权插件

emqx_auth_mongo MongoDB认证/鉴权插件

emqx_auth_jwt JWT认证/鉴权插件

比如用户密码的 配置:

auth.user.1.username = user001

auth.user.1.password = 123456

然后在加载插件:

./bin/emqx_ctl plugins load emqx_auth_username

同样的道理可以配置mysql 的数据库 连接,然后创建一个 表:

CREATE TABLE `mqtt_user` (

`id` int(11) unsigned NOT NULL AUTO_INCREMENT,

`username` varchar(100) DEFAULT NULL,

`password` varchar(100) DEFAULT NULL,

`salt` varchar(100) DEFAULT NULL,

`is_superuser` tinyint(1) DEFAULT 0,

`created` datetime DEFAULT NULL,

PRIMARY KEY (`id`),

UNIQUE KEY `mqtt_username` (`username`)

) ENGINE=MyISAM DEFAULT CHARSET=utf8;

1,关于配置 redis 用户登录

配置auth:

比如使用 redis 进行配置:

auth.redis.server = 127.0.0.1:6379

auth.redis.password_hash = plain

配置 redis server 服务器,设置密码是个明文加密。

然后往 redis 配置

设置super 权限,可以订阅任何 topic。

HSET mqtt_user:user001 is_superuser 1

HSET mqtt_user:user001 password "123456"

设置非管理员,只可以订阅 test 的topic。

HSET mqtt_user:user001 is_superuser 0

HSET mqtt_acl:user001 test 3

加载插件:

./bin/emqx_ctl plugins load emqx_auth_redis

然后就可以使用 用户名,密码进行登录了:

b878203594eec8931b37130b8f33fd9c.png

2,使用java client

使用标准的java client 进行登录。设置连接用户名,密码进行登录。

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

4.0.0

com.cloudmqtt

1.0-SNAPSHOT

com.cloudmqtt.example

org.eclipse.paho

org.eclipse.paho.client.mqttv3

1.2.0

org.apache.maven.plugins

maven-dependency-plugin

2.4

copy-dependencies

package

copy-dependencies

org.apache.maven.plugins

maven-compiler-plugin

1.8

1.8

java 代码:

import org.eclipse.paho.client.mqttv3.*;

import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;

import java.net.URI;

import java.net.URISyntaxException;

/**

* A sample application that demonstrates how to use the Paho MQTT v3.1 Client blocking API.

*/

public class Subscriber implements MqttCallback {

private final int qos = 1;

private String topic = "test";

private MqttClient client;

public Subscriber(String uri) throws MqttException, URISyntaxException {

this(new URI(uri));

}

public Subscriber(URI uri) throws MqttException {

String host = String.format("tcp://%s:%d", uri.getHost(), uri.getPort());

String[] auth = this.getAuth(uri);

String username = auth[0];

String password = auth[1];

String clientId = "MQTT-Java-Example";

if (!uri.getPath().isEmpty()) {

this.topic = uri.getPath().substring(1);

}

MqttConnectOptions conOpt = new MqttConnectOptions();

conOpt.setCleanSession(true);

conOpt.setUserName(username);

conOpt.setPassword(password.toCharArray());

this.client = new MqttClient(host, clientId, new MemoryPersistence());

this.client.setCallback(this);

this.client.connect(conOpt);

this.client.subscribe(this.topic, qos);

}

private String[] getAuth(URI uri) {

System.out.println(uri);

String a = uri.getAuthority();

String[] first = a.split("@");

String[] aa = first[0].split(":");

System.out.println(aa[0]);

return aa;

}

public void sendMessage(String payload) throws MqttException {

MqttMessage message = new MqttMessage(payload.getBytes());

message.setQos(qos);

this.client.publish(this.topic, message); // Blocking publish

}

/**

* @see MqttCallback#connectionLost(Throwable)

*/

public void connectionLost(Throwable cause) {

System.out.println("Connection lost because: " + cause);

System.exit(1);

}

/**

* @see MqttCallback#deliveryComplete(IMqttDeliveryToken)

*/

public void deliveryComplete(IMqttDeliveryToken token) {

}

/**

* @see MqttCallback#messageArrived(String, MqttMessage)

*/

public void messageArrived(String topic, MqttMessage message) throws MqttException {

System.out.println(String.format("[%s] %s", topic, new String(message.getPayload())));

}

public static void main(String[] args) throws MqttException, URISyntaxException {

//mqtt[s]://[username][:password]@host.domain[:port]

Subscriber s = new Subscriber("tcp://user001:123456@127.0.0.1:1883");

s.sendMessage("Hello");

s.sendMessage("Hello 2");

}

}

3,总结

emqx 支持好多种认证的方式,有写死配置文件的。有写redis的。

有些在 mysql 数据库的。还有支持 ldap http jwt 等多种方式,直接配置下,然后加载模块就行。

在连接的时候使用 用户名,密码登录就行了。

非常方便。果然是产品支持的非常好。

fac7641e84ac8a7ea031e74b616a0049.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值