update 南瑞平台接口做加密处理

This commit is contained in:
Lemon
2023-10-14 14:45:27 +08:00
parent 89e9713781
commit 45da1f1ed1
4 changed files with 169 additions and 36 deletions

View File

@@ -1,18 +1,29 @@
package com.jsowell.thirdparty.nanrui; package com.jsowell.thirdparty.nanrui;
import com.alibaba.fastjson2.JSON; import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.jsowell.common.annotation.Anonymous; import com.jsowell.common.annotation.Anonymous;
import com.jsowell.common.core.controller.BaseController; import com.jsowell.common.core.controller.BaseController;
import com.jsowell.common.response.RestApiResponse; import com.jsowell.common.response.RestApiResponse;
import com.jsowell.common.util.JWTUtils;
import com.jsowell.pile.domain.nanrui.NROrderInfo; import com.jsowell.pile.domain.nanrui.NROrderInfo;
import com.jsowell.pile.dto.QueryStartChargeDTO;
import com.jsowell.pile.dto.QueryStationInfoDTO; import com.jsowell.pile.dto.QueryStationInfoDTO;
import com.jsowell.pile.dto.nanrui.NRQueryOrderDTO; import com.jsowell.pile.dto.nanrui.NRQueryOrderDTO;
import com.jsowell.thirdparty.lianlian.common.CommonResult;
import com.jsowell.thirdparty.lianlian.dto.CommonParamsDTO;
import com.jsowell.thirdparty.lianlian.util.Cryptos;
import com.jsowell.thirdparty.lianlian.util.Encodes;
import com.jsowell.thirdparty.nanrui.service.NRService; import com.jsowell.thirdparty.nanrui.service.NRService;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@@ -35,18 +46,34 @@ public class NRController extends BaseController {
* @param dto * @param dto
*/ */
@RequestMapping("/v1/query_stations_info") @RequestMapping("/v1/query_stations_info")
public RestApiResponse<?> query_stations_info(@RequestBody QueryStationInfoDTO dto) { public CommonResult<?> query_stations_info(HttpServletRequest request, @RequestBody CommonParamsDTO dto) {
logger.info("南瑞平台查询充电站信息 params:{}", JSON.toJSONString(dto)); logger.info("南瑞平台查询充电站信息 params:{}", JSON.toJSONString(dto));
RestApiResponse<?> response = null;
try { try {
Map<String, Object> map = nrService.query_stations_info(dto); // 校验令牌
response = new RestApiResponse<>(map); String token = request.getHeader("Authorization");
if (!JWTUtils.checkThirdPartyToken(token)) {
// 校验失败
return CommonResult.failed("令牌校验错误");
}
// 校验签名
Map<String, String> resultMap = nrService.checkoutSign(dto);
if (resultMap == null) {
// 签名错误
return CommonResult.failed("签名校验错误");
}
String operatorSecret = resultMap.get("OperatorSecret");
String dataString = resultMap.get("Data");
// 解密data
byte[] plainText = Cryptos.aesDecrypt(Encodes.decodeBase64(dataString), operatorSecret.getBytes(), operatorSecret.getBytes());
String dataStr = new String(plainText, StandardCharsets.UTF_8);
// 转换成相应对象
QueryStationInfoDTO queryStationInfoDTO = JSONObject.parseObject(dataStr, QueryStationInfoDTO.class);
Map<String, String> map = nrService.query_stations_info(queryStationInfoDTO);
return CommonResult.success(0, "请求停止充电成功!", map.get("Data"), map.get("Sig"));
} catch (Exception e) { } catch (Exception e) {
logger.error("南瑞平台查询充电站信息 error", e); logger.error("南瑞平台查询充电站信息 error", e);
response = new RestApiResponse<>(e);
} }
logger.info("南瑞平台查询充电站信息 result:{}", response); return CommonResult.failed("查询充电站信息异常");
return response;
} }
@@ -55,18 +82,36 @@ public class NRController extends BaseController {
* @param dto * @param dto
*/ */
@RequestMapping("/v1/query_station_status") @RequestMapping("/v1/query_station_status")
public RestApiResponse<?> query_station_status(@RequestBody QueryStationInfoDTO dto) { public CommonResult<?> query_station_status(HttpServletRequest request, @RequestBody CommonParamsDTO dto) {
logger.info("南瑞平台查询设备接口状态 params:{}", JSON.toJSONString(dto)); logger.info("南瑞平台查询设备接口状态 params:{}", JSON.toJSONString(dto));
RestApiResponse<?> response = null;
try { try {
Map<String, Object> map = nrService.query_station_status(dto.getStationIds()); // 校验令牌
response = new RestApiResponse<>(map); String token = request.getHeader("Authorization");
if (!JWTUtils.checkThirdPartyToken(token)) {
// 校验失败
return CommonResult.failed("令牌校验错误");
}
// 校验签名
Map<String, String> resultMap = nrService.checkoutSign(dto);
if (resultMap == null) {
// 签名错误
return CommonResult.failed("签名校验错误");
}
String operatorSecret = resultMap.get("OperatorSecret");
String dataString = resultMap.get("Data");
// 解密data
byte[] plainText = Cryptos.aesDecrypt(Encodes.decodeBase64(dataString), operatorSecret.getBytes(), operatorSecret.getBytes());
String dataStr = new String(plainText, StandardCharsets.UTF_8);
// 转换成相应对象
QueryStationInfoDTO queryStationInfoDTO = JSONObject.parseObject(dataStr, QueryStationInfoDTO.class);
queryStationInfoDTO.setOperatorId(dto.getOperatorID());
Map<String, String> map = nrService.query_station_status(queryStationInfoDTO);
return CommonResult.success(0, "查询设备接口状态成功!", map.get("Data"), map.get("Sig"));
} catch (Exception e) { } catch (Exception e) {
logger.error("南瑞平台查询设备接口状态 error", e); logger.error("南瑞平台查询设备接口状态 error", e);
response = new RestApiResponse<>(e); e.printStackTrace();
} }
logger.info("南瑞平台查询设备接口状态 result:{}", response); return CommonResult.failed("查询设备接口状态异常");
return response;
} }
@@ -75,17 +120,51 @@ public class NRController extends BaseController {
* @param dto * @param dto
*/ */
@RequestMapping("/v1/query_order_info") @RequestMapping("/v1/query_order_info")
public RestApiResponse<?> query_order_info(@RequestBody NRQueryOrderDTO dto) { public CommonResult<?> query_order_info(HttpServletRequest request, @RequestBody CommonParamsDTO dto) {
logger.info("南瑞平台查询充电电量信息 params:{}", JSON.toJSONString(dto)); logger.info("南瑞平台查询充电电量信息 params:{}", JSON.toJSONString(dto));
RestApiResponse<?> response = null;
try { try {
List<NROrderInfo> nrOrderInfos = nrService.query_order_info(dto); // 校验令牌
response = new RestApiResponse<>(nrOrderInfos); String token = request.getHeader("Authorization");
if (!JWTUtils.checkThirdPartyToken(token)) {
// 校验失败
return CommonResult.failed("令牌校验错误");
}
// 校验签名
Map<String, String> resultMap = nrService.checkoutSign(dto);
if (resultMap == null) {
// 签名错误
return CommonResult.failed("签名校验错误");
}
String operatorSecret = resultMap.get("OperatorSecret");
String dataString = resultMap.get("Data");
// 解密data
byte[] plainText = Cryptos.aesDecrypt(Encodes.decodeBase64(dataString), operatorSecret.getBytes(), operatorSecret.getBytes());
String dataStr = new String(plainText, StandardCharsets.UTF_8);
// 转换成相应对象
NRQueryOrderDTO nrQueryOrderDTO = JSONObject.parseObject(dataStr, NRQueryOrderDTO.class);
nrQueryOrderDTO.setOperatorId(dto.getOperatorID());
Map<String, String> map = nrService.query_order_info(nrQueryOrderDTO);
return CommonResult.success(0, "查询充电电量信息成功!", map.get("Data"), map.get("Sig"));
} catch (Exception e) { } catch (Exception e) {
logger.error("南瑞平台查询充电电量信息 error", e); logger.error("南瑞平台查询充电电量信息 error", e);
response = new RestApiResponse<>(e);
} }
logger.info("南瑞平台查询充电电量信息 result:{}", response); return CommonResult.failed("查询设备接口状态异常");
return response; }
/**
* 获取token接口
* http://localhost:8080/nanrui/v1/query_token
*/
@PostMapping("/v1/query_token")
public CommonResult<?> queryToken(@RequestBody CommonParamsDTO dto) {
logger.info("南瑞平台请求令牌 params:{}", JSONObject.toJSONString(dto));
try {
Map<String, String> map = nrService.generateToken(dto);
logger.info("南瑞平台请求令牌 result:{}", JSONObject.toJSONString(map));
return CommonResult.success(0, "请求令牌成功!", map.get("Data"), map.get("Sig"));
} catch (UnsupportedEncodingException e) {
logger.error("获取token接口 异常");
return CommonResult.failed("获取token发生异常");
}
} }
} }

View File

@@ -13,6 +13,8 @@ import lombok.Data;
@Data @Data
public class NRQueryOrderDTO { public class NRQueryOrderDTO {
private String operatorId;
private String orderCode; private String orderCode;
/** /**

View File

@@ -59,7 +59,7 @@ public interface NRService {
* @param dto * @param dto
* @return * @return
*/ */
Map<String, Object> query_stations_info(QueryStationInfoDTO dto); Map<String, String> query_stations_info(QueryStationInfoDTO dto);
/** /**
* 推送告警信息 * 推送告警信息
@@ -82,10 +82,10 @@ public interface NRService {
* 查询设备接口状态 * 查询设备接口状态
* 此接口用于批量查询设备实时状态 * 此接口用于批量查询设备实时状态
* 由充电运营商方实现此接口,省、市两级监管平台调用。 * 由充电运营商方实现此接口,省、市两级监管平台调用。
* @param stationIds * @param dto
* @return * @return
*/ */
Map<String, Object> query_station_status(List<String> stationIds); Map<String, String> query_station_status(QueryStationInfoDTO dto);
/** /**
@@ -102,5 +102,5 @@ public interface NRService {
* @param dto * @param dto
* @return * @return
*/ */
List<NROrderInfo> query_order_info(NRQueryOrderDTO dto); Map<String, String> query_order_info(NRQueryOrderDTO dto);
} }

View File

@@ -6,6 +6,7 @@ import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject; import com.alibaba.fastjson2.JSONObject;
import com.github.pagehelper.PageInfo; import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.jsowell.common.constant.CacheConstants; import com.jsowell.common.constant.CacheConstants;
import com.jsowell.common.constant.Constants; import com.jsowell.common.constant.Constants;
import com.jsowell.common.core.domain.ykc.RealTimeMonitorData; import com.jsowell.common.core.domain.ykc.RealTimeMonitorData;
@@ -79,6 +80,9 @@ public class NRServiceImpl implements NRService {
@Autowired @Autowired
private IThirdPartyStationRelationService thirdPartyStationRelationService; private IThirdPartyStationRelationService thirdPartyStationRelationService;
@Autowired
private IThirdPartyPlatformConfigService thirdPartyPlatformConfigService;
@Autowired @Autowired
private LianLianService lianLianService; private LianLianService lianLianService;
@@ -202,7 +206,7 @@ public class NRServiceImpl implements NRService {
* @return * @return
*/ */
@Override @Override
public Map<String, Object> query_stations_info(QueryStationInfoDTO dto) { public Map<String, String> query_stations_info(QueryStationInfoDTO dto) {
List<NRStationInfo> resultList = new ArrayList<>(); List<NRStationInfo> resultList = new ArrayList<>();
int pageNo = dto.getPageNo() == null ? 1 : dto.getPageNo(); int pageNo = dto.getPageNo() == null ? 1 : dto.getPageNo();
int pageSize = dto.getPageSize() == null ? 10 : dto.getPageSize(); int pageSize = dto.getPageSize() == null ? 10 : dto.getPageSize();
@@ -213,6 +217,10 @@ public class NRServiceImpl implements NRService {
// 未查到数据 // 未查到数据
return null; return null;
} }
ThirdPartyPlatformConfig configInfo = thirdPartyPlatformConfigService.getInfoByOperatorId(dto.getOperatorId());
if (configInfo == null) {
return null;
}
PageInfo<PileStationInfo> pageInfo = new PageInfo<>(stationInfos); PageInfo<PileStationInfo> pageInfo = new PageInfo<>(stationInfos);
for (PileStationInfo pileStationInfo : pageInfo.getList()) { for (PileStationInfo pileStationInfo : pageInfo.getList()) {
// 拼装参数 // 拼装参数
@@ -273,7 +281,19 @@ public class NRServiceImpl implements NRService {
map.put("ItemSize", resultList.size()); map.put("ItemSize", resultList.size());
map.put("StationInfos", resultList); map.put("StationInfos", resultList);
return map; // 加密
Map<String, String> resultMap = Maps.newLinkedHashMap();
// 加密数据
byte[] encryptText = Cryptos.aesEncrypt(JSONObject.toJSONString(map).getBytes(),
configInfo.getOperatorSecret().getBytes(), configInfo.getDataSecretIv().getBytes());
String encryptData = Encodes.encodeBase64(encryptText);
resultMap.put("Data", encryptData);
// 生成sig
String resultSign = GBSignUtils.sign(resultMap, configInfo.getOperatorSecret());
resultMap.put("Sig", resultSign);
return resultMap;
} }
@@ -404,22 +424,26 @@ public class NRServiceImpl implements NRService {
* 查询设备接口状态 * 查询设备接口状态
* 此接口用于批量查询设备实时状态 * 此接口用于批量查询设备实时状态
* 由充电运营商方实现此接口,省、市两级监管平台调用。 * 由充电运营商方实现此接口,省、市两级监管平台调用。
* @param stationIds * @param dto
* @return * @return
*/ */
@Override @Override
public Map<String, Object> query_station_status(List<String> stationIds) { public Map<String, String> query_station_status(QueryStationInfoDTO dto) {
List<String> stationIds = dto.getStationIds();
List<NRStationStatusInfo> resultList = new ArrayList<>(); List<NRStationStatusInfo> resultList = new ArrayList<>();
ThirdPartyPlatformConfig configInfo = thirdPartyPlatformConfigService.getInfoByOperatorId(dto.getOperatorId());
if (configInfo == null) {
return null;
}
// 将 stationIdList 转换成 List<Long> // 将 stationIdList 转换成 List<Long>
List<Long> stationLongList = stationIds.stream() List<Long> stationLongList = stationIds.stream()
.map(Long::parseLong) .map(Long::parseLong)
.collect(Collectors.toList()); .collect(Collectors.toList());
QueryConnectorListDTO dto = QueryConnectorListDTO.builder() QueryConnectorListDTO queryConnectorListDTO = QueryConnectorListDTO.builder()
.stationIdList(stationLongList) .stationIdList(stationLongList)
.build(); .build();
List<PileConnectorInfoVO> connectorInfoVOS = pileConnectorInfoService.getConnectorInfoListByParams(dto); List<PileConnectorInfoVO> connectorInfoVOS = pileConnectorInfoService.getConnectorInfoListByParams(queryConnectorListDTO);
if (CollectionUtils.isEmpty(connectorInfoVOS)) { if (CollectionUtils.isEmpty(connectorInfoVOS)) {
return new LinkedHashMap<>(); return new LinkedHashMap<>();
} }
@@ -469,7 +493,19 @@ public class NRServiceImpl implements NRService {
Map<String, Object> map = new LinkedHashMap<>(); Map<String, Object> map = new LinkedHashMap<>();
map.put("StationStatusInfos", resultList); map.put("StationStatusInfos", resultList);
return map; // 加密
Map<String, String> resultMap = Maps.newLinkedHashMap();
// 加密数据
byte[] encryptText = Cryptos.aesEncrypt(JSONObject.toJSONString(map).getBytes(),
configInfo.getOperatorSecret().getBytes(), configInfo.getDataSecretIv().getBytes());
String encryptData = Encodes.encodeBase64(encryptText);
resultMap.put("Data", encryptData);
// 生成sig
String resultSign = GBSignUtils.sign(resultMap, configInfo.getOperatorSecret());
resultMap.put("Sig", resultSign);
return resultMap;
} }
/** /**
@@ -523,17 +559,33 @@ public class NRServiceImpl implements NRService {
* @return * @return
*/ */
@Override @Override
public List<NROrderInfo> query_order_info(NRQueryOrderDTO dto) { public Map<String, String> query_order_info(NRQueryOrderDTO dto) {
List<NROrderInfo> resultList = new ArrayList<>(); List<NROrderInfo> resultList = new ArrayList<>();
ThirdPartyPlatformConfig configInfo = thirdPartyPlatformConfigService.getInfoByOperatorId(dto.getOperatorId());
if (configInfo == null) {
return null;
}
List<NROrderInfoVO> nrOrderInfos = orderBasicInfoService.getNROrderInfos(dto); List<NROrderInfoVO> nrOrderInfos = orderBasicInfoService.getNROrderInfos(dto);
if (CollectionUtils.isEmpty(nrOrderInfos)) { if (CollectionUtils.isEmpty(nrOrderInfos)) {
return new ArrayList<>(); return Maps.newLinkedHashMap();
} }
for (NROrderInfoVO nrOrderInfoVO : nrOrderInfos) { for (NROrderInfoVO nrOrderInfoVO : nrOrderInfos) {
NROrderInfo nrOrderInfo = formatNROrderInfo(nrOrderInfoVO); NROrderInfo nrOrderInfo = formatNROrderInfo(nrOrderInfoVO);
resultList.add(nrOrderInfo); resultList.add(nrOrderInfo);
} }
return resultList; // 加密
Map<String, String> resultMap = Maps.newLinkedHashMap();
// 加密数据
byte[] encryptText = Cryptos.aesEncrypt(JSONObject.toJSONString(resultList).getBytes(),
configInfo.getOperatorSecret().getBytes(), configInfo.getDataSecretIv().getBytes());
String encryptData = Encodes.encodeBase64(encryptText);
resultMap.put("Data", encryptData);
// 生成sig
String resultSign = GBSignUtils.sign(resultMap, configInfo.getOperatorSecret());
resultMap.put("Sig", resultSign);
return resultMap;
} }
/** /**