Commit 638976dac81a7098b0e8d563379a05e7fd4d6c8b
1 parent
a9733b4d
【代码评审】IoT:整体实现
Showing
1 changed file
with
8 additions
and
6 deletions
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/device/control/IotDeviceUpstreamServiceImpl.java
| @@ -280,11 +280,11 @@ public class IotDeviceUpstreamServiceImpl implements IotDeviceUpstreamService { | @@ -280,11 +280,11 @@ public class IotDeviceUpstreamServiceImpl implements IotDeviceUpstreamService { | ||
| 280 | sendDeviceMessage(message, device); | 280 | sendDeviceMessage(message, device); |
| 281 | } | 281 | } |
| 282 | 282 | ||
| 283 | + // TODO @芋艿:后续需要考虑,http 的认证 | ||
| 283 | @Override | 284 | @Override |
| 284 | public boolean authenticateEmqxConnection(IotDeviceEmqxAuthReqDTO authReqDTO) { | 285 | public boolean authenticateEmqxConnection(IotDeviceEmqxAuthReqDTO authReqDTO) { |
| 285 | log.info("[authenticateEmqxConnection][认证 Emqx 连接: {}]", authReqDTO); | 286 | log.info("[authenticateEmqxConnection][认证 Emqx 连接: {}]", authReqDTO); |
| 286 | - // 1. 校验设备是否存在 | ||
| 287 | - // username 格式:${DeviceName}&${ProductKey} | 287 | + // 1.1 校验设备是否存在。username 格式:${DeviceName}&${ProductKey} |
| 288 | String[] usernameParts = authReqDTO.getUsername().split("&"); | 288 | String[] usernameParts = authReqDTO.getUsername().split("&"); |
| 289 | if (usernameParts.length != 2) { | 289 | if (usernameParts.length != 2) { |
| 290 | log.error("[authenticateEmqxConnection][认证失败,username 格式不正确]"); | 290 | log.error("[authenticateEmqxConnection][认证失败,username 格式不正确]"); |
| @@ -292,17 +292,19 @@ public class IotDeviceUpstreamServiceImpl implements IotDeviceUpstreamService { | @@ -292,17 +292,19 @@ public class IotDeviceUpstreamServiceImpl implements IotDeviceUpstreamService { | ||
| 292 | } | 292 | } |
| 293 | String deviceName = usernameParts[0]; | 293 | String deviceName = usernameParts[0]; |
| 294 | String productKey = usernameParts[1]; | 294 | String productKey = usernameParts[1]; |
| 295 | - IotDeviceDO device = deviceService.getDeviceByProductKeyAndDeviceNameFromCache( | ||
| 296 | - productKey, deviceName); | 295 | + // 1.2 获得设备 |
| 296 | + IotDeviceDO device = deviceService.getDeviceByProductKeyAndDeviceNameFromCache(productKey, deviceName); | ||
| 297 | if (device == null) { | 297 | if (device == null) { |
| 298 | - log.error("[authenticateEmqxConnection][设备({}/{}) 不存在]", | ||
| 299 | - productKey, deviceName); | 298 | + log.error("[authenticateEmqxConnection][设备({}/{}) 不存在]", productKey, deviceName); |
| 300 | return false; | 299 | return false; |
| 301 | } | 300 | } |
| 301 | + // TODO @haohao:需要记录,记录设备的最后时间 | ||
| 302 | + | ||
| 302 | // 2. 校验密码 | 303 | // 2. 校验密码 |
| 303 | String deviceSecret = device.getDeviceSecret(); | 304 | String deviceSecret = device.getDeviceSecret(); |
| 304 | String clientId = authReqDTO.getClientId(); | 305 | String clientId = authReqDTO.getClientId(); |
| 305 | MqttSignResult sign = MqttSignUtils.calculate(productKey, deviceName, deviceSecret, clientId); | 306 | MqttSignResult sign = MqttSignUtils.calculate(productKey, deviceName, deviceSecret, clientId); |
| 307 | + // TODO 建议,先失败,return false; | ||
| 306 | if (StrUtil.equals(sign.getPassword(), authReqDTO.getPassword())) { | 308 | if (StrUtil.equals(sign.getPassword(), authReqDTO.getPassword())) { |
| 307 | log.info("[authenticateEmqxConnection][认证成功]"); | 309 | log.info("[authenticateEmqxConnection][认证成功]"); |
| 308 | return true; | 310 | return true; |