feat:【IoT 物联网】增加网关 HTTP 协议的鉴权,基于 JWT 轻量级

This commit is contained in:
YunaiV
2025-06-03 13:22:55 +08:00
parent 1498389d26
commit 643cc4cfd2
40 changed files with 793 additions and 498 deletions

View File

@@ -1,25 +0,0 @@
package cn.iocoder.yudao.module.iot.net.component.core.config;
import cn.iocoder.yudao.module.iot.net.component.core.upstream.IotDeviceUpstreamClient;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.annotation.EnableScheduling;
/**
* IoT 网络组件的通用自动配置类
*
* @author haohao
*/
@EnableConfigurationProperties(IotNetComponentCommonProperties.class)
public class IotNetComponentCommonAutoConfiguration {
/**
* 创建设备上行客户端
*/
@Bean
public IotDeviceUpstreamClient deviceUpstreamClient() {
return new IotDeviceUpstreamClient();
}
}

View File

@@ -1,26 +0,0 @@
package cn.iocoder.yudao.module.iot.net.component.core.upstream;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.iot.api.device.IotDeviceUpstreamApi;
import cn.iocoder.yudao.module.iot.api.device.dto.control.upstream.*;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
/**
* 设备数据 Upstream 上行客户端
* <p>
* 直接调用 IotDeviceUpstreamApi 接口
*
* @author haohao
*/
@Slf4j
public class IotDeviceUpstreamClient implements IotDeviceUpstreamApi {
@Resource
private IotDeviceUpstreamApi deviceUpstreamApi;
@Override
public CommonResult<Boolean> authenticateEmqxConnection(IotDeviceEmqxAuthReqDTO authReqDTO) {
return deviceUpstreamApi.authenticateEmqxConnection(authReqDTO);
}
}

View File

@@ -1,56 +0,0 @@
package cn.iocoder.yudao.module.iot.net.component.core.util;
import cn.iocoder.yudao.framework.common.util.json.JsonUtils;
import cn.iocoder.yudao.module.iot.net.component.core.pojo.IotStandardResponse;
import io.vertx.core.http.HttpHeaders;
import io.vertx.ext.web.RoutingContext;
import org.springframework.http.MediaType;
/**
* IoT 网络组件的通用工具类
*
* @author 芋道源码
*/
public class IotNetComponentCommonUtils {
/**
* 将对象转换为JSON字符串后写入HTTP响应
*
* @param routingContext 路由上下文
* @param data 数据对象
*/
@SuppressWarnings("deprecation")
public static void writeJsonResponse(RoutingContext routingContext, Object data) {
routingContext.response()
.setStatusCode(200)
.putHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_UTF8_VALUE)
.end(JsonUtils.toJsonString(data));
}
/**
* 生成标准JSON格式的响应并写入HTTP响应基于IotStandardResponse
* <p>
* 推荐使用此方法,统一 MQTT 和 HTTP 的响应格式。使用方式:
*
* <pre>
* // 成功响应
* IotStandardResponse response = IotStandardResponse.success(requestId, method, data);
* IotNetComponentCommonUtils.writeJsonResponse(routingContext, response);
*
* // 错误响应
* IotStandardResponse errorResponse = IotStandardResponse.error(requestId, method, code, message);
* IotNetComponentCommonUtils.writeJsonResponse(routingContext, errorResponse);
* </pre>
*
* @param routingContext 路由上下文
* @param response IotStandardResponse 响应对象
*/
@SuppressWarnings("deprecation")
public static void writeJsonResponse(RoutingContext routingContext, IotStandardResponse response) {
routingContext.response()
.setStatusCode(200)
.putHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_UTF8_VALUE)
.end(JsonUtils.toJsonString(response));
}
}

View File

@@ -2,7 +2,7 @@ package cn.iocoder.yudao.module.iot.net.component.emqx.upstream.router;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.iot.api.device.IotDeviceUpstreamApi;
import cn.iocoder.yudao.module.iot.api.device.dto.control.upstream.IotDeviceEmqxAuthReqDTO;
import cn.iocoder.yudao.module.iot.core.biz.dto.IotDeviceEmqxAuthReqDTO;
import cn.iocoder.yudao.module.iot.net.component.core.util.IotNetComponentCommonUtils;
import io.vertx.core.Handler;
import io.vertx.core.json.JsonObject;

View File

@@ -1,46 +0,0 @@
package cn.iocoder.yudao.module.iot.net.component.server.upstream;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.iot.net.component.server.config.IotNetComponentServerProperties;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.client.RestTemplate;
import static cn.iocoder.yudao.framework.common.exception.enums.GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR;
/**
* 组件上行客户端,用于向主程序上报设备数据
* <p>
* 通过 HTTP 调用远程的 IotDeviceUpstreamApi 接口
*
* @author haohao
*/
@RequiredArgsConstructor
@Slf4j
public class IotComponentUpstreamClient {
public static final String URL_PREFIX = "/rpc-api/iot/device/upstream";
private final IotNetComponentServerProperties properties;
private final RestTemplate restTemplate;
// @Override
// public CommonResult<Boolean> updateDeviceState(IotDeviceStateUpdateReqDTO updateReqDTO) {
// String url = properties.getUpstreamUrl() + URL_PREFIX + "/update-state";
// return doPost(url, updateReqDTO);
// }
@SuppressWarnings("unchecked")
private <T> CommonResult<Boolean> doPost(String url, T requestBody) {
try {
CommonResult<Boolean> result = restTemplate.postForObject(url, requestBody,
(Class<CommonResult<Boolean>>) (Class<?>) CommonResult.class);
log.info("[doPost][url({}) requestBody({}) result({})]", url, requestBody, result);
return result;
} catch (Exception e) {
log.error("[doPost][url({}) requestBody({}) 发生异常]", url, requestBody, e);
return CommonResult.error(INTERNAL_SERVER_ERROR);
}
}
}