diff --git a/yudao-module-iot/yudao-module-iot-gateway/src/main/java/cn/iocoder/yudao/module/iot/gateway/config/IotGatewayConfiguration.java b/yudao-module-iot/yudao-module-iot-gateway/src/main/java/cn/iocoder/yudao/module/iot/gateway/config/IotGatewayConfiguration.java
index 6b47d0df53..a4e93a84fd 100644
--- a/yudao-module-iot/yudao-module-iot-gateway/src/main/java/cn/iocoder/yudao/module/iot/gateway/config/IotGatewayConfiguration.java
+++ b/yudao-module-iot/yudao-module-iot-gateway/src/main/java/cn/iocoder/yudao/module/iot/gateway/config/IotGatewayConfiguration.java
@@ -44,9 +44,15 @@ public class IotGatewayConfiguration {
@Slf4j
public static class HttpProtocolConfiguration {
+ @Bean(name = "httpVertx", destroyMethod = "close")
+ public Vertx httpVertx() {
+ return Vertx.vertx();
+ }
+
@Bean
- public IotHttpUpstreamProtocol iotHttpUpstreamProtocol(IotGatewayProperties gatewayProperties) {
- return new IotHttpUpstreamProtocol(gatewayProperties.getProtocol().getHttp());
+ public IotHttpUpstreamProtocol iotHttpUpstreamProtocol(IotGatewayProperties gatewayProperties,
+ @Qualifier("httpVertx") Vertx httpVertx) {
+ return new IotHttpUpstreamProtocol(gatewayProperties.getProtocol().getHttp(), httpVertx);
}
@Bean
diff --git a/yudao-module-iot/yudao-module-iot-gateway/src/main/java/cn/iocoder/yudao/module/iot/gateway/protocol/coap/router/IotCoapUpstreamHandler.java b/yudao-module-iot/yudao-module-iot-gateway/src/main/java/cn/iocoder/yudao/module/iot/gateway/protocol/coap/router/IotCoapUpstreamHandler.java
index 4af4000d7e..d33eb464bb 100644
--- a/yudao-module-iot/yudao-module-iot-gateway/src/main/java/cn/iocoder/yudao/module/iot/gateway/protocol/coap/router/IotCoapUpstreamHandler.java
+++ b/yudao-module-iot/yudao-module-iot-gateway/src/main/java/cn/iocoder/yudao/module/iot/gateway/protocol/coap/router/IotCoapUpstreamHandler.java
@@ -1,7 +1,6 @@
package cn.iocoder.yudao.module.iot.gateway.protocol.coap.router;
import cn.hutool.core.collection.CollUtil;
-import cn.hutool.core.lang.Assert;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.text.StrPool;
import cn.hutool.core.util.ArrayUtil;
@@ -93,7 +92,10 @@ public class IotCoapUpstreamHandler {
// 2.2 解码消息
IotDeviceMessage message = deviceMessageService.decodeDeviceMessage(payload, productKey, deviceName);
- Assert.equals(method, message.getMethod(), "method 不匹配");
+ if (ObjUtil.notEqual(method, message.getMethod())) {
+ IotCoapUtils.respondError(exchange, CoAP.ResponseCode.BAD_REQUEST, "method 不匹配");
+ return;
+ }
// 2.3 发送消息到消息总线
deviceMessageService.sendDeviceMessage(message, productKey, deviceName, protocol.getServerId());
diff --git a/yudao-module-iot/yudao-module-iot-gateway/src/main/java/cn/iocoder/yudao/module/iot/gateway/protocol/emqx/router/IotEmqxAuthEventHandler.java b/yudao-module-iot/yudao-module-iot-gateway/src/main/java/cn/iocoder/yudao/module/iot/gateway/protocol/emqx/router/IotEmqxAuthEventHandler.java
index 3395d5c8ae..6b6694fd90 100644
--- a/yudao-module-iot/yudao-module-iot-gateway/src/main/java/cn/iocoder/yudao/module/iot/gateway/protocol/emqx/router/IotEmqxAuthEventHandler.java
+++ b/yudao-module-iot/yudao-module-iot-gateway/src/main/java/cn/iocoder/yudao/module/iot/gateway/protocol/emqx/router/IotEmqxAuthEventHandler.java
@@ -104,7 +104,7 @@ public class IotEmqxAuthEventHandler {
JsonObject body = null;
try {
// 1. 解析请求体
- body = parseRequestBody(context);
+ body = parseEventRequestBody(context);
if (body == null) {
return;
}
@@ -153,7 +153,9 @@ public class IotEmqxAuthEventHandler {
}
/**
- * 解析请求体
+ * 解析认证接口请求体
+ *
+ * 认证接口解析失败时返回 JSON 格式响应(包含 result 字段)
*
* @param context 路由上下文
* @return 请求体JSON对象,解析失败时返回null
@@ -174,6 +176,30 @@ public class IotEmqxAuthEventHandler {
}
}
+ /**
+ * 解析事件接口请求体
+ *
+ * 事件接口解析失败时仅返回 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;
+ }
+ }
+
/**
* 执行设备认证
*
diff --git a/yudao-module-iot/yudao-module-iot-gateway/src/main/java/cn/iocoder/yudao/module/iot/gateway/protocol/http/IotHttpUpstreamProtocol.java b/yudao-module-iot/yudao-module-iot-gateway/src/main/java/cn/iocoder/yudao/module/iot/gateway/protocol/http/IotHttpUpstreamProtocol.java
index a9ba930f1d..54cb2da1f4 100644
--- a/yudao-module-iot/yudao-module-iot-gateway/src/main/java/cn/iocoder/yudao/module/iot/gateway/protocol/http/IotHttpUpstreamProtocol.java
+++ b/yudao-module-iot/yudao-module-iot-gateway/src/main/java/cn/iocoder/yudao/module/iot/gateway/protocol/http/IotHttpUpstreamProtocol.java
@@ -6,7 +6,6 @@ import cn.iocoder.yudao.module.iot.gateway.protocol.http.router.IotHttpAuthHandl
import cn.iocoder.yudao.module.iot.gateway.protocol.http.router.IotHttpRegisterHandler;
import cn.iocoder.yudao.module.iot.gateway.protocol.http.router.IotHttpRegisterSubHandler;
import cn.iocoder.yudao.module.iot.gateway.protocol.http.router.IotHttpUpstreamHandler;
-import io.vertx.core.AbstractVerticle;
import io.vertx.core.Vertx;
import io.vertx.core.http.HttpServer;
import io.vertx.core.http.HttpServerOptions;
@@ -24,25 +23,26 @@ import lombok.extern.slf4j.Slf4j;
* @author 芋道源码
*/
@Slf4j
-public class IotHttpUpstreamProtocol extends AbstractVerticle {
+public class IotHttpUpstreamProtocol {
private final IotGatewayProperties.HttpProperties httpProperties;
+ private final Vertx vertx;
+
private HttpServer httpServer;
@Getter
private final String serverId;
- public IotHttpUpstreamProtocol(IotGatewayProperties.HttpProperties httpProperties) {
+ public IotHttpUpstreamProtocol(IotGatewayProperties.HttpProperties httpProperties, Vertx vertx) {
this.httpProperties = httpProperties;
+ this.vertx = vertx;
this.serverId = IotDeviceMessageUtils.generateServerId(httpProperties.getServerPort());
}
- @Override
@PostConstruct
public void start() {
// 创建路由
- Vertx vertx = Vertx.vertx();
Router router = Router.router(vertx);
router.route().handler(BodyHandler.create());
@@ -76,7 +76,6 @@ public class IotHttpUpstreamProtocol extends AbstractVerticle {
}
}
- @Override
@PreDestroy
public void stop() {
if (httpServer != null) {
diff --git a/yudao-module-iot/yudao-module-iot-gateway/src/main/java/cn/iocoder/yudao/module/iot/gateway/protocol/http/router/IotHttpAuthHandler.java b/yudao-module-iot/yudao-module-iot-gateway/src/main/java/cn/iocoder/yudao/module/iot/gateway/protocol/http/router/IotHttpAuthHandler.java
index c6a9331ab6..148756ca8b 100644
--- a/yudao-module-iot/yudao-module-iot-gateway/src/main/java/cn/iocoder/yudao/module/iot/gateway/protocol/http/router/IotHttpAuthHandler.java
+++ b/yudao-module-iot/yudao-module-iot-gateway/src/main/java/cn/iocoder/yudao/module/iot/gateway/protocol/http/router/IotHttpAuthHandler.java
@@ -51,6 +51,9 @@ public class IotHttpAuthHandler extends IotHttpAbstractHandler {
public CommonResult