no message
							parent
							
								
									8a8b403c0f
								
							
						
					
					
						commit
						6360c591b2
					
				| 
						 | 
				
			
			@ -83,14 +83,6 @@ public interface LesService {
 | 
			
		|||
     */
 | 
			
		||||
    String lesCallBackJson(String currentPositionCode,String taskCode);
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 签名json
 | 
			
		||||
     * @param app_id 每个application或者service都会有一对app id 和 secret.
 | 
			
		||||
     * @param signe 签名
 | 
			
		||||
     * @return json
 | 
			
		||||
     */
 | 
			
		||||
    String lesSigneJson(String app_id, String signe);
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * LES任务回调
 | 
			
		||||
     * @param les les
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -164,18 +164,6 @@ public class LesServiceImpl implements LesService {
 | 
			
		|||
        return jsonObject.toString();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public String lesSigneJson(String app_id, String signe) {
 | 
			
		||||
        JSONObject jsonObject = new JSONObject(true);
 | 
			
		||||
        jsonObject.put("app_id", app_id);
 | 
			
		||||
        jsonObject.put("signe", signe);
 | 
			
		||||
        jsonObject.put("timestamp", String.valueOf(System.currentTimeMillis() / 1000));
 | 
			
		||||
        jsonObject.put("hash_type", "sha256");
 | 
			
		||||
        log.info("回传LES请求参数: {}", jsonObject);
 | 
			
		||||
        return jsonObject.toString();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    @Transactional(rollbackFor = Exception.class)
 | 
			
		||||
    public void lesCallBack(Les les) {
 | 
			
		||||
| 
						 | 
				
			
			@ -198,20 +186,20 @@ public class LesServiceImpl implements LesService {
 | 
			
		|||
            String path = jsonObject.getString("path");
 | 
			
		||||
            String appId = jsonObject.getString("appId");
 | 
			
		||||
            String appSecret = jsonObject.getString("appSecret");
 | 
			
		||||
            String timestamp = String.valueOf(System.currentTimeMillis() / 1000);
 | 
			
		||||
 | 
			
		||||
            //jsonBody参数
 | 
			
		||||
            String jsonBody = this.lesCallBackJson(les.getDstPositionCode(), les.getTaskCode());
 | 
			
		||||
            json = this.lesCallBackJson(les.getDstPositionCode(), les.getTaskCode());
 | 
			
		||||
            log.info("LES回传参数: {}", json);
 | 
			
		||||
 | 
			
		||||
            //生成签名;
 | 
			
		||||
            String sign = PostRequestSigner.generateSign(host, path, jsonBody, appSecret);
 | 
			
		||||
            String sign = SignerUtils.generateSign(host, path, appId, timestamp, json, appSecret);
 | 
			
		||||
            log.info("生成的签名: {}", sign);
 | 
			
		||||
 | 
			
		||||
            //请求url
 | 
			
		||||
            String url = UrlApi.lesCallBack();
 | 
			
		||||
            String url = SignerUtils.buildFullUrl(host, path, appId, timestamp, sign);
 | 
			
		||||
            log.info("回传LES请求路径: {}", url);
 | 
			
		||||
 | 
			
		||||
            //请求参数
 | 
			
		||||
            json = this.lesSigneJson(appId, sign);
 | 
			
		||||
 | 
			
		||||
            //返回结果
 | 
			
		||||
            resultJson = HttpPostUtil.sendPostReq(url, json);
 | 
			
		||||
            if (StringUtils.isEmpty(resultJson)) {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,82 +0,0 @@
 | 
			
		|||
package com.youchain.utils;
 | 
			
		||||
 | 
			
		||||
import com.youchain.exception.BadRequestException;
 | 
			
		||||
import lombok.extern.slf4j.Slf4j;
 | 
			
		||||
 | 
			
		||||
import java.io.UnsupportedEncodingException;
 | 
			
		||||
import java.net.URLEncoder;
 | 
			
		||||
import java.nio.charset.StandardCharsets;
 | 
			
		||||
import java.security.MessageDigest;
 | 
			
		||||
import java.security.NoSuchAlgorithmException;
 | 
			
		||||
import java.util.*;
 | 
			
		||||
 | 
			
		||||
@Slf4j
 | 
			
		||||
public class PostRequestSigner {
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 生成POST请求的签名
 | 
			
		||||
     *
 | 
			
		||||
     * @param host      请求域名(不包含端口号)
 | 
			
		||||
     * @param path      请求路径
 | 
			
		||||
     * @param jsonBody  请求体的JSON字符串
 | 
			
		||||
     * @param appSecret 应用密钥
 | 
			
		||||
     * @return 生成的签名字符串(小写)
 | 
			
		||||
     */
 | 
			
		||||
    public static String generateSign(String host, String path, String jsonBody, String appSecret) {
 | 
			
		||||
        try {
 | 
			
		||||
            // 1. 构建签名源字符串
 | 
			
		||||
            String signSource = buildSignSource(host, path, jsonBody, appSecret);
 | 
			
		||||
            log.info("签名生成: {}", signSource);
 | 
			
		||||
 | 
			
		||||
            // 2. 计算SHA256哈希
 | 
			
		||||
            return sha256(signSource);
 | 
			
		||||
 | 
			
		||||
        } catch (Exception e) {
 | 
			
		||||
            throw new BadRequestException("生成签名失败: " + e.getMessage());
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 构建签名源字符串
 | 
			
		||||
     */
 | 
			
		||||
    private static String buildSignSource(String host, String path, String jsonBody, String appSecret) {
 | 
			
		||||
 | 
			
		||||
        StringBuilder signSource = new StringBuilder();
 | 
			
		||||
 | 
			
		||||
        signSource.append(path)
 | 
			
		||||
                .append("POST")
 | 
			
		||||
                .append(host);
 | 
			
		||||
 | 
			
		||||
        // 只有当jsonBody不为空时才添加参数部分
 | 
			
		||||
        if (StringUtils.isNotBlank(jsonBody)) {
 | 
			
		||||
            signSource.append("?jsonBody=").append(jsonBody);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        signSource.append(appSecret);
 | 
			
		||||
 | 
			
		||||
        return signSource.toString();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 计算字符串的SHA256哈希值
 | 
			
		||||
     *
 | 
			
		||||
     * @param input 输入字符串
 | 
			
		||||
     * @return SHA256哈希的十六进制字符串(小写)
 | 
			
		||||
     * @throws NoSuchAlgorithmException 如果SHA256算法不可用
 | 
			
		||||
     */
 | 
			
		||||
    private static String sha256(String input) throws NoSuchAlgorithmException {
 | 
			
		||||
        MessageDigest digest = MessageDigest.getInstance("SHA-256");
 | 
			
		||||
        byte[] hash = digest.digest(input.getBytes(StandardCharsets.UTF_8));
 | 
			
		||||
 | 
			
		||||
        // 转为十六进制字符串
 | 
			
		||||
        StringBuilder hexString = new StringBuilder();
 | 
			
		||||
        for (byte b : hash) {
 | 
			
		||||
            String hex = Integer.toHexString(0xff & b);
 | 
			
		||||
            if (hex.length() == 1) {
 | 
			
		||||
                hexString.append('0');
 | 
			
		||||
            }
 | 
			
		||||
            hexString.append(hex);
 | 
			
		||||
        }
 | 
			
		||||
        return hexString.toString();
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -0,0 +1,120 @@
 | 
			
		|||
package com.youchain.utils;
 | 
			
		||||
 | 
			
		||||
import com.youchain.exception.BadRequestException;
 | 
			
		||||
import lombok.extern.slf4j.Slf4j;
 | 
			
		||||
 | 
			
		||||
import java.nio.charset.StandardCharsets;
 | 
			
		||||
import java.security.MessageDigest;
 | 
			
		||||
import java.security.NoSuchAlgorithmException;
 | 
			
		||||
import java.util.*;
 | 
			
		||||
import java.net.URLEncoder;
 | 
			
		||||
import java.io.UnsupportedEncodingException;
 | 
			
		||||
 | 
			
		||||
@Slf4j
 | 
			
		||||
public class SignerUtils {
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 生成POST请求的签名
 | 
			
		||||
     *
 | 
			
		||||
     * @param host      请求的域名
 | 
			
		||||
     * @param path      请求的接口路径
 | 
			
		||||
     * @param appId     应用ID
 | 
			
		||||
     * @param timestamp 时间戳
 | 
			
		||||
     * @param jsonBody  请求的JSON Body
 | 
			
		||||
     * @param appSecret 应用密钥
 | 
			
		||||
     * @return 签名结果
 | 
			
		||||
     */
 | 
			
		||||
    public static String generateSign(String host, String path, String appId, String timestamp, String jsonBody, String appSecret) {
 | 
			
		||||
        try {
 | 
			
		||||
            // 1. 准备所有需要参与签名的参数
 | 
			
		||||
            Map<String, String> allParams = getStringStringMap(appId, timestamp, jsonBody);
 | 
			
		||||
 | 
			
		||||
            // 3. 拼接参数部分(key=value&key=value形式,需要URL编码)
 | 
			
		||||
            String paramsStr = buildParamString(allParams);
 | 
			
		||||
 | 
			
		||||
            // 3. 签名生成规则
 | 
			
		||||
            String signSource = path + "POST" + host + "?" + paramsStr + appSecret;
 | 
			
		||||
            log.info("签名规则: {}", signSource);
 | 
			
		||||
 | 
			
		||||
            return sha256(signSource);
 | 
			
		||||
        } catch (Exception e) {
 | 
			
		||||
            throw new BadRequestException("生成签名失败: " + e.getMessage());
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 构建签名参数Map
 | 
			
		||||
     */
 | 
			
		||||
    private static Map<String, String> getStringStringMap(String appId, String timestamp, String jsonBody) {
 | 
			
		||||
        Map<String, String> allParams = new TreeMap<>();
 | 
			
		||||
        allParams.put("app_id", appId);
 | 
			
		||||
        allParams.put("timestamp", timestamp);
 | 
			
		||||
        allParams.put("hash_type", "sha256");
 | 
			
		||||
        allParams.put("jsonBody", jsonBody);
 | 
			
		||||
        return allParams;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 构建参数字符串,进行URL编码
 | 
			
		||||
     */
 | 
			
		||||
    private static String buildParamString(Map<String, String> params) {
 | 
			
		||||
        StringBuilder paramsStr = new StringBuilder();
 | 
			
		||||
        for (Map.Entry<String, String> entry : params.entrySet()) {
 | 
			
		||||
            if (paramsStr.length() > 0) {
 | 
			
		||||
                paramsStr.append("&");
 | 
			
		||||
            }
 | 
			
		||||
            paramsStr.append(entry.getKey()).append("=").append(entry.getValue());
 | 
			
		||||
          /*  try {
 | 
			
		||||
                // URL编码处理特殊字符
 | 
			
		||||
                String encodedKey = URLEncoder.encode(key, StandardCharsets.UTF_8.toString());
 | 
			
		||||
                String encodedValue = URLEncoder.encode(value, StandardCharsets.UTF_8.toString());
 | 
			
		||||
                paramsStr.append(encodedKey).append("=").append(encodedValue);
 | 
			
		||||
            } catch (UnsupportedEncodingException e) {
 | 
			
		||||
                throw new BadRequestException("URL编码失败:" + e.getMessage());
 | 
			
		||||
            }*/
 | 
			
		||||
        }
 | 
			
		||||
        return paramsStr.toString();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 计算字符串的SHA256哈希值
 | 
			
		||||
     *
 | 
			
		||||
     * @param input 输入字符串
 | 
			
		||||
     * @return SHA256哈希的十六进制字符串(小写)
 | 
			
		||||
     * @throws NoSuchAlgorithmException 如果SHA256算法不可用
 | 
			
		||||
     */
 | 
			
		||||
    private static String sha256(String input) throws NoSuchAlgorithmException {
 | 
			
		||||
        MessageDigest digest = MessageDigest.getInstance("SHA-256");
 | 
			
		||||
        byte[] hash = digest.digest(input.getBytes(StandardCharsets.UTF_8));
 | 
			
		||||
 | 
			
		||||
        // 转为十六进制字符串
 | 
			
		||||
        StringBuilder hexString = new StringBuilder();
 | 
			
		||||
        for (byte b : hash) {
 | 
			
		||||
            String hex = Integer.toHexString(0xff & b);
 | 
			
		||||
            if (hex.length() == 1) {
 | 
			
		||||
                hexString.append('0');
 | 
			
		||||
            }
 | 
			
		||||
            hexString.append(hex);
 | 
			
		||||
        }
 | 
			
		||||
        return hexString.toString();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 构建完整的请求URL
 | 
			
		||||
     */
 | 
			
		||||
    public static String buildFullUrl(String host, String path, String appId, String timestamp,
 | 
			
		||||
                                      String signe) {
 | 
			
		||||
        try {
 | 
			
		||||
            return "https://" + host + path + "?"
 | 
			
		||||
                    + "app_id=" + URLEncoder.encode(appId, "UTF-8")
 | 
			
		||||
                    + "×tamp=" + URLEncoder.encode(timestamp, "UTF-8")
 | 
			
		||||
                    + "&hash_type=sha256"
 | 
			
		||||
                    + "&signe=" + URLEncoder.encode(signe, "UTF-8");
 | 
			
		||||
 | 
			
		||||
        } catch (UnsupportedEncodingException e) {
 | 
			
		||||
            throw new RuntimeException("URL编码失败", e);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
		Reference in New Issue