Browse Source

采购 debug

tags/24080901
PoffyZhang 1 year ago
parent
commit
4cf0d6cb36
2 changed files with 39 additions and 4 deletions
  1. +17
    -0
      pmapi/src/main/java/com/ningdatech/pmapi/common/util/CryptUtils.java
  2. +22
    -4
      pmapi/src/main/java/com/ningdatech/pmapi/ding/controller/DingInfoPullController.java

+ 17
- 0
pmapi/src/main/java/com/ningdatech/pmapi/common/util/CryptUtils.java View File

@@ -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"));


+ 22
- 4
pmapi/src/main/java/com/ningdatech/pmapi/ding/controller/DingInfoPullController.java View File

@@ -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();
}
}

Loading…
Cancel
Save