InterfaceUtil.java 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. package jnpf.base.util.interfaceUtil;
  2. import cn.hutool.core.collection.CollectionUtil;
  3. import jnpf.util.DateUtil;
  4. import jnpf.util.ServletUtil;
  5. import jnpf.util.StringUtil;
  6. import org.apache.commons.codec.binary.Base64;
  7. import org.apache.commons.codec.binary.Hex;
  8. import javax.crypto.Mac;
  9. import javax.crypto.spec.SecretKeySpec;
  10. import java.io.UnsupportedEncodingException;
  11. import java.net.URLEncoder;
  12. import java.security.InvalidKeyException;
  13. import java.security.NoSuchAlgorithmException;
  14. import java.util.*;
  15. /**
  16. * 接口工具类
  17. *
  18. * @author JNPF开发平台组
  19. * @version V3.4.2
  20. * @copyright 引迈信息技术有限公司
  21. * @date 2022/6/13 10:38
  22. */
  23. public class InterfaceUtil {
  24. public static final String ALGORITH_FORMAC = "HmacSHA256";
  25. public static final String HOST = "Host";
  26. public static final String YMDATE = "YmDate";
  27. public static final String CONTENT_TYPE = " Content-Type";
  28. public static final String CHARSET_NAME = "utf-8";
  29. public static final String USERKEY = "UserKey";
  30. /**
  31. * 验证签名
  32. *
  33. * @param
  34. * @return
  35. * @copyright 引迈信息技术有限公司
  36. * @date 2022/6/14
  37. */
  38. public static boolean verifySignature(String secret, String author) throws NoSuchAlgorithmException, InvalidKeyException, UnsupportedEncodingException {
  39. String method = ServletUtil.getRequest().getMethod();
  40. String url = ServletUtil.getRequest().getRequestURI();
  41. String ymdate = ServletUtil.getRequest().getHeader(YMDATE);
  42. String host = ServletUtil.getRequest().getHeader(HOST);
  43. String source = new StringBuilder()
  44. .append(method).append('\n')
  45. .append(url).append('\n')
  46. .append(ymdate).append('\n')
  47. .append(host).append('\n').toString();
  48. Mac mac = Mac.getInstance(ALGORITH_FORMAC);
  49. SecretKeySpec secretKeySpec = new SecretKeySpec(Base64.decodeBase64(secret), ALGORITH_FORMAC);
  50. mac.init(secretKeySpec);
  51. String signature = Hex.encodeHexString(mac.doFinal(source.getBytes(CHARSET_NAME)));
  52. if (author.equals(signature)) {
  53. return true;
  54. }
  55. return false;
  56. }
  57. /**
  58. * map转 name=value&name=value格式
  59. *
  60. * @param
  61. * @return
  62. * @copyright 引迈信息技术有限公司
  63. * @date 2022/6/14
  64. */
  65. public static String createLinkStringByGet(Map<String, String> params) throws UnsupportedEncodingException {
  66. List<String> keys = new ArrayList<String>(params.keySet());
  67. Collections.sort(keys);
  68. String prestr = "";
  69. for (int i = 0; i < keys.size(); i++) {
  70. String key = keys.get(i);
  71. String value = params.get(key);
  72. value = URLEncoder.encode(value, "UTF-8");
  73. if (i == keys.size() - 1) {//拼接时,不包括最后一个&字符
  74. prestr = prestr + key + "=" + value;
  75. } else {
  76. prestr = prestr + key + "=" + value + "&";
  77. }
  78. }
  79. return prestr;
  80. }
  81. /**
  82. * 判断map内有没有指定key的值
  83. *
  84. * @param
  85. * @return
  86. * @copyright 引迈信息技术有限公司
  87. * @date 2022/6/14
  88. */
  89. public static boolean checkParam(Map<String, String> map, String str) {
  90. if (CollectionUtil.isEmpty(map)) {
  91. return false;
  92. }
  93. if (StringUtil.isEmpty(str)) {
  94. return false;
  95. }
  96. if (map.get(str) != null && StringUtil.isNotEmpty(map.get(str))) {
  97. return true;
  98. }
  99. return false;
  100. }
  101. //
  102. public static Map<String, String> getAuthorization(String intefaceId, String appId, String appSecret, Map<String, String> map) {
  103. Map<String, String> resultMap = new HashMap<>();
  104. try {
  105. String method = ServletUtil.getRequest().getMethod();
  106. String url = "/api/system/DataInterface/" + intefaceId + "/Actions/Response";
  107. String ymdate = "" + DateUtil.getNowDate().getTime();
  108. String host = ServletUtil.getRequest().getHeader(HOST);
  109. String source = new StringBuilder()
  110. .append(method).append('\n')
  111. .append(url).append('\n')
  112. .append(ymdate).append('\n')
  113. .append(host).append('\n').toString();
  114. Mac mac = Mac.getInstance(ALGORITH_FORMAC);
  115. SecretKeySpec secretKeySpec = new SecretKeySpec(Base64.decodeBase64(appSecret), ALGORITH_FORMAC);
  116. mac.init(secretKeySpec);
  117. String signature = Hex.encodeHexString(mac.doFinal(source.getBytes(CHARSET_NAME)));
  118. resultMap.put("YmDate", ymdate);
  119. resultMap.put("Authorization", appId + "::" + signature);
  120. return resultMap;
  121. } catch (Exception e) {
  122. }
  123. return resultMap;
  124. }
  125. }