Commit 84d76e0ac7faca965a088abab4434cd3832a7c53

Authored by YunaiV
1 parent 7138cab3

feat:【IoT 物联网】application 配置文件合并

yudao-module-iot/yudao-module-iot-gateway/src/main/java/cn/iocoder/yudao/module/iot/gateway/config/IotGatewayProperties.java
@@ -247,6 +247,7 @@ public class IotGatewayProperties { @@ -247,6 +247,7 @@ public class IotGatewayProperties {
247 */ 247 */
248 @Data 248 @Data
249 public static class Will { 249 public static class Will {
  250 +
250 /** 251 /**
251 * 是否启用遗嘱消息 252 * 是否启用遗嘱消息
252 */ 253 */
@@ -267,6 +268,7 @@ public class IotGatewayProperties { @@ -267,6 +268,7 @@ public class IotGatewayProperties {
267 * 遗嘱消息是否作为保留消息发布 268 * 遗嘱消息是否作为保留消息发布
268 */ 269 */
269 private boolean retain = true; 270 private boolean retain = true;
  271 +
270 } 272 }
271 273
272 /** 274 /**
@@ -274,6 +276,7 @@ public class IotGatewayProperties { @@ -274,6 +276,7 @@ public class IotGatewayProperties {
274 */ 276 */
275 @Data 277 @Data
276 public static class Ssl { 278 public static class Ssl {
  279 +
277 /** 280 /**
278 * 密钥库(KeyStore)路径,例如:classpath:certs/client.jks 281 * 密钥库(KeyStore)路径,例如:classpath:certs/client.jks
279 * 包含客户端自己的证书和私钥,用于向服务端证明身份(双向认证)。 282 * 包含客户端自己的证书和私钥,用于向服务端证明身份(双向认证)。
@@ -292,6 +295,7 @@ public class IotGatewayProperties { @@ -292,6 +295,7 @@ public class IotGatewayProperties {
292 * 信任库密码 295 * 信任库密码
293 */ 296 */
294 private String trustStorePassword; 297 private String trustStorePassword;
  298 +
295 } 299 }
296 300
297 } 301 }
yudao-module-iot/yudao-module-iot-gateway/src/main/java/cn/iocoder/yudao/module/iot/gateway/protocol/emqx/IotEmqxUpstreamProtocol.java
@@ -254,7 +254,7 @@ public class IotEmqxUpstreamProtocol { @@ -254,7 +254,7 @@ public class IotEmqxUpstreamProtocol {
254 * 创建 MQTT 客户端 254 * 创建 MQTT 客户端
255 */ 255 */
256 private void createMqttClient() { 256 private void createMqttClient() {
257 - // 1. 创建基础配置 257 + // 1.1 创建基础配置
258 MqttClientOptions options = (MqttClientOptions) new MqttClientOptions() 258 MqttClientOptions options = (MqttClientOptions) new MqttClientOptions()
259 .setClientId(emqxProperties.getMqttClientId()) 259 .setClientId(emqxProperties.getMqttClientId())
260 .setUsername(emqxProperties.getMqttUsername()) 260 .setUsername(emqxProperties.getMqttUsername())
@@ -265,8 +265,7 @@ public class IotEmqxUpstreamProtocol { @@ -265,8 +265,7 @@ public class IotEmqxUpstreamProtocol {
265 .setMaxInflightQueue(emqxProperties.getMaxInflightQueue()) 265 .setMaxInflightQueue(emqxProperties.getMaxInflightQueue())
266 .setConnectTimeout(emqxProperties.getConnectTimeoutSeconds() * 1000) // Vert.x 需要毫秒 266 .setConnectTimeout(emqxProperties.getConnectTimeoutSeconds() * 1000) // Vert.x 需要毫秒
267 .setTrustAll(emqxProperties.getTrustAll()); 267 .setTrustAll(emqxProperties.getTrustAll());
268 -  
269 - // 2. 配置遗嘱消息 268 + // 1.2 配置遗嘱消息
270 IotGatewayProperties.EmqxProperties.Will will = emqxProperties.getWill(); 269 IotGatewayProperties.EmqxProperties.Will will = emqxProperties.getWill();
271 if (will.isEnabled()) { 270 if (will.isEnabled()) {
272 Assert.notBlank(will.getTopic(), "遗嘱消息主题(will.topic)不能为空"); 271 Assert.notBlank(will.getTopic(), "遗嘱消息主题(will.topic)不能为空");
@@ -277,30 +276,26 @@ public class IotEmqxUpstreamProtocol { @@ -277,30 +276,26 @@ public class IotEmqxUpstreamProtocol {
277 .setWillQoS(will.getQos()) 276 .setWillQoS(will.getQos())
278 .setWillRetain(will.isRetain()); 277 .setWillRetain(will.isRetain());
279 } 278 }
280 -  
281 - // 3. 配置高级 SSL/TLS (仅在启用 SSL 且不信任所有证书时生效) 279 + // 1.3 配置高级 SSL/TLS (仅在启用 SSL 且不信任所有证书时生效)
282 if (Boolean.TRUE.equals(emqxProperties.getMqttSsl()) && !Boolean.TRUE.equals(emqxProperties.getTrustAll())) { 280 if (Boolean.TRUE.equals(emqxProperties.getMqttSsl()) && !Boolean.TRUE.equals(emqxProperties.getTrustAll())) {
283 IotGatewayProperties.EmqxProperties.Ssl sslOptions = emqxProperties.getSslOptions(); 281 IotGatewayProperties.EmqxProperties.Ssl sslOptions = emqxProperties.getSslOptions();
284 - // 配置信任库 (用于验证服务端证书)  
285 if (StrUtil.isNotBlank(sslOptions.getTrustStorePath())) { 282 if (StrUtil.isNotBlank(sslOptions.getTrustStorePath())) {
286 options.setTrustStoreOptions(new JksOptions() 283 options.setTrustStoreOptions(new JksOptions()
287 .setPath(sslOptions.getTrustStorePath()) 284 .setPath(sslOptions.getTrustStorePath())
288 .setPassword(sslOptions.getTrustStorePassword())); 285 .setPassword(sslOptions.getTrustStorePassword()));
289 } 286 }
290 - // 配置密钥库 (用于客户端双向认证)  
291 if (StrUtil.isNotBlank(sslOptions.getKeyStorePath())) { 287 if (StrUtil.isNotBlank(sslOptions.getKeyStorePath())) {
292 options.setKeyStoreOptions(new JksOptions() 288 options.setKeyStoreOptions(new JksOptions()
293 .setPath(sslOptions.getKeyStorePath()) 289 .setPath(sslOptions.getKeyStorePath())
294 .setPassword(sslOptions.getKeyStorePassword())); 290 .setPassword(sslOptions.getKeyStorePassword()));
295 } 291 }
296 } 292 }
297 -  
298 - // 4. 安全警告日志 293 + // 1.4 安全警告日志
299 if (Boolean.TRUE.equals(emqxProperties.getTrustAll())) { 294 if (Boolean.TRUE.equals(emqxProperties.getTrustAll())) {
300 log.warn("[createMqttClient][安全警告:当前配置信任所有 SSL 证书(trustAll=true),这在生产环境中存在严重安全风险!]"); 295 log.warn("[createMqttClient][安全警告:当前配置信任所有 SSL 证书(trustAll=true),这在生产环境中存在严重安全风险!]");
301 } 296 }
302 297
303 - // 5. 创建客户端实例 298 + // 2. 创建客户端实例
304 this.mqttClient = MqttClient.create(vertx, options); 299 this.mqttClient = MqttClient.create(vertx, options);
305 } 300 }
306 301
yudao-module-iot/yudao-module-iot-gateway/src/main/java/cn/iocoder/yudao/module/iot/gateway/protocol/tcp/IotTcpUpstreamProtocol.java
@@ -54,7 +54,7 @@ public class IotTcpUpstreamProtocol { @@ -54,7 +54,7 @@ public class IotTcpUpstreamProtocol {
54 }); 54 });
55 55
56 // 3. 启动 TCP 服务器 56 // 3. 启动 TCP 服务器
57 - netServer.listen(tcpProperties.getServerPort(), "0.0.0.0") 57 + netServer.listen(tcpProperties.getServerPort())
58 .onSuccess(server -> log.info("[start][IoT 网关 TCP 服务启动成功,端口:{}]", server.actualPort())) 58 .onSuccess(server -> log.info("[start][IoT 网关 TCP 服务启动成功,端口:{}]", server.actualPort()))
59 .onFailure(e -> log.error("[start][IoT 网关 TCP 服务启动失败]", e)); 59 .onFailure(e -> log.error("[start][IoT 网关 TCP 服务启动失败]", e));
60 } 60 }