SpringCloudHttpUtil.java
7.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package com.glsx.usedcar.gsc;
import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.HmacAlgorithms;
import org.apache.commons.codec.digest.HmacUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.web.client.RestTemplate;
import java.util.*;
/**
* @Description springcloud http工具类
* @Author yelq
* @Date 2025/9/18-14:46
* @Version 1.0
*/
@Slf4j
public class SpringCloudHttpUtil {
private final static RestTemplate restTemplate = new RestTemplate();
private static final ObjectMapper objectMapper = new ObjectMapper();
public static void main(String[] args) {
try {
// UserEntity userEntity = new UserEntity();
// userEntity.setUserName("test");
// Object post = post(userEntity, Object.class, "/auction/push");
Object post = post(buildRequest(), Object.class, "/auction/push");
System.out.println(post);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static CarAuctionRequest buildRequest() {
// 构造拍卖数据推送请求的模拟数据,便于本地联调与签名验证
CarAuctionRequest request = new CarAuctionRequest();
// 1. 核心标识
CarAuctionRequest.CoreIdentity coreIdentity = new CarAuctionRequest.CoreIdentity();
coreIdentity.setSourceNo("25104119");
coreIdentity.setAuctionId("AUC-20250118-01");
coreIdentity.setLotNumber(88);
request.setCoreIdentity(coreIdentity);
// 2. 核心状态信息(撮合信息及最终结果仅在 IN_NEGOTIATION 及之后的状态有效,这里使用 SOLD 以覆盖更多字段)
CarAuctionRequest.StatusInfo statusInfo = new CarAuctionRequest.StatusInfo();
statusInfo.setStatus("AUCTIONING");
statusInfo.setStatusDescription("车辆已成交");
statusInfo.setUpdatedDate("2025-10-01 10:00:00");
request.setStatusInfo(statusInfo);
//
// // 3. 拍卖参数
CarAuctionRequest.AuctionParams auctionParams = new CarAuctionRequest.AuctionParams();
auctionParams.setAuctionStartTime("2025-10-01 10:00:00");
auctionParams.setAuctionEndTime("2025-10-01 10:00:00");
auctionParams.setTimeRemainingSeconds(0);
request.setAuctionParams(auctionParams);
// // 4. 撮合信息(最高出价、当前报价、撮合人、撮合截止时间)
CarAuctionRequest.NegotiationInfo negotiationInfo = new CarAuctionRequest.NegotiationInfo();
negotiationInfo.setHighestAuctionBid("178000.00");
negotiationInfo.setCurrentOfferPrice("178000.00");
negotiationInfo.setNegotiatorId("EMP-007");
// 撮合截止时间设为结束时间后 1 小时
negotiationInfo.setNegotiationDeadline("2025-10-01 10:00:00");
request.setNegotiationInfo(negotiationInfo);
//
// // 5. 最终成交结果
CarAuctionRequest.FinalResult finalResult = new CarAuctionRequest.FinalResult();
finalResult.setWinnerId("USER-0000-9527");
finalResult.setTransactionTime("2025-10-01 10:00:00");
finalResult.setTransactionType("AUCTION");
finalResult.setSellingPrice("179000.00");
request.setFinalResult(finalResult);
return request;
}
/**
* @param obj 请求参数
* @param responseType 响应类型
* @param method 例如:/auction/push
* @return
*/
public static <T> T post(Object obj, Class<T> responseType, String method) throws Exception {
long l = System.currentTimeMillis();
String url = "http://gsc.dev.glsx.com.cn/gsc-vcp-thirdparty-facade" + method;
//创建请求头
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.add("Content-Type", "application/json");
httpHeaders.add("X-App-Id", "auction_platform");
httpHeaders.add("X-Timestamp", l + "");
httpHeaders.add("X-Nonce", "123456");
Map<String, Object> params = new HashMap<>();
params.put("X-App-Id", "auction_platform");
params.put("X-Timestamp", l);
params.put("X-Nonce", "123456");
// params.put("sessionId", "xxxxxxxxxxxxxx");
Map<String, Object> objectMap = getOuterKeyValues(obj);
params.putAll(objectMap);
String s = generateSign(params, "a8c0d4ef72a1b3f0c52e93a611cc9fb49f51a0dc46f2e7b4");
httpHeaders.add("X-Sign", s);
HttpEntity<Object> httpEntity = new HttpEntity<>(objectMap, httpHeaders);
//远程调用,
log.info("请求地址:{},请求参数:{},请求头:{}", url, JSON.toJSON(obj), httpHeaders);
T t = restTemplate.postForObject(url, httpEntity, responseType);
log.info("返回结果:{}", JSON.toJSON(t));
return t;
}
public static Map<String, Object> getOuterKeyValues(Object obj) {
try {
JsonNode jsonNode = objectMapper.valueToTree(obj);
Map<String, Object> result = new HashMap<>();
jsonNode.fields().forEachRemaining(entry -> {
// 非空才封装
if (entry.getValue().isNull() || entry.getValue().isMissingNode()) {
return;
}
result.put(entry.getKey(), entry.getValue());
});
return result;
} catch (Exception e) {
throw new RuntimeException("JSON转换失败", e);
}
}
/**
* 生成签名
*
* @param params 请求参数
* @param secret 应用密钥
* @return 签名值
*/
public static String generateSign(Map<String, Object> params, String secret) {
// 1. 参数排序
List<String> keys = new ArrayList<>(params.keySet());
Collections.sort(keys);
// 2. 拼接参数
StringBuilder sb = new StringBuilder();
for (String key : keys) {
if ("X-Sign".equals(key) || params.get(key) == null || "".equals(params.get(key))) {
continue;
}
sb.append(key).append("=").append(params.get(key)).append("&");
}
String stringToSign = sb.toString();
stringToSign = stringToSign + "secret=" + secret;
// 3. 计算HMAC-SHA256签名
return new HmacUtils(HmacAlgorithms.HMAC_SHA_256, secret).hmacHex(stringToSign);
}
/**
* 验证签名
*
* @param params 请求参数
* @param secret 应用密钥
* @return 是否验证通过
*/
public static boolean verifySign(Map<String, Object> params, String secret) {
if (secret == null || secret.isEmpty()) {
log.info("秘钥为空!");
return false;
}
String sign = (String) params.get("X-Sign");
if (StringUtils.isEmpty(sign)) {
return false;
}
return sign.equals(generateSign(params, secret));
}
}