EqbTest.java 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  1. package com.malk.boyang;
  2. import com.alibaba.fastjson.JSONObject;
  3. import java.io.File;
  4. import java.io.FileInputStream;
  5. import java.io.FileNotFoundException;
  6. import java.io.IOException;
  7. import java.nio.file.Files;
  8. import java.nio.file.Path;
  9. import java.security.MessageDigest;
  10. import java.security.NoSuchAlgorithmException;
  11. import java.text.MessageFormat;
  12. import java.util.*;
  13. import com.malk.boyang.service.BoyangService;
  14. import com.malk.boyang.utils.HTTPHelper;
  15. import com.malk.utils.UtilMap;
  16. import lombok.extern.slf4j.Slf4j;
  17. import org.apache.commons.codec.binary.Base64;
  18. import org.apache.commons.codec.binary.Hex;
  19. import org.apache.commons.codec.digest.HmacAlgorithms;
  20. import org.apache.commons.codec.digest.HmacUtils;
  21. import org.json.JSONArray;
  22. import org.junit.Test;
  23. import org.junit.runner.RunWith;
  24. import org.springframework.beans.factory.annotation.Autowired;
  25. import org.springframework.boot.test.context.SpringBootTest;
  26. import org.springframework.test.context.junit4.SpringRunner;
  27. @Slf4j
  28. @SpringBootTest
  29. @RunWith(SpringRunner.class)
  30. public class EqbTest {
  31. @Autowired
  32. private BoyangService boyangService;
  33. //测试环境
  34. // 应用ID
  35. final static String projectId = "1000004";
  36. // 应用密钥
  37. final static String secret = "96Uh7CR83NkN3TA6";
  38. // 接口调用域名
  39. final static String host = "http://122.227.225.202:9011/";
  40. //===============================================================================================
  41. //正式环境
  42. /*// 应用ID
  43. final static String projectId = "1000003";
  44. // 应用密钥
  45. final static String secret = "5V6xsY3q8JWZ9Qik";
  46. // 接口调用域名
  47. final static String host = "https://dzqz.beyond-it-service.com/";*/
  48. /*public static void main(String[] args) {
  49. // 请求签名鉴权-POST请求
  50. testPost(projectId, secret, host);
  51. // 请求签名鉴权-GET请求
  52. // testGet(projectId, secret, host);
  53. // 请求签名鉴权-POST请求-文件上传
  54. // testUpload(projectId, secret, host);
  55. }*/
  56. @Test
  57. /***
  58. * 请求签名鉴权-POST请求
  59. *
  60. * @param projectId 项目Id
  61. * @param secret 项目密钥
  62. * @param host 网关地址
  63. */
  64. public void testPost() {
  65. // 查询查询内部组织详情地址
  66. String getInnerOrganizationsDetailApi = "/manage/v1/innerOrganizations/detail";
  67. // 查询查询内部组织详情请求地址
  68. String getInnerOrganizationsDetailApiUrl = host + getInnerOrganizationsDetailApi;
  69. try {
  70. // 构建请求Body体
  71. JSONObject reqBodyObj = new JSONObject(true);
  72. reqBodyObj.put("organizationCode", "");
  73. reqBodyObj.put("customOrgNo", "");
  74. reqBodyObj.put("name", "博洋");
  75. // 请求Body体数据
  76. String reqBodyData = reqBodyObj.toString();
  77. // 对请求Body体内的数据计算ContentMD5
  78. // 构建参与请求签名计算的明文
  79. String plaintext = reqBodyData;
  80. // 计算请求签名值
  81. String reqSignature = sign(plaintext, secret);
  82. // 构建请求头
  83. LinkedHashMap<String, String> header = new LinkedHashMap<>();
  84. // 构建待签名字符串
  85. String accept = "*/*";
  86. String contentType = "application/json; charset=UTF-8";
  87. header.put("X-timevale-project-id", projectId);
  88. header.put("X-timevale-signature", reqSignature);
  89. header.put("Accept", accept);
  90. header.put("Content-Type", contentType);
  91. // 发送POST请求
  92. String result = HTTPHelper.sendPOST(getInnerOrganizationsDetailApiUrl, reqBodyData, header, "UTF-8");
  93. JSONObject resultObj = JSONObject.parseObject(result);
  94. System.out.println("请求返回信息: " + resultObj.toString());
  95. } catch (Exception e) {
  96. e.printStackTrace();
  97. String msg = MessageFormat.format("请求签名鉴权方式调用接口出现异常: {0}", e.getMessage());
  98. System.out.println(msg);
  99. }
  100. }
  101. @Test
  102. /***
  103. * 使用文件发起签署(一步发起签署)
  104. *
  105. * @param projectId 项目Id
  106. * @param secret 项目密钥
  107. * @param host 网关地址
  108. */
  109. public void testPost2() {
  110. String api = "/esign-signs/v1/signFlow/createAndStart";
  111. String getInnerOrganizationsDetailApiUrl = host + api;
  112. try {
  113. // 构建请求Body体
  114. JSONObject reqBodyObj = new JSONObject(true);
  115. // 添加基本字段
  116. reqBodyObj.put("subject", "测试api直接发起签署");
  117. reqBodyObj.put("businessNo", "R9N-KN3tTOyNDTIWbxI7LA01161763691347");
  118. reqBodyObj.put("businessTypeCode", "889726e889ab84fea3514748d6df565c");
  119. reqBodyObj.put("remark", "测试");
  120. // 构建initiatorInfo对象
  121. JSONObject initiatorInfo = new JSONObject(true);
  122. initiatorInfo.put("userCode", "wanghh");
  123. initiatorInfo.put("departmentCode", "b60a9c18b8cc4ecc80e30f36b4267a68");
  124. initiatorInfo.put("organizationCode", "b60a9c18b8cc4ecc80e30f36b4267a68");
  125. reqBodyObj.put("initiatorInfo", initiatorInfo);
  126. // 添加其他字段
  127. reqBodyObj.put("redirectUrl", "https://www.baidu.com");
  128. reqBodyObj.put("signNotifyUrl", "http://3eb3815c.r23.cpolar.top/boyang/callback");
  129. // 构建signFiles数组
  130. JSONArray signFiles = new JSONArray();
  131. JSONObject signFile = new JSONObject(true);
  132. signFile.put("fileKey", "$832b183d-b3ac-4587-9488-553b82e11eec$2419219229");
  133. signFile.put("fileOrder", 1);
  134. signFiles.put(signFile);
  135. reqBodyObj.put("signFiles", signFiles);
  136. // 构建signerInfos数组
  137. JSONArray signerInfos = new JSONArray();
  138. JSONObject signerInfo = new JSONObject(true);
  139. signerInfo.put("userType", "1");
  140. signerInfo.put("userCode", "wanghh");
  141. signerInfo.put("departmentCode", "b60a9c18b8cc4ecc80e30f36b4267a68");
  142. signerInfo.put("organizationCode", "b60a9c18b8cc4ecc80e30f36b4267a68");
  143. signerInfo.put("signNode", 1);
  144. signerInfo.put("signMode", "0");
  145. signerInfo.put("signOrder", 1);
  146. signerInfos.put(signerInfo);
  147. reqBodyObj.put("signerInfos", signerInfos);
  148. // 请求Body体数据
  149. String reqBodyData = reqBodyObj.toString();
  150. // 对请求Body体内的数据计算ContentMD5
  151. // 构建参与请求签名计算的明文
  152. String plaintext = reqBodyData;
  153. // 计算请求签名值
  154. String reqSignature = sign(plaintext, secret);
  155. // 构建请求头
  156. LinkedHashMap<String, String> header = new LinkedHashMap<>();
  157. // 构建待签名字符串
  158. String accept = "*/*";
  159. String contentType = "application/json; charset=UTF-8";
  160. header.put("X-timevale-project-id", projectId);
  161. header.put("X-timevale-signature", reqSignature);
  162. header.put("Accept", accept);
  163. header.put("Content-Type", contentType);
  164. // 发送POST请求
  165. String result = HTTPHelper.sendPOST(getInnerOrganizationsDetailApiUrl, reqBodyData, header, "UTF-8");
  166. JSONObject resultObj = JSONObject.parseObject(result);
  167. System.out.println("请求返回信息: " + resultObj.toString());
  168. } catch (Exception e) {
  169. e.printStackTrace();
  170. String msg = MessageFormat.format("请求签名鉴权方式调用接口出现异常: {0}", e.getMessage());
  171. System.out.println(msg);
  172. }
  173. }
  174. /***
  175. * 请求签名鉴权-GET请求
  176. *
  177. * @param projectId 项目id
  178. * @param secret 项目密钥
  179. * @param host 网关地址
  180. */
  181. public static void testGet(String projectId, String secret, String host) {
  182. // 获取签署地址列表API地址
  183. String getSignFlowApi = "/esign-signs/v1/signFlow/signUrls?signFlowId=" + "24210df8420968c1e785a3b7582295b6";
  184. // 获取签署地址列表接口请求地址
  185. String hostGetSignFlowApi = host + getSignFlowApi;
  186. try {
  187. // 构建待签名字符串
  188. String accept = "*/*";
  189. String contentType = "application/json; charset=UTF-8";
  190. // 构建参与请求签名计算的明文
  191. int index = getSignFlowApi.indexOf("?");
  192. String plaintext = index == -1 ? "" : getSignFlowApi.substring(index + 1);
  193. // 计算请求签名值
  194. String reqSignature = sign(plaintext, secret);
  195. // 构建请求头
  196. LinkedHashMap<String, String> header = new LinkedHashMap<>();
  197. header.put("X-timevale-project-id", projectId);
  198. header.put("X-timevale-signature", reqSignature);
  199. header.put("Accept", accept);
  200. header.put("Content-Type", contentType);
  201. // 发送GET请求
  202. String result = HTTPHelper.sendGet(hostGetSignFlowApi, header, "UTF-8");
  203. JSONObject resultObj = JSONObject.parseObject(result);
  204. System.out.println("请求返回信息: " + resultObj.toString());
  205. } catch (Exception e) {
  206. e.printStackTrace();
  207. String msg = MessageFormat.format("请求签名鉴权方式调用接口出现异常: {0}", e.getMessage());
  208. System.out.println(msg);
  209. }
  210. }
  211. /***
  212. * 查询电子签署业务模板详情
  213. */
  214. @Test
  215. public void testGet2() {
  216. String api = "/esign-docs/v1/bizTemplates/889726e889ab84fea3514748d6df565c";
  217. String hostGetSignFlowApi = host + api;
  218. try {
  219. // 构建待签名字符串
  220. String accept = "*/*";
  221. String contentType = "application/json; charset=UTF-8";
  222. // 构建参与请求签名计算的明文
  223. int index = api.indexOf("?");
  224. String plaintext = index == -1 ? "" : api.substring(index + 1);
  225. // 计算请求签名值
  226. String reqSignature = sign(plaintext, secret);
  227. // 构建请求头
  228. LinkedHashMap<String, String> header = new LinkedHashMap<>();
  229. header.put("X-timevale-project-id", projectId);
  230. header.put("X-timevale-signature", reqSignature);
  231. header.put("Accept", accept);
  232. header.put("Content-Type", contentType);
  233. // 发送GET请求
  234. String result = HTTPHelper.sendGet(hostGetSignFlowApi, header, "UTF-8");
  235. JSONObject resultObj = JSONObject.parseObject(result);
  236. System.out.println("请求返回信息: " + resultObj.toString());
  237. } catch (Exception e) {
  238. e.printStackTrace();
  239. String msg = MessageFormat.format("请求签名鉴权方式调用接口出现异常: {0}", e.getMessage());
  240. System.out.println(msg);
  241. }
  242. }
  243. @Test
  244. public void testMembersList() {
  245. // 获取签署地址列表API地址
  246. String getSignFlowApi = "/manage/v1/innerOrganizations/members/list";
  247. // 获取签署地址列表接口请求地址
  248. String hostGetSignFlowApi = host + getSignFlowApi;
  249. try {
  250. // 构建请求Body体
  251. JSONObject reqBodyObj = new JSONObject(true);
  252. reqBodyObj.put("queryScope", "0");
  253. reqBodyObj.put("organizationCode", "b60a9c18b8cc4ecc80e30f36b4267a68");
  254. reqBodyObj.put("pageNo", 1);
  255. reqBodyObj.put("pageSize", "50");
  256. // 请求Body体数据
  257. String reqBodyData = reqBodyObj.toString();
  258. // 对请求Body体内的数据计算ContentMD5
  259. // 构建参与请求签名计算的明文
  260. String plaintext = reqBodyData;
  261. // 计算请求签名值
  262. String reqSignature = sign(plaintext, secret);
  263. // 构建请求头
  264. LinkedHashMap<String, String> header = new LinkedHashMap<>();
  265. // 构建待签名字符串
  266. String accept = "*/*";
  267. String contentType = "application/json; charset=UTF-8";
  268. header.put("X-timevale-project-id", projectId);
  269. header.put("X-timevale-signature", reqSignature);
  270. header.put("Accept", accept);
  271. header.put("Content-Type", contentType);
  272. // 发送GET请求
  273. String result = HTTPHelper.sendPOST(hostGetSignFlowApi,reqBodyData, header, "UTF-8");
  274. JSONObject resultObj = JSONObject.parseObject(result);
  275. System.out.println("请求返回信息: " + resultObj.toString());
  276. } catch (Exception e) {
  277. e.printStackTrace();
  278. String msg = MessageFormat.format("请求签名鉴权方式调用接口出现异常: {0}", e.getMessage());
  279. System.out.println(msg);
  280. }
  281. }
  282. @Test
  283. public void test4(){
  284. boyangService.signed("","");
  285. }
  286. @Test
  287. public void uploadEqbFile(){
  288. String fileName = "2.png";
  289. String filePath = "C:\\Users\\EDY\\Pictures\\2.png";
  290. try {
  291. //获得文件后缀名
  292. String suffix = fileName.substring(fileName.lastIndexOf(".")+1);
  293. //生成上传文件链接
  294. String api = host + "file/v1/generateUploadUrl";
  295. JSONObject reqBodyObj = new JSONObject(true);
  296. reqBodyObj.put("requestID", UUID.randomUUID().toString().replaceAll("-",""));//请求ID,长度:32位,
  297. reqBodyObj.put("type", 1);//获取上传链接类型 0为同步上传,适用于100M以内的pdf文件。 1为异步上传,适用于大于100M的pdf文件,以及其他类型的文件:ofd、doc、docx、xls、xlsx、jpg、jpeg、png、zip、rar。
  298. Map data = eqbPost(api, reqBodyObj);
  299. String url = UtilMap.getString(data, "url");
  300. url = url.replace("http://11.0.11.62:8199/",host);
  301. // 上传文件到指定链接
  302. Map<String,Object> body = new HashMap<>();
  303. body.put("file",fileName);
  304. // 构建请求头
  305. String reqSignature2 = sign(JSONObject.toJSONString(body), secret);
  306. LinkedHashMap<String, String> header2 = new LinkedHashMap<>();
  307. // 构建待签名字符串
  308. header2.put("X-timevale-project-id", projectId);
  309. header2.put("X-timevale-signature", reqSignature2);
  310. header2.put("Accept", "*/*");
  311. String result2 = HTTPHelper.uploadFile(url, "file", filePath, body, header2, "UTF-8");
  312. Map parse = (Map) JSONObject.parse(result2);
  313. Map data1 = UtilMap.getMap(parse, "data");
  314. String fileKey = UtilMap.getString(data1, "fileKey");
  315. //若文件不是pdf则需转换
  316. if(!"pdf".equals(suffix)){
  317. //文档格式转换
  318. Map body2 = new HashMap();
  319. body2.put("fileKey", fileKey);
  320. body2.put("targetType", "pdf");
  321. Map map = eqbPost(host + "file/v1/fileConvert", body2);
  322. fileKey = UtilMap.getString(map,"convertedFileKey");
  323. }
  324. System.out.println("fileKey: " + fileKey);
  325. } catch (Exception e) {
  326. e.printStackTrace();
  327. String msg = MessageFormat.format("请求签名鉴权方式调用接口出现异常: {0}", e.getMessage());
  328. System.out.println(msg);
  329. }
  330. }
  331. private Map eqbPost(String url,Map body){
  332. try {
  333. // 请求Body体数据
  334. String reqBodyData = JSONObject.toJSONString(body);
  335. // 计算请求签名值
  336. String reqSignature = sign(reqBodyData, secret);
  337. // 构建请求头
  338. LinkedHashMap<String, String> header = new LinkedHashMap<>();
  339. // 构建待签名字符串
  340. header.put("X-timevale-project-id", projectId);
  341. header.put("X-timevale-signature", reqSignature);
  342. header.put("Accept", "*/*");
  343. header.put("Content-Type", "application/json; charset=UTF-8");
  344. String result = HTTPHelper.sendPOST(url, reqBodyData, header, "UTF-8");
  345. Map resultObj = (Map) JSONObject.parse(result);
  346. Map data = UtilMap.getMap(resultObj, "data");
  347. return data;
  348. }catch (Exception e){
  349. throw new RuntimeException(e);
  350. }
  351. }
  352. @Test
  353. public void test5(){
  354. // 构建请求Body体
  355. JSONObject reqBodyObj = new JSONObject(true);
  356. reqBodyObj.put("hash", "0748da018502f6331f10d64e645bc0b8f20d44cc7d041a9752ce586149b843d7");
  357. reqBodyObj.put("filePwd", "");
  358. // 请求Body体数据
  359. String reqBodyData = reqBodyObj.toString();
  360. // 对请求Body体内的数据计算ContentMD5
  361. // 构建参与请求签名计算的明文
  362. String plaintext = reqBodyData;
  363. // 计算请求签名值
  364. String reqSignature = sign(plaintext, secret);
  365. System.out.println(reqSignature);
  366. // 构建请求头
  367. LinkedHashMap<String, String> header = new LinkedHashMap<>();
  368. // 构建待签名字符串
  369. String accept = "*/*";
  370. String contentType = "application/json; charset=UTF-8";
  371. header.put("X-timevale-project-id", projectId);
  372. header.put("X-timevale-signature", reqSignature);
  373. header.put("Accept", accept);
  374. header.put("Content-Type", contentType);
  375. }
  376. /**
  377. * hmac-sha256签名
  378. *
  379. * @param content 代签名的内容
  380. * @param key 签名的key
  381. * @return 签名
  382. */
  383. public static String sign(String content, String key) {
  384. byte[] bytes = new HmacUtils(HmacAlgorithms.HMAC_SHA_256, key).hmac(content);
  385. return new String(Hex.encodeHex(bytes));
  386. }
  387. /**
  388. * 计算文件的SHA-256哈希值
  389. */
  390. public static String calculateSha256(File file) throws Exception {
  391. byte[] fileBytes = new byte[(int) file.length()];
  392. MessageDigest digest = MessageDigest.getInstance("SHA-256");
  393. byte[] hashBytes = digest.digest(fileBytes);
  394. // 转换为十六进制字符串
  395. StringBuilder hexString = new StringBuilder();
  396. for (byte b : hashBytes) {
  397. String hex = Integer.toHexString(0xff & b);
  398. if (hex.length() == 1) hexString.append('0');
  399. hexString.append(hex);
  400. }
  401. return hexString.toString();
  402. }
  403. /***
  404. * 请求签名鉴权-POST请求
  405. *
  406. */
  407. @Test
  408. public void testUpload() {
  409. String filePath = "d:\\母婴店.pdf";
  410. // 文件上传接口
  411. String uploadApi = "/file/v1/pdf/uploadAndSpilt";
  412. // String uploadApi = "/file/v1/uploadWithAuth";
  413. // 文件上传接口请求地址
  414. String uploadApiUrl = host + uploadApi;
  415. try {
  416. // 构建请求Body体
  417. JSONObject reqBodyObj = new JSONObject(true);
  418. reqBodyObj.put("file", "母婴店.pdf");
  419. // reqBodyObj.put("hash", calculateSha256(new File(filePath)));
  420. // reqBodyObj.put("hash", "17070b46957568b378c4c8970beceaf2d50b590ec13852a91f2eac0031bcdf96");
  421. reqBodyObj.put("filePwd", "");
  422. // 请求Body体数据
  423. String reqBodyData = reqBodyObj.toString();
  424. // 构建参与请求签名计算的明文
  425. String plaintext = reqBodyData;
  426. // 计算请求签名值
  427. String reqSignature = sign(plaintext, secret);
  428. // reqBodyObj.put("file","床上用品店.pdf");
  429. // 构建文件
  430. String fileFieldName = "file";
  431. // 构建请求头
  432. LinkedHashMap<String, String> header = new LinkedHashMap<>();
  433. // 构建待签名字符串
  434. String accept = "*/*";
  435. header.put("X-timevale-project-id", projectId);
  436. header.put("X-timevale-signature", reqSignature);
  437. header.put("Accept", accept);
  438. // 发送POST请求
  439. String result = HTTPHelper.uploadFile(uploadApiUrl, fileFieldName, filePath, reqBodyObj, header, "UTF-8");
  440. JSONObject resultObj = JSONObject.parseObject(result);
  441. System.out.println("请求返回信息: " + resultObj.toString());
  442. } catch (Exception e) {
  443. e.printStackTrace();
  444. String msg = MessageFormat.format("请求签名鉴权方式调用接口出现异常: {0}", e.getMessage());
  445. System.out.println(msg);
  446. }
  447. }
  448. @Test
  449. public void test11(){
  450. Map eqbUser = new HashMap();
  451. eqbUser.put("name", "张三");
  452. eqbUser.put("mobile", "15900010001");
  453. eqbUser.put("customAccountNo", "15900010001");
  454. eqbUser.put("mainOrganizationCode", "b60a9c18b8cc4ecc80e30f36b4267a68");//默认组织账号
  455. Map map = (Map) eqbPost2(host + "/manage/v1/innerUsers/create", Arrays.asList(eqbUser));
  456. System.out.println(map);
  457. }
  458. //e签宝-post请求(application/json)
  459. private Object eqbPost2(String url,Object body){
  460. try {
  461. // 请求Body体数据
  462. String reqBodyData = JSONObject.toJSONString(body);
  463. // 计算请求签名值
  464. String reqSignature = sign(reqBodyData, secret);
  465. // 构建请求头
  466. LinkedHashMap<String, String> header = new LinkedHashMap<>();
  467. // 构建待签名字符串
  468. header.put("X-timevale-project-id", projectId);
  469. header.put("X-timevale-signature", reqSignature);
  470. header.put("Accept", "*/*");
  471. header.put("Content-Type", "application/json; charset=UTF-8");
  472. String resultStr = HTTPHelper.sendPOST(url, reqBodyData, header, "UTF-8");
  473. Map result = (Map) JSONObject.parse(resultStr);
  474. Object data = result.get("data");
  475. return data;
  476. }catch (Exception e){
  477. throw new RuntimeException(e);
  478. }
  479. }
  480. }