2026-06-24 f4bd1f3c89d906131495a0aca5aaf82966378510
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
package cn.iocoder.yudao.module.iot.gateway.protocol.emqx.handler.upstream;
 
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.BooleanUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.spring.SpringUtil;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.iot.core.biz.IotDeviceCommonApi;
import cn.iocoder.yudao.module.iot.core.biz.dto.IotDeviceAuthReqDTO;
import cn.iocoder.yudao.module.iot.core.enums.IotDeviceMessageMethodEnum;
import cn.iocoder.yudao.module.iot.core.mq.message.IotDeviceMessage;
import cn.iocoder.yudao.module.iot.core.topic.IotDeviceIdentity;
import cn.iocoder.yudao.module.iot.core.topic.auth.IotDeviceRegisterReqDTO;
import cn.iocoder.yudao.module.iot.core.topic.auth.IotDeviceRegisterRespDTO;
import cn.iocoder.yudao.module.iot.core.util.IotDeviceAuthUtils;
import cn.iocoder.yudao.module.iot.gateway.protocol.emqx.IotEmqxProtocol;
import cn.iocoder.yudao.module.iot.gateway.service.device.message.IotDeviceMessageService;
import cn.iocoder.yudao.module.iot.gateway.util.IotMqttTopicUtils;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.web.RoutingContext;
import lombok.extern.slf4j.Slf4j;
 
import java.util.Locale;
 
/**
 * IoT 网关 EMQX 认证事件处理器
 * <p>
 * 为 EMQX 提供 HTTP 接口服务,包括:
 * 1. 设备认证接口 - 对应 EMQX HTTP 认证插件 {@link #handleAuth(RoutingContext)}
 * 2. 设备事件处理接口 - 对应 EMQX Webhook 事件通知 {@link #handleEvent(RoutingContext)}
 * 3. 设备 ACL 权限接口 - 对应 EMQX HTTP ACL 插件 {@link #handleAcl(RoutingContext)}
 * 4. 设备注册接口 - 集成一型一密设备注册 {@link #handleDeviceRegister(RoutingContext, String, String)}
 *
 * @author 芋道源码
 */
@Slf4j
public class IotEmqxAuthEventHandler {
 
    /**
     * HTTP 成功状态码(EMQX 要求固定使用 200)
     */
    private static final int SUCCESS_STATUS_CODE = 200;
 
    /**
     * 认证允许结果
     */
    private static final String RESULT_ALLOW = "allow";
    /**
     * 认证拒绝结果
     */
    private static final String RESULT_DENY = "deny";
    /**
     * 认证忽略结果
     */
    private static final String RESULT_IGNORE = "ignore";
 
    /**
     * EMQX 事件类型常量 - 客户端连接
     */
    private static final String EVENT_CLIENT_CONNECTED = "client.connected";
    /**
     * EMQX 事件类型常量 - 客户端断开连接
     */
    private static final String EVENT_CLIENT_DISCONNECTED = "client.disconnected";
 
    /**
     * 认证类型标识 - 设备注册
     */
    private static final String AUTH_TYPE_REGISTER = "|authType=register|";
 
    private final String serverId;
 
    private final IotEmqxProtocol protocol;
 
    private final IotDeviceMessageService deviceMessageService;
    private final IotDeviceCommonApi deviceApi;
 
    public IotEmqxAuthEventHandler(String serverId, IotEmqxProtocol protocol) {
        this.serverId = serverId;
        this.protocol = protocol;
        this.deviceMessageService = SpringUtil.getBean(IotDeviceMessageService.class);
        this.deviceApi = SpringUtil.getBean(IotDeviceCommonApi.class);
    }
 
    // ========== 认证处理 ==========
 
    /**
     * EMQX 认证接口
     */
    public void handleAuth(RoutingContext context) {
        JsonObject body = null;
        try {
            // 1. 参数校验
            body = parseRequestBody(context);
            if (body == null) {
                return;
            }
            String clientId = body.getString("clientid");
            String username = body.getString("username");
            String password = body.getString("password");
            log.debug("[handleAuth][设备认证请求: clientId={}, username={}]", clientId, username);
            if (StrUtil.hasEmpty(clientId, username, password)) {
                log.info("[handleAuth][认证参数不完整: clientId={}, username={}]", clientId, username);
                sendAuthResponse(context, RESULT_DENY);
                return;
            }
 
            // 2.1 情况一:判断是否为注册请求
            if (StrUtil.endWith(clientId, AUTH_TYPE_REGISTER)) {
                handleDeviceRegister(context, username, password);
                return;
            }
 
            // 2.2 情况二:执行认证
            boolean authResult = handleDeviceAuth(clientId, username, password);
            log.info("[handleAuth][设备认证结果: {} -> {}]", username, authResult);
            if (authResult) {
                sendAuthResponse(context, RESULT_ALLOW);
            } else {
                sendAuthResponse(context, RESULT_DENY);
            }
        } catch (Exception e) {
            log.error("[handleAuth][设备认证异常][body={}]", body, e);
            sendAuthResponse(context, RESULT_IGNORE);
        }
    }
 
    /**
     * 解析认证接口请求体
     * <p>
     * 认证接口解析失败时返回 JSON 格式响应(包含 result 字段)
     *
     * @param context 路由上下文
     * @return 请求体JSON对象,解析失败时返回null
     */
    private JsonObject parseRequestBody(RoutingContext context) {
        try {
            JsonObject body = context.body().asJsonObject();
            if (body == null) {
                log.info("[parseRequestBody][请求体为空]");
                sendAuthResponse(context, RESULT_IGNORE);
                return null;
            }
            return body;
        } catch (Exception e) {
            log.error("[parseRequestBody][body({}) 解析请求体失败]", context.body().asString(), e);
            sendAuthResponse(context, RESULT_IGNORE);
            return null;
        }
    }
 
    /**
     * 执行设备认证
     *
     * @param clientId 客户端ID
     * @param username 用户名
     * @param password 密码
     * @return 认证是否成功
     */
    private boolean handleDeviceAuth(String clientId, String username, String password) {
        try {
            CommonResult<Boolean> result = deviceApi.authDevice(new IotDeviceAuthReqDTO()
                    .setClientId(clientId).setUsername(username).setPassword(password));
            result.checkError();
            return BooleanUtil.isTrue(result.getData());
        } catch (Exception e) {
            log.error("[handleDeviceAuth][设备({}) 认证接口调用失败]", username, e);
            throw e;
        }
    }
 
    /**
     * 发送 EMQX 认证响应
     * 根据 EMQX 官方文档要求,必须返回 JSON 格式响应
     *
     * @param context 路由上下文
     * @param result  认证结果:allow、deny、ignore
     */
    private void sendAuthResponse(RoutingContext context, String result) {
        // 构建符合 EMQX 官方规范的响应
        JsonObject response = new JsonObject()
                .put("result", result)
                .put("is_superuser", false);
        // 可以根据业务需求添加客户端属性
        // response.put("client_attrs", new JsonObject().put("role", "device"));
        // 可以添加认证过期时间(可选)
        // response.put("expire_at", System.currentTimeMillis() / 1000 + 3600);
 
        // 回复响应
        context.response()
                .setStatusCode(SUCCESS_STATUS_CODE)
                .putHeader("Content-Type", "application/json; charset=utf-8")
                .end(response.encode());
    }
 
    // ========== ACL 处理 ==========
 
    /**
     * EMQX ACL 接口
     * <p>
     * 用于 EMQX 的 HTTP ACL 插件校验设备的 publish/subscribe 权限。
     * 若请求参数无法识别,则返回 ignore 交给 EMQX 自身 ACL 规则处理。
     */
    public void handleAcl(RoutingContext context) {
        JsonObject body = null;
        try {
            // 1.1 解析请求体
            body = parseRequestBody(context);
            if (body == null) {
                return;
            }
            String username = body.getString("username");
            String topic = body.getString("topic");
            if (StrUtil.hasBlank(username, topic)) {
                log.info("[handleAcl][ACL 参数不完整: username={}, topic={}]", username, topic);
                sendAuthResponse(context, RESULT_IGNORE);
                return;
            }
            // 1.2 解析设备身份
            IotDeviceIdentity deviceInfo = IotDeviceAuthUtils.parseUsername(username);
            if (deviceInfo == null) {
                sendAuthResponse(context, RESULT_IGNORE);
                return;
            }
            // 1.3 解析 ACL 动作(兼容多种 EMQX 版本/插件字段)
            Boolean subscribe = parseAclSubscribeFlag(body);
            if (subscribe == null) {
                sendAuthResponse(context, RESULT_IGNORE);
                return;
            }
 
            // 2. 执行 ACL 校验
            boolean allowed = subscribe
                    ? IotMqttTopicUtils.isTopicSubscribeAllowed(topic, deviceInfo.getProductKey(), deviceInfo.getDeviceName())
                    : IotMqttTopicUtils.isTopicPublishAllowed(topic, deviceInfo.getProductKey(), deviceInfo.getDeviceName());
            sendAuthResponse(context, allowed ? RESULT_ALLOW : RESULT_DENY);
        } catch (Exception e) {
            log.error("[handleAcl][ACL 处理失败][body={}]", body, e);
            sendAuthResponse(context, RESULT_IGNORE);
        }
    }
 
    /**
     * 解析 ACL 动作类型:订阅/发布
     *
     * @param body ACL 请求体
     * @return true 订阅;false 发布;null 不识别
     */
    private static Boolean parseAclSubscribeFlag(JsonObject body) {
        // 1. action 字段(常见为 publish/subscribe)
        String action = body.getString("action");
        if (StrUtil.isNotBlank(action)) {
            String lower = action.toLowerCase(Locale.ROOT);
            if (lower.contains("sub")) {
                return true;
            }
            if (lower.contains("pub")) {
                return false;
            }
        }
 
        // 2. access 字段:可能是数字或字符串
        Integer access = body.getInteger("access");
        if (access != null) {
            if (access == 1) {
                return true;
            }
            if (access == 2) {
                return false;
            }
        }
        String accessText = body.getString("access");
        if (StrUtil.isNotBlank(accessText)) {
            String lower = accessText.toLowerCase(Locale.ROOT);
            if (lower.contains("sub")) {
                return true;
            }
            if (lower.contains("pub")) {
                return false;
            }
            if (StrUtil.isNumeric(accessText)) {
                int value = Integer.parseInt(accessText);
                if (value == 1) {
                    return true;
                }
                if (value == 2) {
                    return false;
                }
            }
        }
        return null;
    }
 
    // ========== 事件处理 ==========
 
    /**
     * EMQX 统一事件处理接口:根据 EMQX 官方 Webhook 设计,统一处理所有客户端事件
     * 支持的事件类型:client.connected、client.disconnected 等
     */
    public void handleEvent(RoutingContext context) {
        JsonObject body = null;
        try {
            // 1. 解析请求体
            body = parseEventRequestBody(context);
            if (body == null) {
                return;
            }
            String event = body.getString("event");
            String username = body.getString("username");
            log.debug("[handleEvent][收到事件: {} - {}]", event, username);
 
            // 2. 根据事件类型进行分发处理
            switch (event) {
                case EVENT_CLIENT_CONNECTED:
                    handleClientConnected(body);
                    break;
                case EVENT_CLIENT_DISCONNECTED:
                    handleClientDisconnected(body);
                    break;
                default:
                    break;
            }
 
            // 3. EMQX Webhook 只需要 200 状态码,无需响应体
            context.response().setStatusCode(SUCCESS_STATUS_CODE).end();
        } catch (Exception e) {
            log.error("[handleEvent][事件处理失败][body={}]", body, e);
            // 即使处理失败,也返回 200 避免 EMQX 重试
            context.response().setStatusCode(SUCCESS_STATUS_CODE).end();
        }
    }
 
    /**
     * 解析事件接口请求体
     * <p>
     * 事件接口解析失败时仅返回 200 状态码,无响应体(符合 EMQX Webhook 规范)
     *
     * @param context 路由上下文
     * @return 请求体JSON对象,解析失败时返回null
     */
    private JsonObject parseEventRequestBody(RoutingContext context) {
        try {
            JsonObject body = context.body().asJsonObject();
            if (body == null) {
                log.info("[parseEventRequestBody][请求体为空]");
                context.response().setStatusCode(SUCCESS_STATUS_CODE).end();
                return null;
            }
            return body;
        } catch (Exception e) {
            log.error("[parseEventRequestBody][body({}) 解析请求体失败]", context.body().asString(), e);
            context.response().setStatusCode(SUCCESS_STATUS_CODE).end();
            return null;
        }
    }
 
    /**
     * 处理客户端连接事件
     */
    private void handleClientConnected(JsonObject body) {
        String username = body.getString("username");
        log.info("[handleClientConnected][设备上线: {}]", username);
        handleDeviceStateChange(username, true);
    }
 
    /**
     * 处理客户端断开连接事件
     */
    private void handleClientDisconnected(JsonObject body) {
        String username = body.getString("username");
        String reason = body.getString("reason");
        log.info("[handleClientDisconnected][设备下线: {} ({})]", username, reason);
        handleDeviceStateChange(username, false);
    }
 
    /**
     * 处理设备状态变化
     *
     * @param username 用户名
     * @param online   是否在线 true 在线 false 离线
     */
    private void handleDeviceStateChange(String username, boolean online) {
        // 1. 解析设备信息
        IotDeviceIdentity deviceInfo = IotDeviceAuthUtils.parseUsername(username);
        if (deviceInfo == null) {
            log.debug("[handleDeviceStateChange][跳过非设备({})连接]", username);
            return;
        }
 
        try {
            // 2. 构建设备状态消息
            IotDeviceMessage message = online ? IotDeviceMessage.buildStateUpdateOnline()
                    : IotDeviceMessage.buildStateOffline();
 
            // 3. 发送设备状态消息
            deviceMessageService.sendDeviceMessage(message,
                    deviceInfo.getProductKey(), deviceInfo.getDeviceName(), serverId);
        } catch (Exception e) {
            log.error("[handleDeviceStateChange][发送设备状态消息失败: {}]", username, e);
        }
    }
 
    // ========= 注册处理 =========
 
    /**
     * 处理设备注册请求(一型一密)
     *
     * @param context  路由上下文
     * @param username 用户名
     * @param password 密码(签名)
     */
    private void handleDeviceRegister(RoutingContext context, String username, String password) {
        try {
            // 1. 解析设备信息
            IotDeviceIdentity deviceInfo = IotDeviceAuthUtils.parseUsername(username);
            if (deviceInfo == null) {
                log.warn("[handleDeviceRegister][设备注册失败: 无法解析 username={}]", username);
                sendAuthResponse(context, RESULT_DENY);
                return;
            }
 
            // 2. 调用注册 API
            IotDeviceRegisterReqDTO params = new IotDeviceRegisterReqDTO()
                    .setProductKey(deviceInfo.getProductKey())
                    .setDeviceName(deviceInfo.getDeviceName())
                    .setSign(password);
            CommonResult<IotDeviceRegisterRespDTO> result = deviceApi.registerDevice(params);
            result.checkError();
 
            // 3. 允许连接
            log.info("[handleDeviceRegister][设备注册成功: {}]", username);
            sendAuthResponse(context, RESULT_ALLOW);
 
            // 4. 延迟 5 秒发送注册结果(等待设备连接成功并完成订阅)
            sendRegisterResultMessage(username, result.getData());
        } catch (Exception e) {
            log.warn("[handleDeviceRegister][设备注册失败: {}, 错误: {}]", username, e.getMessage());
            sendAuthResponse(context, RESULT_DENY);
        }
    }
 
    /**
     * 发送注册结果消息给设备
     * <p>
     * 注意:延迟 5 秒发送,等待设备连接成功并完成订阅。
     *
     * @param username 用户名
     * @param result   注册结果
     */
    @SuppressWarnings("DataFlowIssue")
    private void sendRegisterResultMessage(String username, IotDeviceRegisterRespDTO result) {
        IotDeviceIdentity deviceInfo = IotDeviceAuthUtils.parseUsername(username);
        Assert.notNull(deviceInfo, "设备信息不能为空");
        try {
            // 1.1 构建响应消息
            String method = IotDeviceMessageMethodEnum.DEVICE_REGISTER.getMethod();
            IotDeviceMessage responseMessage = IotDeviceMessage.replyOf(null, method, result, 0, null);
            // 1.2 序列化消息
            byte[] encodedData = deviceMessageService.serializeDeviceMessage(responseMessage,
                    cn.iocoder.yudao.module.iot.core.enums.IotSerializeTypeEnum.JSON);
            // 1.3 构建响应主题
            String replyTopic = IotMqttTopicUtils.buildTopicByMethod(method,
                    deviceInfo.getProductKey(), deviceInfo.getDeviceName(), true);
 
            // 2. 构建响应主题,并延迟发布(等待设备连接成功并完成订阅)
            protocol.publishDelayMessage(replyTopic, encodedData, 5000);
            log.info("[sendRegisterResultMessage][发送注册结果: topic={}]", replyTopic);
        } catch (Exception e) {
            log.error("[sendRegisterResultMessage][发送注册结果失败: {}]", username, e);
        }
    }
 
}