From 86d368337175a7d7eed5e9330bd80a838ab4f396 Mon Sep 17 00:00:00 2001
From: jiazx0107@163.com <jiazx0107@163.com>
Date: 星期四, 09 十一月 2023 17:25:23 +0800
Subject: [PATCH] Merge remote-tracking branch 'origin/igds-api-gateway' into igds-api-gateway
---
src/main/java/com/fzzy/gateway/hx2023/service/HxGatewayRemoteServiceImpl.java | 267 +++++++++++++++++++++++++++++++++++++++++-----------
1 files changed, 208 insertions(+), 59 deletions(-)
diff --git a/src/main/java/com/fzzy/gateway/hx2023/service/HxGatewayRemoteServiceImpl.java b/src/main/java/com/fzzy/gateway/hx2023/service/HxGatewayRemoteServiceImpl.java
index d95db6b..c5c8362 100644
--- a/src/main/java/com/fzzy/gateway/hx2023/service/HxGatewayRemoteServiceImpl.java
+++ b/src/main/java/com/fzzy/gateway/hx2023/service/HxGatewayRemoteServiceImpl.java
@@ -1,25 +1,27 @@
package com.fzzy.gateway.hx2023.service;
-import com.fzzy.api.data.AuthToken;
+import com.alibaba.fastjson2.JSONObject;
import com.fzzy.api.data.PushProtocol;
-import com.fzzy.api.entity.ApiLog;
-import com.fzzy.api.utils.ContextUtil;
-import com.fzzy.api.utils.RedisConst;
-import com.fzzy.api.utils.RedisUtil;
+import com.fzzy.api.utils.*;
import com.fzzy.api.view.repository.ApiLogRep;
+import com.fzzy.gateway.service.GatewayConfService;
+import com.fzzy.gateway.util.GatewayHttpUtil;
import com.fzzy.gateway.api.GatewayRemoteService;
-import com.fzzy.gateway.data.GatewayResponse;
import com.fzzy.gateway.entity.GatewayConf;
import com.fzzy.gateway.hx2023.ScConstant;
-import com.fzzy.push.gb2022.HttpClientUtil;
+import com.fzzy.gateway.hx2023.data.CloudResp;
+import com.fzzy.gateway.util.GatewayRSAUtils;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;
-import org.springframework.stereotype.Service;
import javax.annotation.Resource;
+import java.io.UnsupportedEncodingException;
+import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
+import java.util.TreeMap;
@Slf4j
@@ -30,8 +32,9 @@
@Resource
private ApiLogRep apiLogRep;
+
@Resource
- private RedisUtil redisUtil;
+ private GatewayConfService gatewayConfService;
@Override
@@ -40,80 +43,226 @@
}
@Override
- public GatewayResponse authorize(GatewayConf conf) {
-
- //娣诲姞LOG
- ApiLog apiLog = new ApiLog();
- apiLog.setData("閴存潈鎺ュ彛");
- apiLog.setId(ContextUtil.getUUID());
- apiLog.setKqdm(conf.getKqdm());
-
+ public void init(GatewayConf gatewayConf) {
try {
- Map<String, Object> map = new HashMap<>();
- map.put("username", conf.getUserName());
- map.put("username", conf.getPassword());
+ if (StringUtils.isEmpty(gatewayConf.getApiUrl())) {
+ return;
+ }
- log.debug("-----------------鏁版嵁鎶ユ枃----------------{}", map);
+ //鑾峰彇鍏閽ユ帴鍙�
+ Map<String, String> params = new HashMap<>();
+ params.put("appId", gatewayConf.getGatewayId());
+
+ String url = gatewayConf.getApiUrl() + "reserver/api/key/apply";
+ String jsonStr = GatewayHttpUtil.doGet(url, params);
+
+ log.info("---鑾峰彇鍏閽ユ帴鍙�-杩斿洖---{}", jsonStr);
+
+ CloudResp respKey = JSONObject.parseObject(jsonStr, CloudResp.class);
+
+ if (ScConstant.CODE_200 == respKey.getCode()) {
+ JSONObject object = respKey.getData();
+ String pubKey = (String) object.get("pubKey");
+ String priKey = (String) object.get("priKey");
+
+ gatewayConf.setPublicKey(pubKey);
+ gatewayConf.setPrivateKey(priKey);
+ }
- String url = conf.getApiUrl() + ScConstant.API_URL_AUTH;
+ //鑾峰彇 AccessToken 鎺ュ彛
+ String sign = getSign(params, gatewayConf.getPrivateKey());
+ params.put("sign", sign);
+ url = gatewayConf.getApiUrl() + "reserver/api/token/apply";
+ jsonStr = GatewayHttpUtil.doGet(url, params);
+ log.info("---鑾峰彇AccessToken鎺ュ彛-杩斿洖---{}", jsonStr);
+ CloudResp respToken = JSONObject.parseObject(jsonStr, CloudResp.class);
+ if (ScConstant.CODE_200 == respToken.getCode()) {
+ JSONObject object = respKey.getData();
+ if (null != object) {
+ String token = (String) object.get("token");
+ gatewayConf.setAccessToken(token);
+ }
+ }
- GatewayResponse responseDto = HttpClientUtil.pushGateway(url, map);
- apiLog.setStatus(responseDto.getStatus());
- apiLog.setResult(responseDto.getMessage());
- apiLogRep.save(apiLog);
+ //鏇存柊缂撳瓨
+ updateAuthToken(gatewayConf);
- updateAuthToken(responseDto, conf);
-
- return responseDto;
} catch (Exception e) {
- apiLog.setStatus(99);
- apiLog.setResult("澶辫触锛�" + e.getMessage());
- apiLogRep.save(apiLog);
- log.error(e.getMessage(), e);
- return new GatewayResponse(99, e.getMessage());
+ log.error("------鍒濆鍖栧け璐�-----{}", e);
}
+ }
+
+ @Override
+ public void heartbeat(GatewayConf gatewayConf) {
+ try {
+
+ if (StringUtils.isEmpty(gatewayConf.getApiUrl())) {
+ return;
+ }
+
+ gatewayConf = getCacheConf(gatewayConf.getKqdm());
+ //缃戝叧蹇冭烦鎺ュ彛
+ Map<String, String> params = new HashMap<>();
+ params.put("token", gatewayConf.getAccessToken());
+ params.put("gatewayId", gatewayConf.getGatewayId());
+ params.put("gatewayIp", gatewayConf.getGatewayIp());
+ params.put("gatewayMac", gatewayConf.getGatewayMac());
+ params.put("heartbeat", "1");
+ params.put("timestamp", System.currentTimeMillis() + "");
+
+ String sign = getSign(params, gatewayConf.getPrivateKey());
+ params.put("sign", sign);
+
+ String url = gatewayConf.getApiUrl() + "reserver/api/iot/equipment/heartbeat";
+
+ String jsonStr = GatewayHttpUtil.doGet(url, params);
+ log.info("---缃戝叧蹇冭烦鎺ュ彛-杩斿洖---{}", jsonStr);
+
+ } catch (Exception e) {
+ log.error("------缃戝叧蹇冭烦鎺ュ彛--鎵ц澶辫触-----{}", e);
+ }
+
+ }
+
+ @Override
+ public void pushInfo(GatewayConf gatewayConf) {
+ try {
+
+ if (StringUtils.isEmpty(gatewayConf.getApiUrl())) {
+ return;
+ }
+
+ gatewayConf = getCacheConf(gatewayConf.getKqdm());
+ //缃戝叧蹇冭烦鎺ュ彛
+ Map<String, String> params = new HashMap<>();
+ params.put("token", gatewayConf.getAccessToken());
+ params.put("gatewayId", gatewayConf.getGatewayId());
+ params.put("gatewayIp", gatewayConf.getGatewayIp());
+ params.put("gatewayMac", gatewayConf.getGatewayMac());
+ params.put("gatewayCPU", gatewayConf.getGatewayCPU());
+ params.put("gatewayMem", gatewayConf.getGatewayMem());
+ params.put("gatewayHardDisk", gatewayConf.getGatewayHardDisk());
+ params.put("timestamp", System.currentTimeMillis() + "");
+
+ String sign = getSign(params, gatewayConf.getPrivateKey());
+ params.put("sign", sign);
+
+ String url = gatewayConf.getApiUrl() + "reserver/api/iot/equipment/heartbeat";
+
+ String jsonStr = GatewayHttpUtil.doGet(url, params);
+ log.info("---鎺ㄩ�佺綉鍏充俊鎭�-杩斿洖---{}", jsonStr);
+
+ } catch (Exception e) {
+ log.error("------鎺ㄩ�佺綉鍏充俊鎭�--鎵ц澶辫触-----{}", e);
+ }
+ }
+
+
+ public String getSign(Map<String, String> parames, String priKey) {
+ //鍙傛暟鎷兼帴
+ String msg = "";
+ parames = sortMapKey(parames);
+ for (Map.Entry<String, String> param : parames.entrySet()) {
+ msg += param.getKey() + "=" + param.getValue() + "&";
+ }
+ msg = msg.substring(0, msg.length() - 1);
+
+ log.debug("------寰呭姞瀵嗕俊鎭�-----{}", msg);
+
+ //MD5鍔犲瘑
+ String md5sign = MyMD5Util.getMD5(msg);
+ log.debug("------md5鍔犲瘑-----{}", md5sign);
+
+ //RSA鍔犲瘑
+ String result = GatewayRSAUtils.encryptByPrivate(md5sign, priKey);
+ log.debug("------RSA鍔犲瘑-----{}", result);
+
+ return result;
+ }
+ public static String getSign2(Map<String, String> parames, String priKey) {
+ //鍙傛暟鎷兼帴
+ String msg = "";
+ parames = sortMapKey(parames);
+ for (Map.Entry<String, String> param : parames.entrySet()) {
+ msg += param.getKey() + "=" + param.getValue() + "&";
+ }
+ msg = msg.substring(0, msg.length() - 1);
+
+ log.debug("------寰呭姞瀵嗕俊鎭�-----{}", msg);
+
+ //MD5鍔犲瘑
+ String md5sign = MyMD5Util.getMD5(msg);
+ log.debug("------md5鍔犲瘑-----{}", md5sign);
+
+ //RSA鍔犲瘑
+ String result = GatewayRSAUtils.encryptByPrivate(md5sign, priKey);
+ log.debug("------RSA鍔犲瘑-----{}", result);
+
+ return result;
}
/**
* @param kqdm
* @return
*/
- public AuthToken getAuthToken(String kqdm) {
- try {
- String key = RedisConst.buildKey(RedisConst.KYE_TOKEN, kqdm);
+ public GatewayConf getCacheConf(String kqdm) {
+ return gatewayConfService.getCacheConf(kqdm);
+ }
- AuthToken token = (AuthToken) redisUtil.get(key);
+ private void updateAuthToken(GatewayConf conf) {
- if (null == token) {
- log.error("------------------鏈幏鍙栧埌TOKEN---------------");
- return null;
+ gatewayConfService.updateCache(conf);
+
+ }
+
+ private static Map<String, String> sortMapKey(Map<String, String> map){
+ if(map == null || map.isEmpty()){
+ return null;
+ }
+
+
+ Map<String, String> sortMap = new TreeMap<>(new Comparator<String>() {
+ @Override
+ public int compare(String o1, String o2) {
+ return o1.compareTo(o2);
}
-
- return token;
- } catch (Exception e) {
- log.error(e.getMessage(), e);
- return null;
- }
+ });
+ sortMap.putAll(map);
+ return sortMap;
}
- private void updateAuthToken(GatewayResponse dto, GatewayConf conf) {
- String key = RedisConst.buildKey(RedisConst.KYE_TOKEN, conf.getKqdm());
- AuthToken token = getAuthToken(conf.getKqdm());
- if (null == token) {
- token = new AuthToken();
- token.setKqdm(conf.getKqdm());
+ public static void main(String[] args) throws UnsupportedEncodingException {
+ //缃戝叧蹇冭烦鎺ュ彛
+// Map<String, String> params = new HashMap<>();
+// //params.put("token", "");
+// params.put("gatewayId", "6e3d92ff71b911eea5e50250f2000002");
+// params.put("gatewayIp", "123");
+// params.put("gatewayMac", "123");
+// params.put("gatewayCPU","123");
+// params.put("gatewayMem", "123");
+// params.put("gatewayHardDisk", "123");
+// params.put("timestamp", "2023-10-31 12:12:12");
+// String sign = getSign2(params,"MIIBVQIBADANBgkqhkiG9w0BAQEFAASCAT8wggE7AgEAAkEA0lg35sJGRyUFEDzt\np9pWvc0JD+0+SmiZIgMbUaEM5gqBsvUJ9LnzB0rpMTbuqAxAFbUH1Nw4qXdJVnKS\nRYfPgQIDAQABAkBPkxIq2Uou9gFRYEtkGcSA70VDYjQTgDiWVmiKXAv+JkEJmOno\\nMrHYQGAiP0q4xW66F+CLNCrGPVJns2ywMxwxAiEA/vyNX5M0/t+RHAxpMp484i9T\nb5Qf+HvepOV3c0UGZx0CIQDTLj4+qgoXGOLCM9/dzVJZ04VIjK6u546z22hXFWgI\ntQIhAMWFP8JMlx3kc1UF7Cuw1jrkLD7fwJBM7CBSPODHFHmlAiEAhv4Hcf02B/+z\nbBrG3rzSCHOKvo6XV1zTvqrhLtGTOAUCIGmoaIDgTuS0ZF71+c9mYvPVNL0T02GQ\nE8Y3ExreQphx");
+// params.put("sign", sign);
+//
+// System.out.println(sign);
+ byte[] encryptedData = GatewayRSAUtils.decryptBase64("yx7jNj9DGS7AkH/sXo5IrwaGNCSUnAZFmjXwWovMBfvYGVQur1RELXUu4fqIMZZ/ck6CAA3ESOJCS+aCD0aVdA==");
+ StringBuilder hexString = new StringBuilder();
+ for (byte b : encryptedData) {
+ String hex = Integer.toHexString(0xFF & b);
+ if (hex.length() == 1) {
+ hexString.append('0');
+ }
+ hexString.append(hex);
}
+ String hexStringResult = hexString.toString();
+ System.out.println(hexStringResult);
+ System.out.println(BytesUtil.bytesToString(GatewayRSAUtils.decryptBase64("mgtJOOn5iZBvmQI3hzvaw2J4BiZUcXhLTsgLk8++Ig2iIZcPaky7V+ylkA+BAYxIBS5ovHXjRo4w26TTEq9k7Q==")));
- if (null != dto.getResult()) {
- token.setToken(dto.getResult().getToken());
- }
-
- redisUtil.set(key, token);
}
-
}
--
Gitblit v1.9.3