From 4cf0d6cb363ea15a05ea9d3d90b1655566c69a8e Mon Sep 17 00:00:00 2001 From: PoffyZhang <99775271@qq.com> Date: Fri, 28 Jul 2023 10:43:39 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=87=E8=B4=AD=20debug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ningdatech/pmapi/common/util/CryptUtils.java | 17 ++++++++++++++ .../ding/controller/DingInfoPullController.java | 26 ++++++++++++++++++---- 2 files changed, 39 insertions(+), 4 deletions(-) diff --git a/pmapi/src/main/java/com/ningdatech/pmapi/common/util/CryptUtils.java b/pmapi/src/main/java/com/ningdatech/pmapi/common/util/CryptUtils.java index 2a2c0f7..9ae2328 100644 --- a/pmapi/src/main/java/com/ningdatech/pmapi/common/util/CryptUtils.java +++ b/pmapi/src/main/java/com/ningdatech/pmapi/common/util/CryptUtils.java @@ -7,6 +7,7 @@ import javax.crypto.SecretKey; import javax.crypto.spec.SecretKeySpec; import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; +import java.security.InvalidKeyException; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; @@ -61,6 +62,22 @@ public class CryptUtils { mac.init(secretKey); return mac.doFinal(data); } + + /** + * 生成HMAC-MD5值 + * @param data 消息数据 + * @param key 密钥 + * @return HMAC-MD5值 + * @throws NoSuchAlgorithmException + * @throws InvalidKeyException + */ + public static byte[] hmacMd5(byte[] data, byte[] key) + throws NoSuchAlgorithmException, InvalidKeyException { + SecretKeySpec signingKey = new SecretKeySpec(key, "HmacMD5"); + Mac mac = Mac.getInstance("HmacMD5"); + mac.init(signingKey); + return mac.doFinal(data); + } public static void main(String[] args) throws Exception{ System.out.println("HMACStr:\n" + encryptHMAC("a", "hank")); diff --git a/pmapi/src/main/java/com/ningdatech/pmapi/ding/controller/DingInfoPullController.java b/pmapi/src/main/java/com/ningdatech/pmapi/ding/controller/DingInfoPullController.java index 0f2b03a..6fa68c9 100644 --- a/pmapi/src/main/java/com/ningdatech/pmapi/ding/controller/DingInfoPullController.java +++ b/pmapi/src/main/java/com/ningdatech/pmapi/ding/controller/DingInfoPullController.java @@ -20,6 +20,9 @@ import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; import org.springframework.web.bind.annotation.*; import org.springframework.web.client.RestTemplate; import javax.annotation.Resource; +import java.io.UnsupportedEncodingException; +import java.nio.charset.StandardCharsets; +import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; import java.util.Objects; @@ -110,7 +113,7 @@ public class DingInfoPullController { @ApiOperation(value = "测试项目归并", notes = "测试项目归并") @GetMapping("/test-guibing") - public String testGuibing() { + public String testGuibing() throws NoSuchAlgorithmException, InvalidKeyException { long timestamp = System.currentTimeMillis(); String appSecret = "496f0f2a19994f76b4fd9dae087366c7"; String appKey = "A331101453557202109017383"; @@ -127,7 +130,8 @@ public class DingInfoPullController { restTemplate = new RestTemplate(); } String appId = "A330000100000202105005790"; - String capCode = MD5.create().digestHex(timestamp + appId); + String capCode = bytesToHex(CryptUtils.hmacMd5((timestamp + appId).getBytes(StandardCharsets.UTF_8), + "bcb4fc0f517fc168ee543fcf4d231f3b".getBytes(StandardCharsets.UTF_8))); String url = "https://interface.zjzwfw.gov.cn/gateway/api/proxy/001003001029/" + "dataSharing/5crv7i4727abLCv0.htm?requestTime=" + timestamp + "&sign=" + sign + "&appKey=" + appKey + @@ -141,7 +145,7 @@ public class DingInfoPullController { @ApiOperation(value = "测试项目详情推送", notes = "测试项目详情推送") @GetMapping("/test-push-project") - public String testPushProject() { + public String testPushProject() throws NoSuchAlgorithmException, InvalidKeyException { long timestamp = System.currentTimeMillis(); String appSecret = "496f0f2a19994f76b4fd9dae087366c7"; String appKey = "A331101453557202109017383"; @@ -179,7 +183,8 @@ public class DingInfoPullController { String baseManDeprtDing = "GO_9a7dce7fbaf54787998fa004f1b7c0e1"; String baseProvManDeprtDing = "GO_6c383c049d95461f9a0df780140ceb32"; String baseBasisEstablish = "中共遂昌县委办公室关于研究开发“天工”智能化管理平台(三期)的函"; - String capCode = MD5.create().digestHex(timestamp + areaCode); + String capCode = bytesToHex(CryptUtils.hmacMd5((timestamp + areaCode).getBytes(StandardCharsets.UTF_8), + "bcb4fc0f517fc168ee543fcf4d231f3b".getBytes(StandardCharsets.UTF_8))); String url = "https://interface.zjzwfw.gov.cn/gateway/api/proxy/001003001029/dataSharing/3XN9R93Pva6db7sf.htm?" + "requestTime=" + timestamp + "&sign=" + sign + "&appKey=" + appKey + "&baseProjPrincipal=" + baseProjPrincipal + "" + @@ -198,4 +203,17 @@ public class DingInfoPullController { log.info(forEntity.getBody()); return forEntity.getBody(); } + + /** + * 将byte数组转为十六进制字符串 + * @param bytes byte数组 + * @return 十六进制字符串 + */ + private static String bytesToHex(byte[] bytes) { + StringBuilder hex = new StringBuilder(); + for (byte b : bytes) { + hex.append(String.format("%02x", b)); + } + return hex.toString(); + } }