From dc5136bee4b71867af2ea23912405e614fe17a13 Mon Sep 17 00:00:00 2001 From: sxu <602087911@qq.com> Date: Mon, 2 Sep 2024 16:49:36 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8E=A8=E9=80=81=E8=A3=85=E5=A4=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../bonus/sgzb/common/core/utils/RsaUtil.java | 135 ++++++++++++++++++ .../base/controller/MaMachineController.java | 10 ++ .../sgzb/base/service/MaMachineService.java | 3 + .../service/impl/MaMachineServiceImpl.java | 66 ++++++++- .../main/resources/bootstrap-sgzb_nw_dev.yml | 1 + .../resources/bootstrap-sgzb_nw_local.yml | 1 + 6 files changed, 212 insertions(+), 4 deletions(-) create mode 100644 sgzb-common/sgzb-common-core/src/main/java/com/bonus/sgzb/common/core/utils/RsaUtil.java diff --git a/sgzb-common/sgzb-common-core/src/main/java/com/bonus/sgzb/common/core/utils/RsaUtil.java b/sgzb-common/sgzb-common-core/src/main/java/com/bonus/sgzb/common/core/utils/RsaUtil.java new file mode 100644 index 0000000..3369a85 --- /dev/null +++ b/sgzb-common/sgzb-common-core/src/main/java/com/bonus/sgzb/common/core/utils/RsaUtil.java @@ -0,0 +1,135 @@ +package com.bonus.sgzb.common.core.utils; + +import javax.crypto.Cipher; +import java.security.KeyFactory; +import java.security.PrivateKey; +import java.security.PublicKey; +import java.security.spec.PKCS8EncodedKeySpec; +import java.security.spec.X509EncodedKeySpec; +import java.util.Base64; + +/** + * @Author ma_sh + * @create 2024/5/25 16:07 + */ +public class RsaUtil { + + //签名算法名称 + private static final String RSA_KEY_ALGORITHM = "RSA"; + + //RSA密钥长度,默认密钥长度是1024,密钥长度必须是64的倍数,在512到65536位之间,不管是RSA还是RSA2长度推荐使用2048 + private static final int KEY_SIZE = 2048; + + /** + * 公钥加密(用于数据加密) + * + * @param data 加密前的字符串 + * @param publicKeyStr base64编码后的公钥 + * @return base64编码后的字符串 + * @throws Exception + */ + public static String encryptByPublicKey(String data, String publicKeyStr) throws Exception { + //Java原生base64解码 + byte[] pubKey = Base64.getDecoder().decode(publicKeyStr); + //创建X509编码密钥规范 + X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(pubKey); + //返回转换指定算法的KeyFactory对象 + KeyFactory keyFactory = KeyFactory.getInstance(RSA_KEY_ALGORITHM); + //根据X509编码密钥规范产生公钥对象 + PublicKey publicKey = keyFactory.generatePublic(x509KeySpec); + //根据转换的名称获取密码对象Cipher(转换的名称:算法/工作模式/填充模式) + Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm()); + //用公钥初始化此Cipher对象(加密模式) + cipher.init(Cipher.ENCRYPT_MODE, publicKey); + //对数据加密 + byte[] encrypt = cipher.doFinal(data.getBytes()); + //返回base64编码后的字符串 + return Base64.getEncoder().encodeToString(encrypt); + } + + + /** + * 私钥解密(用于数据解密) + * + * @param data 解密前的字符串 + * @param privateKeyStr 私钥 + * @return 解密后的字符串 + * @throws Exception + */ + public static String decryptByPrivateKey(String data, String privateKeyStr) throws Exception { + //Java原生base64解码 + byte[] priKey = Base64.getDecoder().decode(privateKeyStr); + //创建PKCS8编码密钥规范 + PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(priKey); + //返回转换指定算法的KeyFactory对象 + KeyFactory keyFactory = KeyFactory.getInstance(RSA_KEY_ALGORITHM); + //根据PKCS8编码密钥规范产生私钥对象 + PrivateKey privateKey = keyFactory.generatePrivate(pkcs8KeySpec); + //根据转换的名称获取密码对象Cipher(转换的名称:算法/工作模式/填充模式) + Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm()); + //用私钥初始化此Cipher对象(解密模式) + cipher.init(Cipher.DECRYPT_MODE, privateKey); + //对数据解密 + byte[] decrypt = cipher.doFinal(Base64.getDecoder().decode(data)); + //返回字符串 + return new String(decrypt); + } + + + + /** + * 私钥加密(用于数据签名) + * + * @param data 加密前的字符串 + * @param privateKeyStr base64编码后的私钥 + * @return base64编码后后的字符串 + * @throws Exception + */ + public static String encryptByPrivateKey(String data, String privateKeyStr) throws Exception { + //Java原生base64解码 + byte[] priKey = Base64.getDecoder().decode(privateKeyStr); + //创建PKCS8编码密钥规范 + PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(priKey); + //返回转换指定算法的KeyFactory对象 + KeyFactory keyFactory = KeyFactory.getInstance(RSA_KEY_ALGORITHM); + //根据PKCS8编码密钥规范产生私钥对象 + PrivateKey privateKey = keyFactory.generatePrivate(pkcs8KeySpec); + //根据转换的名称获取密码对象Cipher(转换的名称:算法/工作模式/填充模式) + Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm()); + //用私钥初始化此Cipher对象(加密模式) + cipher.init(Cipher.ENCRYPT_MODE, privateKey); + //对数据加密 + byte[] encrypt = cipher.doFinal(data.getBytes()); + //返回base64编码后的字符串 + return Base64.getEncoder().encodeToString(encrypt); + } + + /** + * 公钥解密(用于数据验签) + * + * @param data 解密前的字符串 + * @param publicKeyStr base64编码后的公钥 + * @return 解密后的字符串 + * @throws Exception + */ + public static String decryptByPublicKey(String data, String publicKeyStr) throws Exception { + //Java原生base64解码 + byte[] pubKey = Base64.getDecoder().decode(publicKeyStr); + //创建X509编码密钥规范 + X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(pubKey); + //返回转换指定算法的KeyFactory对象 + KeyFactory keyFactory = KeyFactory.getInstance(RSA_KEY_ALGORITHM); + //根据X509编码密钥规范产生公钥对象 + PublicKey publicKey = keyFactory.generatePublic(x509KeySpec); + //根据转换的名称获取密码对象Cipher(转换的名称:算法/工作模式/填充模式) + Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm()); + //用公钥初始化此Cipher对象(解密模式) + cipher.init(Cipher.DECRYPT_MODE, publicKey); + //对数据解密 + byte[] decrypt = cipher.doFinal(Base64.getDecoder().decode(data)); + //返回字符串 + return new String(decrypt); + } + +} + diff --git a/sgzb-modules/sgzb-material/src/main/java/com/bonus/sgzb/base/controller/MaMachineController.java b/sgzb-modules/sgzb-material/src/main/java/com/bonus/sgzb/base/controller/MaMachineController.java index d43eae9..8665e7c 100644 --- a/sgzb-modules/sgzb-material/src/main/java/com/bonus/sgzb/base/controller/MaMachineController.java +++ b/sgzb-modules/sgzb-material/src/main/java/com/bonus/sgzb/base/controller/MaMachineController.java @@ -117,4 +117,14 @@ public class MaMachineController extends BaseController { { return success(maMachineService.selectMaMachineByMaId(maId)); } + + @Log(title = "把设备推送到租赁平台", businessType = BusinessType.QUERY) + @ApiOperation(value = "把设备推送到租赁平台") + @PostMapping("/pushNotifications") + public AjaxResult pushNotifications(@RequestBody List maMachineList) + { + logger.info("MaMachineController pushNotifications 装备推送入口===="); + return maMachineService.pushNotifications(maMachineList); + } + } diff --git a/sgzb-modules/sgzb-material/src/main/java/com/bonus/sgzb/base/service/MaMachineService.java b/sgzb-modules/sgzb-material/src/main/java/com/bonus/sgzb/base/service/MaMachineService.java index d48f15b..c8b9af3 100644 --- a/sgzb-modules/sgzb-material/src/main/java/com/bonus/sgzb/base/service/MaMachineService.java +++ b/sgzb-modules/sgzb-material/src/main/java/com/bonus/sgzb/base/service/MaMachineService.java @@ -1,6 +1,7 @@ package com.bonus.sgzb.base.service; import com.bonus.sgzb.base.api.domain.MaMachine; +import com.bonus.sgzb.common.core.web.domain.AjaxResult; import java.util.List; @@ -22,4 +23,6 @@ public interface MaMachineService { public MaMachine selectMaMachineByMaId(Long maId); MaMachine getMachineByQrCode(MaMachine maMachine); + + AjaxResult pushNotifications(List maMachineList); } diff --git a/sgzb-modules/sgzb-material/src/main/java/com/bonus/sgzb/base/service/impl/MaMachineServiceImpl.java b/sgzb-modules/sgzb-material/src/main/java/com/bonus/sgzb/base/service/impl/MaMachineServiceImpl.java index 9d718f2..770b74a 100644 --- a/sgzb-modules/sgzb-material/src/main/java/com/bonus/sgzb/base/service/impl/MaMachineServiceImpl.java +++ b/sgzb-modules/sgzb-material/src/main/java/com/bonus/sgzb/base/service/impl/MaMachineServiceImpl.java @@ -1,5 +1,7 @@ package com.bonus.sgzb.base.service.impl; +import com.alibaba.fastjson2.JSONArray; +import com.alibaba.fastjson2.JSONObject; import com.bonus.sgzb.base.api.domain.MaLabelBind; import com.bonus.sgzb.base.api.domain.MaMachine; import com.bonus.sgzb.base.api.domain.MaType; @@ -8,19 +10,26 @@ import com.bonus.sgzb.base.domain.MaPropSet; import com.bonus.sgzb.base.domain.vo.IotRecordVo; import com.bonus.sgzb.base.mapper.*; import com.bonus.sgzb.base.service.MaMachineService; +import com.bonus.sgzb.common.core.constant.Constants; import com.bonus.sgzb.common.core.exception.ServiceException; +import com.bonus.sgzb.common.core.utils.HttpHelper; +import com.bonus.sgzb.common.core.utils.RsaUtil; +import com.bonus.sgzb.common.core.web.domain.AjaxResult; import com.bonus.sgzb.common.security.utils.SecurityUtils; +import com.bonus.sgzb.material.exception.ExceptionDict; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource; -import java.util.ArrayList; -import java.util.Date; -import java.util.List; +import java.util.*; @Service +@Slf4j public class MaMachineServiceImpl implements MaMachineService { - + @Value("${sgzb.zlptUrl}") + private String zlptUrl; @Resource private MaMachineMapper maMachineMapper; @@ -171,4 +180,53 @@ public class MaMachineServiceImpl implements MaMachineService { } return ma; } + + @Override + public AjaxResult pushNotifications(List maMachineList) { + log.info("MaMachineServiceImpl pushNotifications 开始处理设备推送逻辑==={}",maMachineList); + if (maMachineList.isEmpty()){ + throw new ServiceException(String.format(ExceptionDict.PARAM_IS_NULL_ERROR_MSG,"maMachineList"), ExceptionDict.PARAM_IS_NULL_ERROR); + } + //遍历推送,防止数据量过大 + String content = JSONObject.toJSONString(maMachineList); + String encrypt; + try { + encrypt = RsaUtil.encryptByPublicKey(content, Constants.publicKey); + Map map = new HashMap(); + map.put("body", encrypt); + String body = JSONObject.toJSONString(map); + String data = HttpHelper.sendHttpPost(zlptUrl, body); + log.info("dataString-=========:" + data); + //对返回的结果进行处理 + resultDataHandler(data); + + } catch (Exception e) { + // throw new RuntimeException(e); + return AjaxResult.success("请求成功!"); + } + return AjaxResult.success("请求成功!"); + } + + private void resultDataHandler(String data) throws Exception { + JSONObject object = JSONObject.parseObject(data); + System.err.println(data); + String code = object.getString("code"); + if ("200".equals(code)) { + String dataResultString = object.getString("data"); + // 数据解密 + String dataArrayString = RsaUtil.decryptByPrivateKey(dataResultString, Constants.publicKey); + log.info("dataArrayString-=========:" + dataArrayString); + JSONArray dataArray = JSONArray.parseArray(dataArrayString); + if (dataArray != null && dataArray.size() > 0) { + + + }else { + + } + + } else { + + } + + } } diff --git a/sgzb-modules/sgzb-material/src/main/resources/bootstrap-sgzb_nw_dev.yml b/sgzb-modules/sgzb-material/src/main/resources/bootstrap-sgzb_nw_dev.yml index 6916ce5..80c5b27 100644 --- a/sgzb-modules/sgzb-material/src/main/resources/bootstrap-sgzb_nw_dev.yml +++ b/sgzb-modules/sgzb-material/src/main/resources/bootstrap-sgzb_nw_dev.yml @@ -38,4 +38,5 @@ sgzb: job: settlementJobDay: 1 settlementJobCron: "0 0 1 1 * ?" + zlptUrl: http://test-rental.zhgkxt.com/proxy/item-center/supply/item/pushNotifications diff --git a/sgzb-modules/sgzb-material/src/main/resources/bootstrap-sgzb_nw_local.yml b/sgzb-modules/sgzb-material/src/main/resources/bootstrap-sgzb_nw_local.yml index d0474fe..91bbb66 100644 --- a/sgzb-modules/sgzb-material/src/main/resources/bootstrap-sgzb_nw_local.yml +++ b/sgzb-modules/sgzb-material/src/main/resources/bootstrap-sgzb_nw_local.yml @@ -38,4 +38,5 @@ sgzb: job: settlementJobDay: 1 settlementJobCron: "0 0 1 1 * ?" + zlptUrl: http://test-rental.zhgkxt.com/proxy/item-center/supply/item/pushNotifications