智控台只给已登记设备发凭证

智控台这边以前任何人报一个设备号过来就换一个凭证。现在改成只有在智
控台登记过的设备才发凭证和 MQTT 连接信息,登记是要登录才能操作的,
所以不会再变成谁都能换凭证的通道。设备号格式也跟着收紧了,之前的写
法把数字字母都算成合法地址,现在只认十六进制,另外也认不加分隔符
的那种写法。
This commit is contained in:
hrz 2026-10-22 01:26:40 +00:00
parent 0b5a0097cd
commit d1bd477fe9
2 changed files with 29 additions and 18 deletions

View file

@ -107,7 +107,7 @@ public class OTAController {
/**
* 简单判断mac地址是否有效(非严格)
*
*
* @param macAddress
* @return
*/
@ -115,8 +115,8 @@ public class OTAController {
if (StringUtils.isBlank(macAddress)) {
return false;
}
// MAC地址通常为12位十六进制数字,可以包含冒号或连字符分隔符
String macPattern = "^([0-9A-Za-z]{2}[:-]){5}([0-9A-Za-z]{2})$";
// MAC地址为12位十六进制数字,可用冒号、连字符或下划线分隔,也接受无分隔符形态
String macPattern = "^([0-9A-Fa-f]{2}[:\\-_]){5}[0-9A-Fa-f]{2}$|^[0-9A-Fa-f]{12}$";
return macAddress.matches(macPattern);
}
}

View file

@ -219,13 +219,20 @@ public class DeviceServiceImpl extends BaseServiceImpl<DeviceDao, DeviceEntity>
// 检查是否启用认证并生成token
String authEnabled = sysParamsService.getValue(Constant.SERVER_AUTH_ENABLED, true);
if ("true".equalsIgnoreCase(authEnabled)) {
try {
// 生成token
String token = generateWebSocketToken(clientId, macAddress);
websocket.setToken(token);
} catch (Exception e) {
log.error("生成WebSocket token失败: {}", e.getMessage());
// 仅已登记绑定的设备签发 token;未登记的设备不返回可用凭证,
// 登记操作需登录智控台,因此不会成为任意 MAC 换取 token 的通道
if (deviceById == null) {
log.warn("设备未在智控台登记,跳过 WebSocket token 签发: {}", macAddress);
websocket.setToken("");
} else {
try {
// 生成token
String token = generateWebSocketToken(clientId, macAddress);
websocket.setToken(token);
} catch (Exception e) {
log.error("生成WebSocket token失败: {}", e.getMessage());
websocket.setToken("");
}
}
} else {
websocket.setToken("");
@ -252,16 +259,20 @@ public class DeviceServiceImpl extends BaseServiceImpl<DeviceDao, DeviceEntity>
// 从系统参数获取MQTT Gateway地址,仅在配置有效时使用
String mqttUdpConfig = sysParamsService.getValue(Constant.SERVER_MQTT_GATEWAY, true);
if (mqttUdpConfig != null && !mqttUdpConfig.equals("null") && !mqttUdpConfig.isEmpty()) {
try {
String groupId = deviceById != null && deviceById.getBoard() != null ? deviceById.getBoard()
: "GID_default";
DeviceReportRespDTO.MQTT mqtt = buildMqttConfig(macAddress, groupId);
if (mqtt != null) {
mqtt.setEndpoint(mqttUdpConfig);
response.setMqtt(mqtt);
// MQTT 配置含连接签名,与 websocket token 一样仅对已登记设备下发
if (deviceById == null) {
log.warn("设备未在智控台登记,跳过 MQTT 配置下发: {}", macAddress);
} else {
try {
String groupId = deviceById.getBoard() != null ? deviceById.getBoard() : "GID_default";
DeviceReportRespDTO.MQTT mqtt = buildMqttConfig(macAddress, groupId);
if (mqtt != null) {
mqtt.setEndpoint(mqttUdpConfig);
response.setMqtt(mqtt);
}
} catch (Exception e) {
log.error("生成MQTT配置失败: {}", e.getMessage());
}
} catch (Exception e) {
log.error("生成MQTT配置失败: {}", e.getMessage());
}
}