no message
parent
d923f6e007
commit
c56d4a3f80
|
|
@ -246,8 +246,7 @@ public class NioF3AppServiceImpl implements NioF3AppService {
|
||||||
}
|
}
|
||||||
|
|
||||||
//回传LES
|
//回传LES
|
||||||
String signe="";
|
lesService.lesCallBack(les);
|
||||||
lesService.lesCallBack(les, lesService.lesSigneJson(UrlApi.app_id, signe));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
|
|
@ -91,13 +91,11 @@ public interface LesService {
|
||||||
*/
|
*/
|
||||||
String lesSigneJson(String app_id, String signe);
|
String lesSigneJson(String app_id, String signe);
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* LES任务回调
|
* LES任务回调
|
||||||
* @param les 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.businessdata.service.mapstruct.LesMapper;
|
||||||
import com.youchain.exception.BadRequestException;
|
import com.youchain.exception.BadRequestException;
|
||||||
import com.youchain.modules.system.domain.Dict;
|
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.modules.system.repository.DictRepository;
|
||||||
import com.youchain.utils.*;
|
import com.youchain.utils.*;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
|
|
@ -41,6 +43,8 @@ public class LesServiceImpl implements LesService {
|
||||||
|
|
||||||
private final DictRepository dictRepository;
|
private final DictRepository dictRepository;
|
||||||
|
|
||||||
|
private final DictDetailRepository dictDetailRepository;
|
||||||
|
|
||||||
private final PointService pointService;
|
private final PointService pointService;
|
||||||
|
|
||||||
private final LesMapper lesMapper;
|
private final LesMapper lesMapper;
|
||||||
|
|
@ -159,6 +163,7 @@ public class LesServiceImpl implements LesService {
|
||||||
return jsonObject.toString();
|
return jsonObject.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String lesSigneJson(String app_id, String signe) {
|
public String lesSigneJson(String app_id, String signe) {
|
||||||
JSONObject jsonObject = new JSONObject(true);
|
JSONObject jsonObject = new JSONObject(true);
|
||||||
|
|
@ -166,17 +171,48 @@ public class LesServiceImpl implements LesService {
|
||||||
jsonObject.put("signe", signe);
|
jsonObject.put("signe", signe);
|
||||||
jsonObject.put("timestamp", String.valueOf(System.currentTimeMillis() / 1000));
|
jsonObject.put("timestamp", String.valueOf(System.currentTimeMillis() / 1000));
|
||||||
jsonObject.put("hash_type", "sha256");
|
jsonObject.put("hash_type", "sha256");
|
||||||
|
log.info("回传LES请求参数: {}", jsonObject);
|
||||||
return jsonObject.toString();
|
return jsonObject.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@Transactional(rollbackFor = Exception.class)
|
@Transactional(rollbackFor = Exception.class)
|
||||||
public void lesCallBack(Les les, String json) {
|
public void lesCallBack(Les les) {
|
||||||
|
//接口开关是否启动
|
||||||
Dict dict = dictRepository.findDictByName("OPEN");
|
Dict dict = dictRepository.findDictByName("OPEN");
|
||||||
String resultJson = "";
|
String resultJson = "";
|
||||||
|
String json = "";
|
||||||
if (dict != null) {
|
if (dict != null) {
|
||||||
String url="";
|
Dict signeDict = dictRepository.findDictByName("signe");
|
||||||
resultJson = HttpPostUtil.sendPostReq(UrlApi.lesCallBack(), json);
|
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)) {
|
if (StringUtils.isEmpty(resultJson)) {
|
||||||
throw new BadRequestException("LES返回信息:LES回传接口调用失败!");
|
throw new BadRequestException("LES返回信息:LES回传接口调用失败!");
|
||||||
}
|
}
|
||||||
|
|
@ -207,7 +243,7 @@ public class LesServiceImpl implements LesService {
|
||||||
throw new BadRequestException(currentPositionCode + "停靠点未识别到任务");
|
throw new BadRequestException(currentPositionCode + "停靠点未识别到任务");
|
||||||
}
|
}
|
||||||
List<Long> lesIds = lesList.stream().map(Les::getId).collect(Collectors.toList());
|
List<Long> lesIds = lesList.stream().map(Les::getId).collect(Collectors.toList());
|
||||||
lesRepository.batchUpdateStatus(BizStatus.ARRIVED,lesIds);
|
lesRepository.batchUpdateStatus(BizStatus.ARRIVED, lesIds);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
|
|
@ -18,19 +18,24 @@ package com.youchain.modules.system.repository;
|
||||||
import com.youchain.modules.system.domain.DictDetail;
|
import com.youchain.modules.system.domain.DictDetail;
|
||||||
import org.springframework.data.jpa.repository.JpaRepository;
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
|
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
|
||||||
|
import org.springframework.data.jpa.repository.Query;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Liu Xue
|
* @author Liu Xue
|
||||||
* @date 2019-04-10
|
* @date 2019-04-10
|
||||||
*/
|
*/
|
||||||
public interface DictDetailRepository extends JpaRepository<DictDetail, Long>, JpaSpecificationExecutor<DictDetail> {
|
public interface DictDetailRepository extends JpaRepository<DictDetail, Long>, JpaSpecificationExecutor<DictDetail> {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 根据字典名称查询
|
* 根据字典名称查询
|
||||||
|
*
|
||||||
* @param name /
|
* @param name /
|
||||||
* @return /
|
* @return /
|
||||||
*/
|
*/
|
||||||
List<DictDetail> findByDictName(String name);
|
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;
|
package com.youchain.utils;
|
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
import java.io.UnsupportedEncodingException;
|
import java.io.UnsupportedEncodingException;
|
||||||
import java.net.URLEncoder;
|
import java.net.URLEncoder;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
|
|
@ -7,38 +9,24 @@ import java.security.MessageDigest;
|
||||||
import java.security.NoSuchAlgorithmException;
|
import java.security.NoSuchAlgorithmException;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
public class PostRequestSigner {
|
public class PostRequestSigner {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 生成POST请求的签名
|
* 生成POST请求的签名
|
||||||
*
|
*
|
||||||
* @param host 请求域名(不包含端口号)
|
* @param host 请求域名(不包含端口号)
|
||||||
* @param path 请求路径
|
* @param path 请求路径
|
||||||
* @param urlParams URL中的参数(不包含signe)
|
* @param jsonBody 请求体的JSON字符串
|
||||||
* @param jsonBody 请求体的JSON字符串
|
* @param appSecret 应用密钥
|
||||||
* @param appSecret 应用密钥
|
|
||||||
* @param accessToken 访问令牌(可为null)
|
|
||||||
* @return 生成的签名字符串(小写)
|
* @return 生成的签名字符串(小写)
|
||||||
*/
|
*/
|
||||||
public static String generateSign(String host, String path, Map<String, String> urlParams,
|
public static String generateSign(String host, String path,
|
||||||
String jsonBody, String appSecret, String accessToken) {
|
String jsonBody, String appSecret) {
|
||||||
try {
|
try {
|
||||||
// 1. 准备所有需要参与签名的参数
|
// 1. 准备所有需要参与签名的参数
|
||||||
Map<String, String> allParams = new TreeMap<>(); // TreeMap会自动按key升序排序
|
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参数
|
// 添加JSON Body参数
|
||||||
if (jsonBody != null && !jsonBody.isEmpty()) {
|
if (jsonBody != null && !jsonBody.isEmpty()) {
|
||||||
allParams.put("jsonBody", jsonBody);
|
allParams.put("jsonBody", jsonBody);
|
||||||
|
|
@ -61,11 +49,8 @@ public class PostRequestSigner {
|
||||||
.append("?").append(paramsStr.toString()) // 参数部分
|
.append("?").append(paramsStr.toString()) // 参数部分
|
||||||
.append(appSecret); // appSecret
|
.append(appSecret); // appSecret
|
||||||
|
|
||||||
// 如果有access_token,添加到签名字符串
|
|
||||||
if (accessToken != null && !accessToken.isEmpty()) {
|
|
||||||
signSource.append(accessToken);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
log.info("签名生成: {}", signSource);
|
||||||
// 4. 计算SHA256哈希并转为小写字符串
|
// 4. 计算SHA256哈希并转为小写字符串
|
||||||
return sha256(signSource.toString());
|
return sha256(signSource.toString());
|
||||||
|
|
||||||
|
|
@ -96,49 +81,4 @@ public class PostRequestSigner {
|
||||||
}
|
}
|
||||||
return hexString.toString();
|
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