MQTT server EMQX settings login verification and client offline notification

overview

In the process of using pythonand developing, MQTTthe message subscription mechanism is used to realize business requirements. One of them is that clients who subscribe to messages need to receive 上线与下线push messages about other clients' MQTT servers. Here is a record of the implementation steps and precautions.

Operating system platform and software version :

  • operating system:Windows 10
  • emqx version:emqx 4.3

The main steps to realize are :

  1. Change the client subscription verification mechanism, that is, 是否允许匿名登录from allow to disallow
  2. Create an administrator user name and password, and give it the system instant message subscription permission (client log-off and offline notifications are system-level messages)
  3. Start the user name and password verification plug-in function

The main considerations :

  • emqxThe version difference ( emqx 4.3the version 4.2is different from the setting method of the previous version)
  • After the modification of the configuration file is completed, the eqmx server needs to be restarted to take effect

Operating procedures

emqx 4.2and previous versions

emqx 4.2How to change the login user name and password in previous versions can be retrieved through search engines. There are a large number of documents or technical blogs with instructions, so I won’t repeat them here.

emqx 4.3 version

Modify anonymous login authentication settings

Assuming that emqx is installed on D:\the disk, the installation tutorial , at this time, open the system configuration file, located in d:\emqx\etc\emqx.conf, open in text mode, modify the configuration item allow_anonymousas false, before modification, the system default value true:

## Allow anonymous authentication by default if no auth plugins loaded.
## Notice: Disable the option in production deployment!
##
## Value: true | false
allow_anonymous = false

After modification, save and exit.

Open the login verification plugin

Log in 127.0.0.1:18083, enter the default account name adminand default password publicto log in to the console, click on the left navigation bar Plugins(the Chinese interface shows is 插件), and emqx_auth_mnesiastart

Before the Emqx4.3 version, there was an emqx_auth_usernameextension, which could emqx_auth_usernamemodify its configuration by starting the plug-in module to verify the account and password.
But Emqx 4.3it is different after the version. The official document indicates that emqx 4.3in the version emqx_auth_clientidand emqx_auth_usernmaeis merged into emqx_auth_mnesia. emqx_auth_usernameThe module is henceforth obsolete. Emqx 4.3Loading the plug-in in the version emq_auth_usernamewill report an error that does not exist, so don't look for the installation emqx_auth_usernamemodule anymore. Directly modify emqx_auth_mnesia.confthe module configuration file and add the account password.

Add username and password

Find emqx_auth_mnesiathe configuration file corresponding to the plug-in, located in D:\emqx\etc\plugins\emqx_auth_mnesia.conf, add the following username and password:

## Password hash.
##
## Value: plain | md5 | sha | sha256 | sha512
auth.mnesia.password_hash = sha256

##--------------------------------------------------------------------
## ClientId Authentication
##--------------------------------------------------------------------

auth.client.1.clientid = admin
auth.client.1.password = your_own_password

##--------------------------------------------------------------------
## Username Authentication
##--------------------------------------------------------------------

## Examples:
auth.user.1.username = admin
auth.user.1.password = your_own_password

Save and exit.

Modify access control configuration files

Open d:\emqx\etc\acl.conf, add a line of user access configuration:

{allow, {user, "admin"}, subscribe, ["$SYS/#"]}.

Allows adminusers to subscribe to system-level messages. Save and exit, restart emqxthe service

System message subscription for client online and offline

Client offline and online messages are system-level notifications, which require the client to subscribe to the system's predefined topic:

  • offline topic$SYS/brokers/+/clients/+/disconnected

  • online topic$SYS/brokers/+/clients/+/connected

  • Online and offline topic$SYS/brokers/+/clients/#

Sample code:

def on_connect(client, userdata, flags, rc):
	# this method will be called when client connected to server successfully
	# TODO do something when this client been notified about successfully connected to server
    pass


def on_message(client, userdata, msg: mqtt.MQTTMessage):
	# this method will be called when this client get a message under the topic(s) it subscribed, encluding the system message (I assuming you've already configure it properly)
	# TODO do something when this client get a message
    pass


mqtt_server = "127.0.0.1"
client = mqtt.Client("surveillance_client")
client.username_pw_set("admin", "aoto@123")
# 定义回调方法
client.on_connect = on_connect
client.on_message = on_message
# 600为keepalive的时间间隔
client.connect(mqtt_server, 1883, 600)
client.subscribe('surveillance', qos=0)
client.subscribe(r"$SYS/brokers/+/clients/#", qos=0)
# 启动mqtt消息订阅(非阻塞式)
client.loop_start()

After the above code runs, if other clients go online or offline, on_message()the method is called back here, and the system log-off message is recovered, as follows:

客户端上线 topic:  $SYS/brokers/[email protected]/clients/654321/connected
客户端上线消息报文 :
{
  "username": "admin",
  "ts": 1627476021893,
  "sockport": 1883,
  "proto_ver": 4,
  "proto_name": "MQTT",
  "keepalive": 600,
  "ipaddress": "127.0.0.1",
  "expiry_interval": 0,
  "connected_at": 1627476021893,
  "connack": 0,
  "clientid": "654321",
  "clean_start": true
}

客户端下线 topic:  $SYS/brokers/[email protected]/clients/654321/disconnected
客户端下线消息报文 :

{
  "username": "admin",
  "ts": 1627476028659,
  "reason": "tcp_closed",
  "disconnected_at": 1627476028659,
  "clientid": "654321"
}

Regular expressions can be used r"^\$SYS\/brokers\/.*\/clients\/.*\/(dis)?connected"to match and filter online and offline messages for business logic processing.

Guess you like

Origin blog.csdn.net/LJX_ahut/article/details/119189984