1
0

ServiceUtil.java 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. package com.siwei.apply.utils;
  2. import com.siwei.apply.domain.NodeAttachment;
  3. import org.apache.commons.collections4.CollectionUtils;
  4. import org.apache.commons.lang3.StringUtils;
  5. import java.net.URI;
  6. import java.security.MessageDigest;
  7. import java.util.*;
  8. /**
  9. * 服务类工具
  10. */
  11. public class ServiceUtil {
  12. private static final long EXPIRY_TIME = 10 * 60 * 1000; // 10 minutes in milliseconds
  13. private static final String SECRET_KEY = "siwei"; // Secret key for signature
  14. private static final String TIME_KEY = "?time="; // time
  15. private static final String DOMAIN = "http://10.224.1.227:15000/prod-api/apply"; // doman
  16. /**
  17. * 获取指定目录下的子children list
  18. *
  19. * @param dirName 目录名称
  20. * @param nodeAttachment 附件信息
  21. * @return
  22. */
  23. public static List<Map<String, String>> getDirChildrenList(String dirName, NodeAttachment nodeAttachment) {
  24. List<Map<String, String>> list = new ArrayList<>();
  25. if (Objects.nonNull(nodeAttachment) && Objects.nonNull(nodeAttachment.getAttachment())) {
  26. Map<String, Object> attachment = nodeAttachment.getAttachment();
  27. //第一曾经children 所有目录list
  28. List<Map> childrenList = (List<Map>) attachment.get("children");
  29. if (CollectionUtils.isNotEmpty(childrenList)) {
  30. for (Map childMap : childrenList) {
  31. String childName = (String) childMap.get("name");
  32. //if (childName.equalsIgnoreCase(dirName)) {
  33. if (StringUtils.containsIgnoreCase(childName, dirName)) {
  34. list = (List<Map<String, String>>) childMap.get("children");
  35. break;
  36. }
  37. }
  38. }
  39. }
  40. return list;
  41. }
  42. public static Map<String, Object> getDirChildrenMap(String dirName, NodeAttachment nodeAttachment) {
  43. Map<String, Object> retMap = null;
  44. if (Objects.nonNull(nodeAttachment) && Objects.nonNull(nodeAttachment.getAttachment())) {
  45. Map<String, Object> attachment = nodeAttachment.getAttachment();
  46. //第一层级children 所有目录list
  47. List<Map> childrenList = (List<Map>) attachment.get("children");
  48. if (CollectionUtils.isNotEmpty(childrenList)) {
  49. for (Map childMap : childrenList) {
  50. String childName = (String) childMap.get("name");
  51. if (StringUtils.containsIgnoreCase(childName, dirName)) {
  52. retMap = childMap;
  53. break;
  54. }
  55. }
  56. }
  57. }
  58. return retMap;
  59. }
  60. /**
  61. *
  62. * @param nodeAttachment
  63. * @throws Exception
  64. */
  65. public static void convertFilePath(NodeAttachment nodeAttachment) throws Exception {
  66. String attachmentId = nodeAttachment.getId();
  67. String timestamp = String.valueOf(System.currentTimeMillis());
  68. if (Objects.nonNull(nodeAttachment.getAttachment())) {
  69. Map<String, Object> attachment = nodeAttachment.getAttachment();
  70. //第一层级children 所有目录list
  71. List<Map> childrenList = (List<Map>) attachment.get("children");
  72. if (CollectionUtils.isNotEmpty(childrenList)) {
  73. for (Map childMap : childrenList) {
  74. String childName = (String) childMap.get("name");
  75. childMap.put("path", "--");//这里隐藏目录路径
  76. List<Map<String, String>> list = (List<Map<String, String>>) childMap.get("children");
  77. for (Map<String, String> map : list) {
  78. String fileName = map.get("name");
  79. if(StringUtils.isNotBlank(fileName)){
  80. String SignedUrl = attachmentId + "/" + childName + "/" + fileName;
  81. String signedRes = fileUrlSigned(SignedUrl, timestamp);
  82. String path = DOMAIN+ "/public/offer/download/"+signedRes+"/"+timestamp+"/"+attachmentId+"/"+childName+"/"+fileName;
  83. map.put("path", path);
  84. }
  85. }
  86. }
  87. }
  88. }
  89. }
  90. // Validate the URL by checking its timestamp and signature
  91. public static boolean isUrlValid222(String url, long originalTimestamp) {
  92. try {
  93. // Extract the timestamp and signature from the URL
  94. URI uri = URI.create(url);
  95. String query = uri.getQuery();
  96. String[] params = query.split("&");
  97. long timestamp = 0;
  98. String signature = null;
  99. // Parse timestamp and signature
  100. for (String param : params) {
  101. if (param.startsWith("timestamp=")) {
  102. timestamp = Long.parseLong(param.split("=")[1]);
  103. } else if (param.startsWith("signature=")) {
  104. signature = param.split("=")[1];
  105. }
  106. }
  107. // Check if the link is expired (10 minutes expiry)
  108. if (System.currentTimeMillis() - timestamp > EXPIRY_TIME) {
  109. return false;
  110. }
  111. // Verify the signature
  112. String originalUrl = url.split("&signature=")[0];
  113. String expectedSignature = generateSignature(originalUrl);
  114. return expectedSignature.equals(signature);
  115. } catch (Exception e) {
  116. e.printStackTrace();
  117. return false;
  118. }
  119. }
  120. // Validate the URL by checking its timestamp and signature
  121. public static boolean isUrlValid(String originalUrl, String signature, Long originalTimestamp) {
  122. try {
  123. originalUrl += TIME_KEY + originalTimestamp;
  124. // Check if the link is expired (10 minutes expiry)
  125. if (System.currentTimeMillis() - originalTimestamp > EXPIRY_TIME) {
  126. return false;
  127. }
  128. String expectedSignature = generateSignature(originalUrl);
  129. return expectedSignature.equals(signature);
  130. } catch (Exception e) {
  131. e.printStackTrace();
  132. return false;
  133. }
  134. }
  135. // Convert byte array to Hex
  136. private static String bytesToHex(byte[] bytes) {
  137. StringBuilder sb = new StringBuilder();
  138. for (byte b : bytes) {
  139. sb.append(String.format("%02x", b));
  140. }
  141. return sb.toString();
  142. }
  143. // Generate a hash-based signature for the URL
  144. private static String generateSignature(String data) throws Exception {
  145. MessageDigest digest = MessageDigest.getInstance("SHA-256");
  146. byte[] hash = digest.digest((data + SECRET_KEY).getBytes("UTF-8"));
  147. return bytesToHex(hash);
  148. }
  149. // Generate the signed URL
  150. public static String fileUrlSigned(String baseUrl, String timestamp) throws Exception {
  151. String urlWithTimestamp = baseUrl + TIME_KEY + timestamp;
  152. return generateSignature(urlWithTimestamp);
  153. }
  154. }