no message
							parent
							
								
									d923f6e007
								
							
						
					
					
						commit
						c56d4a3f80
					
				| 
						 | 
				
			
			@ -246,8 +246,7 @@ public class NioF3AppServiceImpl implements NioF3AppService {
 | 
			
		|||
        }
 | 
			
		||||
 | 
			
		||||
        //回传LES
 | 
			
		||||
        String signe="";
 | 
			
		||||
        lesService.lesCallBack(les, lesService.lesSigneJson(UrlApi.app_id, signe));
 | 
			
		||||
        lesService.lesCallBack(les);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -91,13 +91,11 @@ public interface LesService {
 | 
			
		|||
     */
 | 
			
		||||
    String lesSigneJson(String app_id, String signe);
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * LES任务回调
 | 
			
		||||
     * @param les les
 | 
			
		||||
     * @param json 参数
 | 
			
		||||
     */
 | 
			
		||||
    void lesCallBack(Les les,String json);
 | 
			
		||||
    void lesCallBack(Les les);
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 任务到达节点记录时间
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -14,6 +14,8 @@ import com.youchain.businessdata.service.dto.LesQueryCriteria;
 | 
			
		|||
import com.youchain.businessdata.service.mapstruct.LesMapper;
 | 
			
		||||
import com.youchain.exception.BadRequestException;
 | 
			
		||||
import com.youchain.modules.system.domain.Dict;
 | 
			
		||||
import com.youchain.modules.system.domain.DictDetail;
 | 
			
		||||
import com.youchain.modules.system.repository.DictDetailRepository;
 | 
			
		||||
import com.youchain.modules.system.repository.DictRepository;
 | 
			
		||||
import com.youchain.utils.*;
 | 
			
		||||
import lombok.RequiredArgsConstructor;
 | 
			
		||||
| 
						 | 
				
			
			@ -41,6 +43,8 @@ public class LesServiceImpl implements LesService {
 | 
			
		|||
 | 
			
		||||
    private final DictRepository dictRepository;
 | 
			
		||||
 | 
			
		||||
    private final DictDetailRepository dictDetailRepository;
 | 
			
		||||
 | 
			
		||||
    private final PointService pointService;
 | 
			
		||||
 | 
			
		||||
    private final LesMapper lesMapper;
 | 
			
		||||
| 
						 | 
				
			
			@ -159,6 +163,7 @@ public class LesServiceImpl implements LesService {
 | 
			
		|||
        return jsonObject.toString();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public String lesSigneJson(String app_id, String signe) {
 | 
			
		||||
        JSONObject jsonObject = new JSONObject(true);
 | 
			
		||||
| 
						 | 
				
			
			@ -166,17 +171,48 @@ public class LesServiceImpl implements LesService {
 | 
			
		|||
        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, String json) {
 | 
			
		||||
    public void lesCallBack(Les les) {
 | 
			
		||||
        //接口开关是否启动
 | 
			
		||||
        Dict dict = dictRepository.findDictByName("OPEN");
 | 
			
		||||
        String resultJson = "";
 | 
			
		||||
        String json = "";
 | 
			
		||||
        if (dict != null) {
 | 
			
		||||
            String url="";
 | 
			
		||||
            resultJson = HttpPostUtil.sendPostReq(UrlApi.lesCallBack(), json);
 | 
			
		||||
            Dict signeDict = dictRepository.findDictByName("signe");
 | 
			
		||||
            if (signeDict == null) {
 | 
			
		||||
                throw new BadRequestException("signe签名未配置");
 | 
			
		||||
            }
 | 
			
		||||
            DictDetail dictDetail = dictDetailRepository.findDictDetailByLabel(signeDict.getId(), signeDict.getDescription());
 | 
			
		||||
            String value = dictDetail.getValue();
 | 
			
		||||
            JSONObject jsonObject = JSONObject.parseObject(value);
 | 
			
		||||
            if (jsonObject == null) {
 | 
			
		||||
                throw new BadRequestException("signe签名格式错误");
 | 
			
		||||
            }
 | 
			
		||||
            String host = jsonObject.getString("host");
 | 
			
		||||
            String path = jsonObject.getString("path");
 | 
			
		||||
            String appId = jsonObject.getString("appId");
 | 
			
		||||
            String appSecret = jsonObject.getString("appSecret");
 | 
			
		||||
 | 
			
		||||
            //json参数
 | 
			
		||||
            String jsonBody = this.lesCallBackJson(les.getDstPositionCode(), les.getTaskCode());
 | 
			
		||||
 | 
			
		||||
            //生成签名;
 | 
			
		||||
            String sign = PostRequestSigner.generateSign(host, path, jsonBody, appSecret);
 | 
			
		||||
 | 
			
		||||
            //请求url
 | 
			
		||||
            String url = UrlApi.lesCallBack();
 | 
			
		||||
            log.info("回传LES请求路径: {}", url);
 | 
			
		||||
 | 
			
		||||
            //请求参数
 | 
			
		||||
            json = this.lesSigneJson(appId, sign);
 | 
			
		||||
 | 
			
		||||
            //返回结果
 | 
			
		||||
            resultJson = HttpPostUtil.sendPostReq(url, json);
 | 
			
		||||
            if (StringUtils.isEmpty(resultJson)) {
 | 
			
		||||
                throw new BadRequestException("LES返回信息:LES回传接口调用失败!");
 | 
			
		||||
            }
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -18,6 +18,7 @@ package com.youchain.modules.system.repository;
 | 
			
		|||
import com.youchain.modules.system.domain.DictDetail;
 | 
			
		||||
import org.springframework.data.jpa.repository.JpaRepository;
 | 
			
		||||
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
 | 
			
		||||
import org.springframework.data.jpa.repository.Query;
 | 
			
		||||
 | 
			
		||||
import java.util.List;
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -29,8 +30,12 @@ public interface DictDetailRepository extends JpaRepository<DictDetail, Long>, J
 | 
			
		|||
 | 
			
		||||
    /**
 | 
			
		||||
     * 根据字典名称查询
 | 
			
		||||
     *
 | 
			
		||||
     * @param name /
 | 
			
		||||
     * @return /
 | 
			
		||||
     */
 | 
			
		||||
    List<DictDetail> findByDictName(String name);
 | 
			
		||||
 | 
			
		||||
    @Query("from DictDetail dictDetail where dictDetail.dict.id=:dictId and dictDetail.label=:label  ")
 | 
			
		||||
    DictDetail findDictDetailByLabel(Long dictId, String label);
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -1,5 +1,7 @@
 | 
			
		|||
package com.youchain.utils;
 | 
			
		||||
 | 
			
		||||
import lombok.extern.slf4j.Slf4j;
 | 
			
		||||
 | 
			
		||||
import java.io.UnsupportedEncodingException;
 | 
			
		||||
import java.net.URLEncoder;
 | 
			
		||||
import java.nio.charset.StandardCharsets;
 | 
			
		||||
| 
						 | 
				
			
			@ -7,7 +9,7 @@ import java.security.MessageDigest;
 | 
			
		|||
import java.security.NoSuchAlgorithmException;
 | 
			
		||||
import java.util.*;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@Slf4j
 | 
			
		||||
public class PostRequestSigner {
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
| 
						 | 
				
			
			@ -15,30 +17,16 @@ public class PostRequestSigner {
 | 
			
		|||
     *
 | 
			
		||||
     * @param host      请求域名(不包含端口号)
 | 
			
		||||
     * @param path      请求路径
 | 
			
		||||
     * @param urlParams   URL中的参数(不包含signe)
 | 
			
		||||
     * @param jsonBody  请求体的JSON字符串
 | 
			
		||||
     * @param appSecret 应用密钥
 | 
			
		||||
     * @param accessToken 访问令牌(可为null)
 | 
			
		||||
     * @return 生成的签名字符串(小写)
 | 
			
		||||
     */
 | 
			
		||||
    public static String generateSign(String host, String path, Map<String, String> urlParams,
 | 
			
		||||
                                      String jsonBody, String appSecret, String accessToken) {
 | 
			
		||||
    public static String generateSign(String host, String path,
 | 
			
		||||
                                      String jsonBody, String appSecret) {
 | 
			
		||||
        try {
 | 
			
		||||
            // 1. 准备所有需要参与签名的参数
 | 
			
		||||
            Map<String, String> allParams = new TreeMap<>(); // TreeMap会自动按key升序排序
 | 
			
		||||
 | 
			
		||||
            // 添加URL参数(排除signe)
 | 
			
		||||
            if (urlParams != null && !urlParams.isEmpty()) {
 | 
			
		||||
                for (Map.Entry<String, String> entry : urlParams.entrySet()) {
 | 
			
		||||
                    String key = entry.getKey();
 | 
			
		||||
                    // 跳过signe参数
 | 
			
		||||
                    if ("signe".equals(key)) {
 | 
			
		||||
                        continue;
 | 
			
		||||
                    }
 | 
			
		||||
                    allParams.put(key, entry.getValue() != null ? entry.getValue() : "");
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            // 添加JSON Body参数
 | 
			
		||||
            if (jsonBody != null && !jsonBody.isEmpty()) {
 | 
			
		||||
                allParams.put("jsonBody", jsonBody);
 | 
			
		||||
| 
						 | 
				
			
			@ -61,11 +49,8 @@ public class PostRequestSigner {
 | 
			
		|||
                    .append("?").append(paramsStr.toString()) // 参数部分
 | 
			
		||||
                    .append(appSecret);                    // appSecret
 | 
			
		||||
 | 
			
		||||
            // 如果有access_token,添加到签名字符串
 | 
			
		||||
            if (accessToken != null && !accessToken.isEmpty()) {
 | 
			
		||||
                signSource.append(accessToken);
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            log.info("签名生成: {}", signSource);
 | 
			
		||||
            // 4. 计算SHA256哈希并转为小写字符串
 | 
			
		||||
            return sha256(signSource.toString());
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -96,49 +81,4 @@ public class PostRequestSigner {
 | 
			
		|||
        }
 | 
			
		||||
        return hexString.toString();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private static String SignerUrl() {
 | 
			
		||||
        try {
 | 
			
		||||
            // 示例参数
 | 
			
		||||
            String host = "tsp-dev.nio.com";
 | 
			
		||||
            String path = "/api/1/infotainment/weather/now";
 | 
			
		||||
            String appId = "test_app_id";
 | 
			
		||||
            String appSecret = "test_app_secret";
 | 
			
		||||
            long timestamp = System.currentTimeMillis() / 1000; // Unix时间戳(秒)
 | 
			
		||||
 | 
			
		||||
            // URL参数(不包含signe)
 | 
			
		||||
            Map<String, String> urlParams = new HashMap<>();
 | 
			
		||||
            urlParams.put("app_id", appId);
 | 
			
		||||
            urlParams.put("timestamp", String.valueOf(timestamp));
 | 
			
		||||
            urlParams.put("hash_type", "sha256");
 | 
			
		||||
 | 
			
		||||
            // JSON请求体
 | 
			
		||||
            String jsonBody = "{\"location\":\"beijing\",\"language\":\"zh-CN\"}";
 | 
			
		||||
 | 
			
		||||
            // 生成签名
 | 
			
		||||
            String sign = PostRequestSigner.generateSign(host, path, urlParams, jsonBody, appSecret, null);
 | 
			
		||||
            System.out.println("生成的签名: " + sign);
 | 
			
		||||
 | 
			
		||||
            // 可以将签名添加到URL参数中,用于实际请求
 | 
			
		||||
            urlParams.put("signe", sign);
 | 
			
		||||
 | 
			
		||||
            // 构建完整的请求URL示例
 | 
			
		||||
            StringBuilder urlBuilder = new StringBuilder();
 | 
			
		||||
            urlBuilder.append("https://").append(host).append(path).append("?");
 | 
			
		||||
            for (Map.Entry<String, String> entry : urlParams.entrySet()) {
 | 
			
		||||
                urlBuilder.append(entry.getKey()).append("=")
 | 
			
		||||
                        .append(URLEncoder.encode(entry.getValue(), StandardCharsets.UTF_8.name())).append("&");
 | 
			
		||||
            }
 | 
			
		||||
            String requestUrl = urlBuilder.toString();
 | 
			
		||||
            if (requestUrl.endsWith("&")) {
 | 
			
		||||
                requestUrl = requestUrl.substring(0, requestUrl.length() - 1);
 | 
			
		||||
            }
 | 
			
		||||
            System.out.println("完整请求URL: " + requestUrl);
 | 
			
		||||
            return requestUrl;
 | 
			
		||||
        } catch (UnsupportedEncodingException e) {
 | 
			
		||||
            e.printStackTrace();
 | 
			
		||||
        }
 | 
			
		||||
        return null;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue