SkTest.java 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. package com.malk.siku;
  2. import com.malk.siku.service.SikuTaskService;
  3. import lombok.extern.slf4j.Slf4j;
  4. import org.apache.commons.codec.digest.DigestUtils;
  5. import org.junit.Test;
  6. import org.junit.runner.RunWith;
  7. import org.springframework.beans.factory.annotation.Autowired;
  8. import org.springframework.boot.test.context.SpringBootTest;
  9. import org.springframework.test.context.junit4.SpringRunner;
  10. import java.io.UnsupportedEncodingException;
  11. import java.math.BigInteger;
  12. import java.nio.charset.StandardCharsets;
  13. import java.security.MessageDigest;
  14. import java.security.NoSuchAlgorithmException;
  15. import java.util.*;
  16. @Slf4j
  17. @SpringBootTest
  18. @RunWith(SpringRunner.class)
  19. public class SkTest {
  20. @Autowired
  21. private SikuTaskService sikuTaskService;
  22. @Test
  23. public void getSecret(){
  24. //每刻报销-测试环境
  25. /*String appCode="AP52Y01LHHTAP9";
  26. String appSecret="N4WuERLteAUPaWebnsHy";*/
  27. //每刻报销-生产环境
  28. String appCode="AP52RG1SCG8S6L";
  29. String appSecret="YFTbD2MviqUwztBTbMWd";
  30. //每刻云票-测试环境
  31. /*String appCode="AP53EP1SVDS1N9";
  32. String appSecret="qF4nm3nPnyXYcrWcr5jl";*/
  33. long timeMillis = System.currentTimeMillis();
  34. log.info("time:{}",timeMillis);
  35. String s = DigestUtils.sha256Hex(appSecret + ":" + appCode + ":" + timeMillis);
  36. log.info("s:{}",s);
  37. }
  38. @Test
  39. public void ssoGetToken(){
  40. //生成token所使用的java代码
  41. String ssoSecret = "EC52NRFQYEFIB5:h34BITN5HAtVMt82P9VvTRhFYpCXLgfo";//该秘钥获取路径“设置-安全设置-系统安全策略-SSO”(若无此入口,需项目经理在boss系统开通相关许可)
  42. long timestamp = System.currentTimeMillis();
  43. String userId = "HZ769";
  44. String token = DigestUtils.sha256Hex((ssoSecret + ":" + userId + ":" + timestamp).getBytes());
  45. System.out.println("timestamp:"+timestamp);
  46. System.out.println("token:"+token);
  47. }
  48. @Test
  49. public void test(){
  50. sikuTaskService.syncLoanManage();
  51. }
  52. //java md5算法
  53. public static String md5(String plainText)
  54. {
  55. byte[] secretBytes = null;
  56. try {
  57. MessageDigest md = MessageDigest.getInstance("MD5");
  58. md.update(plainText.getBytes("utf-8"));
  59. secretBytes = md.digest();
  60. }
  61. catch (NoSuchAlgorithmException e) {
  62. throw new RuntimeException("no such algorithm!");
  63. } catch (UnsupportedEncodingException e) {
  64. throw new RuntimeException(e);
  65. }
  66. String md5code = new BigInteger(1, secretBytes).toString(16);
  67. int length = md5code.length();
  68. for (int i = 0; i < 32 - length; i++) {
  69. md5code = "0" + md5code;
  70. }
  71. return md5code;
  72. }
  73. //java sha256算法
  74. public static String sha256Hex(String input) {
  75. try {
  76. // 创建一个MessageDigest实例,并指定使用SHA-256算法
  77. MessageDigest digest = MessageDigest.getInstance("SHA-256");
  78. // 将输入字符串转换为字节数组,并更新摘要
  79. byte[] hash = digest.digest(input.getBytes(StandardCharsets.UTF_8));
  80. // 将字节数组转换为十六进制字符串
  81. StringBuilder hexString = new StringBuilder();
  82. for (byte b : hash) {
  83. String hex = Integer.toHexString(0xff & b);
  84. if (hex.length() == 1) hexString.append('0');
  85. hexString.append(hex);
  86. }
  87. return hexString.toString();
  88. } catch (NoSuchAlgorithmException e) {
  89. throw new RuntimeException(e);
  90. }
  91. }
  92. public static String genSign(HashMap<String, String> params, String signKey, Integer signMethod)
  93. {
  94. params.put("sign_key", signKey);
  95. String result = "";
  96. try {
  97. List<Map.Entry<String, String>> infoIds = new ArrayList<Map.Entry<String, String>>(params.entrySet());
  98. Collections.sort(infoIds, new Comparator<Map.Entry<String, String>>() {
  99. public int compare(Map.Entry<String, String> o1, Map.Entry<String, String> o2) {
  100. return (o1.getKey()).toString().compareTo(o2.getKey());
  101. }
  102. });
  103. // 构造签名键值对的格式
  104. for (Map.Entry<String, String> item : infoIds) {
  105. if (item.getKey() != null || item.getKey() != "") {
  106. String key = item.getKey();
  107. String val = item.getValue().trim();
  108. if (result == "") {
  109. result += key + "=" + val;
  110. } else {
  111. result += "&" + key + "=" + val;
  112. }
  113. }
  114. }
  115. //System.out.println(result);
  116. } catch(Exception e) {
  117. throw new RuntimeException("error");
  118. }
  119. if(signMethod == 1) {
  120. return sha256Hex(result);
  121. }
  122. return md5(result);
  123. }
  124. public static void main (String[] args) throws java.lang.Exception {
  125. long l = System.currentTimeMillis() / 1000;
  126. HashMap<String, String> map = new HashMap<String, String>();
  127. map.put("client_id", "3295ee5021a0dd6436d18f56e7c761c6");
  128. map.put("client_secret", "fe5539d17722035cba70a2e6220c43a4");
  129. map.put("grant_type", "client_credentials");
  130. map.put("phone", "18857526310");
  131. long currentTimeMillis = System.currentTimeMillis();
  132. map.put("timestamp", currentTimeMillis+"");
  133. String signKey = "038bD3BAb4a25D84dc96";
  134. Integer signMethod = 0;
  135. String sign = genSign(map, signKey, signMethod);
  136. System.out.println("sign:"+sign);
  137. System.out.println("timestamp:"+currentTimeMillis);
  138. }
  139. }