From a0a26c3d64fe25465b7367a6f8b5c43c1013d6d8 Mon Sep 17 00:00:00 2001 From: YunaiV Date: Tue, 3 Jun 2025 22:27:04 +0800 Subject: [PATCH] =?UTF-8?q?feat=EF=BC=9A=E3=80=90IoT=20=E7=89=A9=E8=81=94?= =?UTF-8?q?=E7=BD=91=E3=80=91=E5=A2=9E=E5=8A=A0=E7=BD=91=E5=85=B3=20HTTP?= =?UTF-8?q?=20=E5=8D=8F=E8=AE=AE=E7=9A=84=E9=89=B4=E6=9D=83=EF=BC=8C?= =?UTF-8?q?=E5=9F=BA=E4=BA=8E=20JWT=20=E8=BD=BB=E9=87=8F=E7=BA=A7=EF=BC=88?= =?UTF-8?q?=E5=B7=B2=E6=B5=8B=E8=AF=95=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/handler/GlobalExceptionHandler.java | 2 +- .../module/iot/api/device/IoTDeviceApiImpl.java | 3 ++- .../iot/service/device/IotDeviceService.java | 2 +- .../http/router/IotHttpAuthHandler.java | 17 +++++++++++------ .../device/IotDeviceClientServiceImpl.java | 6 ++++-- 5 files changed, 19 insertions(+), 11 deletions(-) diff --git a/yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/web/core/handler/GlobalExceptionHandler.java b/yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/web/core/handler/GlobalExceptionHandler.java index e27d04ec68..11ef9a8ef6 100644 --- a/yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/web/core/handler/GlobalExceptionHandler.java +++ b/yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/web/core/handler/GlobalExceptionHandler.java @@ -179,7 +179,7 @@ public class GlobalExceptionHandler { if(ex.getCause() instanceof InvalidFormatException) { InvalidFormatException invalidFormatException = (InvalidFormatException) ex.getCause(); return CommonResult.error(BAD_REQUEST.getCode(), String.format("请求参数类型错误:%s", invalidFormatException.getValue())); - }else { + } else { return defaultExceptionHandler(ServletUtils.getRequest(), ex); } } diff --git a/yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/api/device/IoTDeviceApiImpl.java b/yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/api/device/IoTDeviceApiImpl.java index d4e5b3774f..3e5008c5aa 100644 --- a/yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/api/device/IoTDeviceApiImpl.java +++ b/yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/api/device/IoTDeviceApiImpl.java @@ -10,6 +10,7 @@ import jakarta.annotation.security.PermitAll; import org.springframework.context.annotation.Primary; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RestController; import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success; @@ -30,7 +31,7 @@ public class IoTDeviceApiImpl implements IotDeviceCommonApi { @Override @PostMapping(RpcConstants.RPC_API_PREFIX + "/iot/device/auth") @PermitAll - public CommonResult authDevice(IotDeviceAuthReqDTO authReqDTO) { + public CommonResult authDevice(@RequestBody IotDeviceAuthReqDTO authReqDTO) { return success(deviceService.authDevice(authReqDTO)); } diff --git a/yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/device/IotDeviceService.java b/yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/device/IotDeviceService.java index 8971820194..e2aa21304f 100644 --- a/yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/device/IotDeviceService.java +++ b/yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/device/IotDeviceService.java @@ -235,6 +235,6 @@ public interface IotDeviceService { * @param authReqDTO 认证信息 * @return 是否认证成功 */ - boolean authDevice(IotDeviceAuthReqDTO authReqDTO); + boolean authDevice(@Valid IotDeviceAuthReqDTO authReqDTO); } 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 1e65d645ce..8c59e6a270 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 @@ -2,11 +2,13 @@ package cn.iocoder.yudao.module.iot.gateway.protocol.http.router; import cn.hutool.core.lang.Assert; import cn.hutool.core.map.MapUtil; +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.mq.message.IotDeviceMessage; import cn.iocoder.yudao.module.iot.core.mq.producer.IotDeviceMessageProducer; import cn.iocoder.yudao.module.iot.core.util.IotDeviceAuthUtils; import cn.iocoder.yudao.module.iot.gateway.protocol.http.IotHttpUpstreamProtocol; @@ -47,7 +49,7 @@ public class IotHttpAuthHandler extends IotHttpAbstractHandler { @Override public CommonResult handle0(RoutingContext context) { - // 解析参数 + // 1. 解析参数 JsonObject body = context.body().asJsonObject(); String clientId = body.getString("clientId"); if (StrUtil.isEmpty(clientId)) { @@ -62,20 +64,23 @@ public class IotHttpAuthHandler extends IotHttpAbstractHandler { throw invalidParamException("password 不能为空"); } - // 执行认证 + // 2.1 执行认证 CommonResult result = deviceClientService.authDevice(new IotDeviceAuthReqDTO() .setClientId(clientId).setUsername(username).setPassword(password)); - if (result == null || !result.isSuccess()) { + result.checkError();; + if (!BooleanUtil.isTrue(result.getData())) { throw exception(DEVICE_AUTH_FAIL); } - - // 生成 Token + // 2.2 生成 Token IotDeviceAuthUtils.DeviceInfo deviceInfo = deviceTokenService.parseUsername(username); Assert.notNull(deviceInfo, "设备信息不能为空"); String token = deviceTokenService.createToken(deviceInfo.getProductKey(), deviceInfo.getDeviceName()); Assert.notBlank(token, "生成 token 不能为空位"); - // TODO @芋艿:发送上线消息; + // 3. 执行上线 + deviceMessageProducer.sendDeviceMessage(IotDeviceMessage.of(deviceInfo.getProductKey(), deviceInfo.getDeviceName(), + protocol.getServerId()) + .ofStateOnline()); // 构建响应数据 return success(MapUtil.of("token", token)); diff --git a/yudao-module-iot/yudao-module-iot-gateway/src/main/java/cn/iocoder/yudao/module/iot/gateway/service/device/IotDeviceClientServiceImpl.java b/yudao-module-iot/yudao-module-iot-gateway/src/main/java/cn/iocoder/yudao/module/iot/gateway/service/device/IotDeviceClientServiceImpl.java index f61bf3df90..366d94aab1 100644 --- a/yudao-module-iot/yudao-module-iot-gateway/src/main/java/cn/iocoder/yudao/module/iot/gateway/service/device/IotDeviceClientServiceImpl.java +++ b/yudao-module-iot/yudao-module-iot-gateway/src/main/java/cn/iocoder/yudao/module/iot/gateway/service/device/IotDeviceClientServiceImpl.java @@ -1,5 +1,6 @@ package cn.iocoder.yudao.module.iot.gateway.service.device; +import cn.hutool.core.lang.Assert; 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; @@ -31,7 +32,7 @@ public class IotDeviceClientServiceImpl implements IotDeviceCommonApi { public void init() { IotGatewayProperties.RpcProperties rpc = gatewayProperties.getRpc(); restTemplate = new RestTemplateBuilder() - .rootUri(rpc.getUrl() + "/rpc-api/iot/device/") + .rootUri(rpc.getUrl() + "/rpc-api/iot/device") .readTimeout(rpc.getReadTimeout()) .connectTimeout(rpc.getConnectTimeout()) .build(); @@ -39,7 +40,7 @@ public class IotDeviceClientServiceImpl implements IotDeviceCommonApi { @Override public CommonResult authDevice(IotDeviceAuthReqDTO authReqDTO) { - return doPost("auth", authReqDTO); + return doPost("/auth", authReqDTO); } @SuppressWarnings("unchecked") @@ -48,6 +49,7 @@ public class IotDeviceClientServiceImpl implements IotDeviceCommonApi { CommonResult result = restTemplate.postForObject(url, requestBody, (Class>) (Class) CommonResult.class); log.info("[doPost][url({}) requestBody({}) result({})]", url, requestBody, result); + Assert.notNull(result, "请求结果不能为空"); return result; } catch (Exception e) { log.error("[doPost][url({}) requestBody({}) 发生异常]", url, requestBody, e);