推送装备
This commit is contained in:
parent
7d12bce02e
commit
dc5136bee4
|
|
@ -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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
@ -117,4 +117,14 @@ public class MaMachineController extends BaseController {
|
||||||
{
|
{
|
||||||
return success(maMachineService.selectMaMachineByMaId(maId));
|
return success(maMachineService.selectMaMachineByMaId(maId));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Log(title = "把设备推送到租赁平台", businessType = BusinessType.QUERY)
|
||||||
|
@ApiOperation(value = "把设备推送到租赁平台")
|
||||||
|
@PostMapping("/pushNotifications")
|
||||||
|
public AjaxResult pushNotifications(@RequestBody List<MaMachine> maMachineList)
|
||||||
|
{
|
||||||
|
logger.info("MaMachineController pushNotifications 装备推送入口====");
|
||||||
|
return maMachineService.pushNotifications(maMachineList);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
package com.bonus.sgzb.base.service;
|
package com.bonus.sgzb.base.service;
|
||||||
|
|
||||||
import com.bonus.sgzb.base.api.domain.MaMachine;
|
import com.bonus.sgzb.base.api.domain.MaMachine;
|
||||||
|
import com.bonus.sgzb.common.core.web.domain.AjaxResult;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
|
@ -22,4 +23,6 @@ public interface MaMachineService {
|
||||||
public MaMachine selectMaMachineByMaId(Long maId);
|
public MaMachine selectMaMachineByMaId(Long maId);
|
||||||
|
|
||||||
MaMachine getMachineByQrCode(MaMachine maMachine);
|
MaMachine getMachineByQrCode(MaMachine maMachine);
|
||||||
|
|
||||||
|
AjaxResult pushNotifications(List<MaMachine> maMachineList);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,7 @@
|
||||||
package com.bonus.sgzb.base.service.impl;
|
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.MaLabelBind;
|
||||||
import com.bonus.sgzb.base.api.domain.MaMachine;
|
import com.bonus.sgzb.base.api.domain.MaMachine;
|
||||||
import com.bonus.sgzb.base.api.domain.MaType;
|
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.domain.vo.IotRecordVo;
|
||||||
import com.bonus.sgzb.base.mapper.*;
|
import com.bonus.sgzb.base.mapper.*;
|
||||||
import com.bonus.sgzb.base.service.MaMachineService;
|
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.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.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.stereotype.Service;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
import java.util.ArrayList;
|
import java.util.*;
|
||||||
import java.util.Date;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
|
@Slf4j
|
||||||
public class MaMachineServiceImpl implements MaMachineService {
|
public class MaMachineServiceImpl implements MaMachineService {
|
||||||
|
@Value("${sgzb.zlptUrl}")
|
||||||
|
private String zlptUrl;
|
||||||
@Resource
|
@Resource
|
||||||
private MaMachineMapper maMachineMapper;
|
private MaMachineMapper maMachineMapper;
|
||||||
|
|
||||||
|
|
@ -171,4 +180,53 @@ public class MaMachineServiceImpl implements MaMachineService {
|
||||||
}
|
}
|
||||||
return ma;
|
return ma;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public AjaxResult pushNotifications(List<MaMachine> 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<String, String> map = new HashMap<String, String>();
|
||||||
|
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 {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -38,4 +38,5 @@ sgzb:
|
||||||
job:
|
job:
|
||||||
settlementJobDay: 1
|
settlementJobDay: 1
|
||||||
settlementJobCron: "0 0 1 1 * ?"
|
settlementJobCron: "0 0 1 1 * ?"
|
||||||
|
zlptUrl: http://test-rental.zhgkxt.com/proxy/item-center/supply/item/pushNotifications
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -38,4 +38,5 @@ sgzb:
|
||||||
job:
|
job:
|
||||||
settlementJobDay: 1
|
settlementJobDay: 1
|
||||||
settlementJobCron: "0 0 1 1 * ?"
|
settlementJobCron: "0 0 1 1 * ?"
|
||||||
|
zlptUrl: http://test-rental.zhgkxt.com/proxy/item-center/supply/item/pushNotifications
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue