前言
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
然后就可以使用 用户名,密码进行登录了:
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 等多种方式,直接配置下,然后加载模块就行。
在连接的时候使用 用户名,密码登录就行了。
非常方便。果然是产品支持的非常好。