【代码评审】IoT:整体实现

This commit is contained in:
YunaiV 2025-03-16 23:15:32 +08:00
parent a9733b4d2a
commit 638976dac8
1 changed files with 8 additions and 6 deletions

View File

@ -280,11 +280,11 @@ public class IotDeviceUpstreamServiceImpl implements IotDeviceUpstreamService {
sendDeviceMessage(message, device); sendDeviceMessage(message, device);
} }
// TODO @芋艿后续需要考虑http 的认证
@Override @Override
public boolean authenticateEmqxConnection(IotDeviceEmqxAuthReqDTO authReqDTO) { public boolean authenticateEmqxConnection(IotDeviceEmqxAuthReqDTO authReqDTO) {
log.info("[authenticateEmqxConnection][认证 Emqx 连接: {}]", authReqDTO); log.info("[authenticateEmqxConnection][认证 Emqx 连接: {}]", authReqDTO);
// 1. 校验设备是否存在 // 1.1 校验设备是否存在username 格式${DeviceName}&${ProductKey}
// username 格式${DeviceName}&${ProductKey}
String[] usernameParts = authReqDTO.getUsername().split("&"); String[] usernameParts = authReqDTO.getUsername().split("&");
if (usernameParts.length != 2) { if (usernameParts.length != 2) {
log.error("[authenticateEmqxConnection][认证失败username 格式不正确]"); log.error("[authenticateEmqxConnection][认证失败username 格式不正确]");
@ -292,17 +292,19 @@ public class IotDeviceUpstreamServiceImpl implements IotDeviceUpstreamService {
} }
String deviceName = usernameParts[0]; String deviceName = usernameParts[0];
String productKey = usernameParts[1]; String productKey = usernameParts[1];
IotDeviceDO device = deviceService.getDeviceByProductKeyAndDeviceNameFromCache( // 1.2 获得设备
productKey, deviceName); IotDeviceDO device = deviceService.getDeviceByProductKeyAndDeviceNameFromCache(productKey, deviceName);
if (device == null) { if (device == null) {
log.error("[authenticateEmqxConnection][设备({}/{}) 不存在]", log.error("[authenticateEmqxConnection][设备({}/{}) 不存在]", productKey, deviceName);
productKey, deviceName);
return false; return false;
} }
// TODO @haohao需要记录记录设备的最后时间
// 2. 校验密码 // 2. 校验密码
String deviceSecret = device.getDeviceSecret(); String deviceSecret = device.getDeviceSecret();
String clientId = authReqDTO.getClientId(); String clientId = authReqDTO.getClientId();
MqttSignResult sign = MqttSignUtils.calculate(productKey, deviceName, deviceSecret, clientId); MqttSignResult sign = MqttSignUtils.calculate(productKey, deviceName, deviceSecret, clientId);
// TODO 建议先失败return false
if (StrUtil.equals(sign.getPassword(), authReqDTO.getPassword())) { if (StrUtil.equals(sign.getPassword(), authReqDTO.getPassword())) {
log.info("[authenticateEmqxConnection][认证成功]"); log.info("[authenticateEmqxConnection][认证成功]");
return true; return true;