فهرست منبع

feat(charging): 优化充电业务接口与加密解密逻辑

- 更新AES加密工具类中的测试数据,增加 AccessToken 等字段
- 新增解密测试代码用于验证外部加密数据
- 重构 ChargingReceptionServiceImpl 中响应构建方式,提升可读性
- 修改 HmacMD5Util 工具类中时间戳测试数据
- 调整 LinkDataController 接口文档描述及返回类型注释
- 统一使用平台统一密钥进行数据加解密处理
- 修复 ResponseParmsEntity 返回值设置链式调用问题
- 为 ResponseParmsEntity 添加@JsonProperty注解以支持JSON序列化
- 补充RequestParmsEntity类注释信息
wzq 2 هفته پیش
والد
کامیت
ceabb64712

+ 30 - 30
src/main/java/com/zsElectric/boot/charging/controller/LinkDataController.java

@@ -21,7 +21,7 @@ import org.springframework.web.bind.annotation.*;
 @Slf4j
 @RestController
 @RequiredArgsConstructor
-@Tag(name = "充电业务相关接口")
+@Tag(name = "充电业务相关接口_第三方")
 @RequestMapping("/dev/v1/linkData")
 public class LinkDataController {
 
@@ -30,10 +30,11 @@ public class LinkDataController {
     private final JwtTokenUtil jwtTokenUtil;
 
     /**
-     *  获取token
-     * @author SheepHy
+     *  获取Token
+     *  Token作为全局唯一凭证,调用各接口时均需要使用,该接口作为双方获取Token的接口,双方均需要实现。
+     * @author wzq
      * @param request,参数为类则包含{@link RequestParmsEntity}
-     * @return 获取tokenVO
+     * @return ResponseParmsEntity{@link ResponseParmsEntity}
      */
     @Operation(summary = "获取token")
     @PostMapping("/query_token")
@@ -44,22 +45,22 @@ public class LinkDataController {
             //验证签名
             if (!HmacMD5Util.verify(request.getOperatorID() + request.getData() + request.getTimeStamp() + request.getSeq(),
                     ConnectivityConstants.SIG_SECRET, request.getSig())) {
-                return responseParmsEntity
-                        .setRet(4001)
-                        .setMsg("签名验证失败")
-                        .setData("")
-                        .setSig(HmacMD5Util.genSign(responseParmsEntity.getRet(), responseParmsEntity.getMsg(), responseParmsEntity.getData(),
+                 responseParmsEntity.setRet(4001);
+                responseParmsEntity.setMsg("签名验证失败");
+                responseParmsEntity.setData("");
+                responseParmsEntity.setSig(HmacMD5Util.genSign(responseParmsEntity.getRet(), responseParmsEntity.getMsg(), responseParmsEntity.getData(),
                                 ConnectivityConstants.SIG_SECRET));
+                return responseParmsEntity;
             }
 
             String data = request.getData();
             String string = AESCryptoUtils.decrypt(data, ConnectivityConstants.DATA_SECRET, ConnectivityConstants.DATA_SECRET_IV);
             QueryTokenRequestParms queryTokenRequestParms = new Gson().fromJson(string, QueryTokenRequestParms.class);
             if (queryTokenRequestParms == null || queryTokenRequestParms.getOperatorID() == null || queryTokenRequestParms.getOperatorSecret() == null) {
-                responseParmsEntity
-                        .setRet(4003)
-                        .setMsg("参数错误")
-                        .setData("");
+
+                responseParmsEntity.setRet(4003);
+                responseParmsEntity.setMsg("参数错误");
+                responseParmsEntity.setData("");
                 responseParmsEntity.setSig(HmacMD5Util.genSign(responseParmsEntity.getRet(), responseParmsEntity.getMsg(), responseParmsEntity.getData(),
                         ConnectivityConstants.SIG_SECRET));
                 return responseParmsEntity;
@@ -67,10 +68,9 @@ public class LinkDataController {
 
             //判断运营商ID与密钥是否正确
             if (!queryTokenRequestParms.getOperatorID().equals(ConnectivityConstants.PLATFORM_OPERATOR_ID) && !queryTokenRequestParms.getOperatorSecret().equals(ConnectivityConstants.PLATFORM_OPERATOR_SECRET)) {
-                responseParmsEntity
-                        .setRet(4004)
-                        .setMsg("OperatorID或OperatorSecret错误!")
-                        .setData("");
+                responseParmsEntity .setRet(4004);
+                responseParmsEntity.setMsg("OperatorID或OperatorSecret错误!");
+                responseParmsEntity .setData("");
                 responseParmsEntity.setSig(HmacMD5Util.genSign(responseParmsEntity.getRet(), responseParmsEntity.getMsg(), responseParmsEntity.getData(),
                         ConnectivityConstants.SIG_SECRET));
             }
@@ -88,22 +88,20 @@ public class LinkDataController {
 
             log.info("生成token信息:{}", new Gson().toJson(queryTokenResponseData));
 
-            String encodeData = AESCryptoUtils.encrypt(new Gson().toJson(queryTokenResponseData), ConnectivityConstants.PLATFORM_DATA_SECRET,
-                    ConnectivityConstants.PLATFORM_DATA_SECRET_IV);
+            String encodeData = AESCryptoUtils.encrypt(new Gson().toJson(queryTokenResponseData), ConnectivityConstants.DATA_SECRET,
+                    ConnectivityConstants.DATA_SECRET_IV);
 
-            responseParmsEntity
-                    .setRet(0)
-                    .setMsg("成功")
-                    .setData(encodeData)
-                    .setSig(HmacMD5Util.genSign(responseParmsEntity.getRet(), responseParmsEntity.getMsg(), responseParmsEntity.getData(),
+            responseParmsEntity.setRet(0);
+            responseParmsEntity.setMsg("成功");
+            responseParmsEntity .setData(encodeData);
+            responseParmsEntity.setSig(HmacMD5Util.genSign(responseParmsEntity.getRet(), responseParmsEntity.getMsg(), responseParmsEntity.getData(),
                             ConnectivityConstants.SIG_SECRET));
             return responseParmsEntity;
         } catch (Exception e) {
             log.error("系统错误:{}", e.getMessage());
-            responseParmsEntity
-                    .setRet(500)
-                    .setMsg("系统错误")
-                    .setData("");
+            responseParmsEntity .setRet(500);
+            responseParmsEntity.setMsg("系统错误");
+            responseParmsEntity.setData("");
             responseParmsEntity.setSig(HmacMD5Util.genSign(responseParmsEntity.getRet(), responseParmsEntity.getMsg(), responseParmsEntity.getData(),
                     ConnectivityConstants.SIG_SECRET));
             return responseParmsEntity;
@@ -115,7 +113,7 @@ public class LinkDataController {
      * <p>2.4 推送启动充电结果</p>
      * @author SheepHy
      * @param requestDTO,参数为类则包含{@link RequestParmsEntity}
-     * @return 推送启动充电结果VO
+     * @return 推送启动充电结果VO
      */
     @Operation(summary = "推送启动充电结果")
     @PostMapping("/notification_start_charge_result")
@@ -156,7 +154,9 @@ public class LinkDataController {
 
     /**
      * 3.2 设备状态变化推送
-     * */
+     * @param requestDTO
+     * @return
+     */
     @Operation(summary = "设备状态变化推送")
     @PostMapping("/notification_stationStatus")
     @Log(value = "设备状态变化推送", module = LogModuleEnum.PARKING, params = true, result = true)

+ 33 - 25
src/main/java/com/zsElectric/boot/charging/service/impl/ChargingReceptionServiceImpl.java

@@ -57,11 +57,13 @@ public class ChargingReceptionServiceImpl implements ChargingReceptionService {
             log.error("数据解密失败:{}", e.getMessage());
             throw new BusinessException("数据解密失败:" + e.getMessage(), e);
         }
-        return new ResponseParmsEntity()
-                .setRet(STATUS_OK)
-                .setMsg("请求成功")
-                .setData(encryptData)
-                .setSig(genSign);
+        ResponseParmsEntity responseParmsEntity = new ResponseParmsEntity();
+        responseParmsEntity.setRet(STATUS_OK);
+        responseParmsEntity.setMsg("请求成功");
+        responseParmsEntity.setData(encryptData);
+        responseParmsEntity.setSig(genSign);
+        return responseParmsEntity;
+
     }
 
     @Override
@@ -94,11 +96,14 @@ public class ChargingReceptionServiceImpl implements ChargingReceptionService {
             log.error("数据解密失败:{}", e.getMessage());
             throw new BusinessException("数据解密失败:" + e.getMessage(), e);
         }
-        return new ResponseParmsEntity()
-                .setRet(STATUS_OK)
-                .setMsg("请求成功")
-                .setData(encryptData)
-                .setSig(genSign);
+
+        ResponseParmsEntity responseParmsEntity = new ResponseParmsEntity();
+        responseParmsEntity.setRet(STATUS_OK);
+        responseParmsEntity.setMsg("请求成功");
+        responseParmsEntity.setData(encryptData);
+        responseParmsEntity.setSig(genSign);
+        return responseParmsEntity;
+
     }
 
     @Override
@@ -130,11 +135,14 @@ public class ChargingReceptionServiceImpl implements ChargingReceptionService {
             log.error("数据解密失败:{}", e.getMessage());
             throw new BusinessException("数据解密失败:" + e.getMessage(), e);
         }
-        return new ResponseParmsEntity()
-                .setRet(STATUS_OK)
-                .setMsg("请求成功")
-                .setData(encryptData)
-                .setSig(genSign);
+
+        ResponseParmsEntity responseParmsEntity = new ResponseParmsEntity();
+        responseParmsEntity.setRet(STATUS_OK);
+        responseParmsEntity.setMsg("请求成功");
+        responseParmsEntity.setData(encryptData);
+        responseParmsEntity.setSig(genSign);
+        return responseParmsEntity;
+
     }
 
     @Override
@@ -166,11 +174,12 @@ public class ChargingReceptionServiceImpl implements ChargingReceptionService {
             log.error("数据解密失败:{}", e.getMessage());
             throw new BusinessException("数据解密失败:" + e.getMessage(), e);
         }
-        return new ResponseParmsEntity()
-                .setRet(STATUS_OK)
-                .setMsg("请求成功")
-                .setData(encryptData)
-                .setSig(genSign);
+        ResponseParmsEntity responseParmsEntity = new ResponseParmsEntity();
+        responseParmsEntity.setRet(STATUS_OK);
+        responseParmsEntity.setMsg("请求成功");
+        responseParmsEntity.setData(encryptData);
+        responseParmsEntity.setSig(genSign);
+        return responseParmsEntity;
     }
 
     @Override
@@ -193,11 +202,10 @@ public class ChargingReceptionServiceImpl implements ChargingReceptionService {
                 encryptData = chargingUtil.encryptData(new Gson().toJson(statusMap));
                 genSign = genSign(STATUS_OK, "", encryptData, ConnectivityConstants.SIG_SECRET);
 
-                responseParmsEntity
-                        .setRet(STATUS_OK)
-                        .setMsg("")
-                        .setData(encryptData)
-                        .setSig(genSign);
+                responseParmsEntity.setRet(STATUS_OK);
+                responseParmsEntity.setMsg("");
+                responseParmsEntity.setData(encryptData);
+                responseParmsEntity .setSig(genSign);
             } else {
                 log.error("数据验签失败");
                 throw new BusinessException("数据验签失败");

+ 10 - 1
src/main/java/com/zsElectric/boot/common/util/AESCryptoUtils.java

@@ -149,7 +149,7 @@ public class AESCryptoUtils {
 
         try {
             // 测试数据
-            String originalData = "{\"OperatorID\":\"MA6DP6BE7\", \"OperatorSecret\":\"Sov2Gs590CLUbx4g\"}";
+            String originalData = "{\"OperatorID\":\"MA6DP6BE7\",\"SuccStat\":0,\"AccessToken\":\"eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJNQTZEUDZCRTciLCJpYXQiOjE3NjQ1ODE0NzQsImV4cCI6MTc2NDU4ODY3NH0.mfXp_Wly4QiVYILK6hNSOEGSU7XejlYZRing0CRc8MQW1xuUxrQQwOxeCAwI_Bnay5WzqaYYFQeVuNeignxAuQ\",\"TokenAvailableTime\":6885,\"FailReason\":0}";
             String key = DATA_SECRET;   // 16字节密钥
             String iv = DATA_SECRET_IV;   // 16字节初始化向量
 
@@ -165,6 +165,15 @@ public class AESCryptoUtils {
             System.out.println("加密结果: " + encryptedData);
             System.out.println("加密耗时: " + encryptTime + "ms");
 
+            String data = "WiZdxjtssyPEyh8q5v9ZV0OzTHBt9Pjj2v2FI9W7pZZ5Zu+gllFfpUdWi2LuXKgyvPuwbi/Z15QxavUx5kASvXqUjmiip7e7SFgtpM2ZSQ/hDUwOyMEIJJlzydzhx347r5vzy4iLls4z8HGlMUt69kr9M7s0M0VQgZqYoRwKXiYtTJbcG+rWlmd+KeZ8Lm14jBpa6oiZgjjORzx0vttrOFiaAoCyUUvvt59d4YlhL7LC+H4eFMv9cf4JnHF/RJk0f6g8ExJfLz1sPK0G8HZ0zykDc9simTjR9PPHT2OANzoSq2FW3ZCJIxGDfd7vqtEahZLb+RNozVzA2wqrl8+3UmhYRr+g800QjfyqUOmfXTGCTa8gYC4p6LkI39KihBYE";
+            System.out.println("测试的加密数据:"+data);
+
+            String key1 = DATA_SECRET;   // 16字节密钥
+            String iv1 = DATA_SECRET_IV;
+            System.out.println("密钥: " + key);
+            System.out.println("初始化向量: " + iv);
+            String string = decrypt(data, key1, iv1);
+            System.out.println("测试的解密:"+string);
             // 解密
             startTime = System.currentTimeMillis();
             String decryptedData = decrypt(encryptedData, key, iv);

+ 1 - 1
src/main/java/com/zsElectric/boot/common/util/HmacMD5Util.java

@@ -211,7 +211,7 @@ public class HmacMD5Util {
     public static void main(String[] args) {
         try {
             // 测试数据
-            String data = "MA6DP6BE7WiZdxjtssyPEyh8q5v9ZVz0fCBOQQysXhCO8S0pq/+R7UTnfBQUQKS84LWix46SCMKMAsB26Bg5EI8qyg4tvbw==202511210705520001";
+            String data = "MA6DP6BE7WiZdxjtssyPEyh8q5v9ZVz0fCBOQQysXhCO8S0pq/+R7UTnfBQUQKS84LWix46SCMKMAsB26Bg5EI8qyg4tvbw==202512010917490001";
             String key = "U9xFXjjdYAycq30C";
             
             System.out.println("=== HMAC-MD5签名测试 ===");

+ 5 - 0
src/main/java/com/zsElectric/boot/common/util/electric/RequestParmsEntity.java

@@ -7,6 +7,11 @@ import lombok.experimental.Accessors;
 import java.io.Serial;
 import java.io.Serializable;
 
+/**
+ * @author wzq
+ * @date 2025/11/25
+ * @description 请求参数实体
+ */
 @Accessors(chain = true)
 public class RequestParmsEntity implements Serializable {
 

+ 37 - 2
src/main/java/com/zsElectric/boot/common/util/electric/ResponseParmsEntity.java

@@ -1,13 +1,13 @@
 package com.zsElectric.boot.common.util.electric;
 
+import com.fasterxml.jackson.annotation.JsonProperty;
 import lombok.Data;
 import lombok.experimental.Accessors;
 
 import java.io.Serial;
 import java.io.Serializable;
 
-@Data
-@Accessors(chain = true)
+
 public class ResponseParmsEntity implements Serializable {
 
     @Serial
@@ -41,4 +41,39 @@ public class ResponseParmsEntity implements Serializable {
      */
     private String Sig;
 
+    @JsonProperty("Ret")
+    public Integer getRet() {
+        return Ret;
+    }
+
+    public void setRet(Integer ret) {
+        Ret = ret;
+    }
+
+    @JsonProperty("Msg")
+    public String getMsg() {
+        return Msg;
+    }
+
+    public void setMsg(String msg) {
+        Msg = msg;
+    }
+
+    @JsonProperty("Data")
+    public String getData() {
+        return Data;
+    }
+
+    public void setData(String data) {
+        Data = data;
+    }
+
+    @JsonProperty("Sig")
+    public String getSig() {
+        return Sig;
+    }
+
+    public void setSig(String sig) {
+        Sig = sig;
+    }
 }