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;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.jsowell.common.annotation.Anonymous;
import com.jsowell.common.core.controller.BaseController;
import com.jsowell.common.response.RestApiResponse;
import com.jsowell.common.util.JWTUtils;
import com.jsowell.pile.domain.nanrui.NROrderInfo;
import com.jsowell.pile.dto.QueryStartChargeDTO;
import com.jsowell.pile.dto.QueryStationInfoDTO;
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 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.RequestMapping;
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.Map;
@@ -35,18 +46,34 @@ public class NRController extends BaseController {
* @param dto
*/
@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));
RestApiResponse<?> response = null;
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) {
logger.error("南瑞平台查询充电站信息 error", e);
response = new RestApiResponse<>(e);
}
logger.info("南瑞平台查询充电站信息 result:{}", response);
return response;
return CommonResult.failed("查询充电站信息异常");
}
@@ -55,18 +82,36 @@ public class NRController extends BaseController {
* @param dto
*/
@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));
RestApiResponse<?> response = null;
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) {
logger.error("南瑞平台查询设备接口状态 error", e);
response = new RestApiResponse<>(e);
e.printStackTrace();
}
logger.info("南瑞平台查询设备接口状态 result:{}", response);
return response;
return CommonResult.failed("查询设备接口状态异常");
}
@@ -75,17 +120,51 @@ public class NRController extends BaseController {
* @param dto
*/
@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));
RestApiResponse<?> response = null;
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) {
logger.error("南瑞平台查询充电电量信息 error", e);
response = new RestApiResponse<>(e);
}
logger.info("南瑞平台查询充电电量信息 result:{}", response);
return response;
return CommonResult.failed("查询设备接口状态异常");
}
/**
* 获取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发生异常");
}
}
}