| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178 |
- package com.siwei.apply.utils;
- import com.siwei.apply.domain.NodeAttachment;
- import org.apache.commons.collections4.CollectionUtils;
- import org.apache.commons.lang3.StringUtils;
- import java.net.URI;
- import java.security.MessageDigest;
- import java.util.*;
- /**
- * 服务类工具
- */
- public class ServiceUtil {
- private static final long EXPIRY_TIME = 10 * 60 * 1000; // 10 minutes in milliseconds
- private static final String SECRET_KEY = "siwei"; // Secret key for signature
- private static final String TIME_KEY = "?time="; // time
- private static final String DOMAIN = "http://10.224.1.227:15000/prod-api/apply"; // doman
- /**
- * 获取指定目录下的子children list
- *
- * @param dirName 目录名称
- * @param nodeAttachment 附件信息
- * @return
- */
- public static List<Map<String, String>> getDirChildrenList(String dirName, NodeAttachment nodeAttachment) {
- List<Map<String, String>> list = new ArrayList<>();
- if (Objects.nonNull(nodeAttachment) && Objects.nonNull(nodeAttachment.getAttachment())) {
- Map<String, Object> attachment = nodeAttachment.getAttachment();
- //第一曾经children 所有目录list
- List<Map> childrenList = (List<Map>) attachment.get("children");
- if (CollectionUtils.isNotEmpty(childrenList)) {
- for (Map childMap : childrenList) {
- String childName = (String) childMap.get("name");
- //if (childName.equalsIgnoreCase(dirName)) {
- if (StringUtils.containsIgnoreCase(childName, dirName)) {
- list = (List<Map<String, String>>) childMap.get("children");
- break;
- }
- }
- }
- }
- return list;
- }
- public static Map<String, Object> getDirChildrenMap(String dirName, NodeAttachment nodeAttachment) {
- Map<String, Object> retMap = null;
- if (Objects.nonNull(nodeAttachment) && Objects.nonNull(nodeAttachment.getAttachment())) {
- Map<String, Object> attachment = nodeAttachment.getAttachment();
- //第一层级children 所有目录list
- List<Map> childrenList = (List<Map>) attachment.get("children");
- if (CollectionUtils.isNotEmpty(childrenList)) {
- for (Map childMap : childrenList) {
- String childName = (String) childMap.get("name");
- if (StringUtils.containsIgnoreCase(childName, dirName)) {
- retMap = childMap;
- break;
- }
- }
- }
- }
- return retMap;
- }
- /**
- *
- * @param nodeAttachment
- * @throws Exception
- */
- public static void convertFilePath(NodeAttachment nodeAttachment) throws Exception {
- String attachmentId = nodeAttachment.getId();
- String timestamp = String.valueOf(System.currentTimeMillis());
- if (Objects.nonNull(nodeAttachment.getAttachment())) {
- Map<String, Object> attachment = nodeAttachment.getAttachment();
- //第一层级children 所有目录list
- List<Map> childrenList = (List<Map>) attachment.get("children");
- if (CollectionUtils.isNotEmpty(childrenList)) {
- for (Map childMap : childrenList) {
- String childName = (String) childMap.get("name");
- childMap.put("path", "--");//这里隐藏目录路径
- List<Map<String, String>> list = (List<Map<String, String>>) childMap.get("children");
- for (Map<String, String> map : list) {
- String fileName = map.get("name");
- if(StringUtils.isNotBlank(fileName)){
- String SignedUrl = attachmentId + "/" + childName + "/" + fileName;
- String signedRes = fileUrlSigned(SignedUrl, timestamp);
- String path = DOMAIN+ "/public/offer/download/"+signedRes+"/"+timestamp+"/"+attachmentId+"/"+childName+"/"+fileName;
- map.put("path", path);
- }
- }
- }
- }
- }
- }
- // Validate the URL by checking its timestamp and signature
- public static boolean isUrlValid222(String url, long originalTimestamp) {
- try {
- // Extract the timestamp and signature from the URL
- URI uri = URI.create(url);
- String query = uri.getQuery();
- String[] params = query.split("&");
- long timestamp = 0;
- String signature = null;
- // Parse timestamp and signature
- for (String param : params) {
- if (param.startsWith("timestamp=")) {
- timestamp = Long.parseLong(param.split("=")[1]);
- } else if (param.startsWith("signature=")) {
- signature = param.split("=")[1];
- }
- }
- // Check if the link is expired (10 minutes expiry)
- if (System.currentTimeMillis() - timestamp > EXPIRY_TIME) {
- return false;
- }
- // Verify the signature
- String originalUrl = url.split("&signature=")[0];
- String expectedSignature = generateSignature(originalUrl);
- return expectedSignature.equals(signature);
- } catch (Exception e) {
- e.printStackTrace();
- return false;
- }
- }
- // Validate the URL by checking its timestamp and signature
- public static boolean isUrlValid(String originalUrl, String signature, Long originalTimestamp) {
- try {
- originalUrl += TIME_KEY + originalTimestamp;
- // Check if the link is expired (10 minutes expiry)
- if (System.currentTimeMillis() - originalTimestamp > EXPIRY_TIME) {
- return false;
- }
- String expectedSignature = generateSignature(originalUrl);
- return expectedSignature.equals(signature);
- } catch (Exception e) {
- e.printStackTrace();
- return false;
- }
- }
- // Convert byte array to Hex
- private static String bytesToHex(byte[] bytes) {
- StringBuilder sb = new StringBuilder();
- for (byte b : bytes) {
- sb.append(String.format("%02x", b));
- }
- return sb.toString();
- }
- // Generate a hash-based signature for the URL
- private static String generateSignature(String data) throws Exception {
- MessageDigest digest = MessageDigest.getInstance("SHA-256");
- byte[] hash = digest.digest((data + SECRET_KEY).getBytes("UTF-8"));
- return bytesToHex(hash);
- }
- // Generate the signed URL
- public static String fileUrlSigned(String baseUrl, String timestamp) throws Exception {
- String urlWithTimestamp = baseUrl + TIME_KEY + timestamp;
- return generateSignature(urlWithTimestamp);
- }
- }
|