feat:【IoT 物联网】增加网关 HTTP 协议的鉴权,基于 JWT 轻量级
This commit is contained in:
@@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1 +0,0 @@
|
||||
cn.iocoder.yudao.module.iot.net.component.core.config.IotNetComponentCommonAutoConfiguration
|
||||
@@ -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;
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user