|
|
@@ -81,14 +81,132 @@ public class HttpClientUtil {
|
|
|
connection.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
|
|
|
// connection.setRequestProperty("appKey", "xxxxxxxxx");
|
|
|
// connection.setRequestProperty("appSecret", "xxxxxxxxxxxxxxxx");
|
|
|
- connection.setRequestProperty("__tenant", "39fdfe6a-b1a9-ea14-7c44-e42efad2aca3");
|
|
|
+// connection.setRequestProperty("__tenant", "39fdfe6a-b1a9-ea14-7c44-e42efad2aca3");
|
|
|
+ connection.setConnectTimeout(10000); // 10秒连接超时
|
|
|
+ connection.setReadTimeout(30000); // 30秒读取超时
|
|
|
+ connection.setDoOutput(true);
|
|
|
+
|
|
|
+ // 3. 发送请求
|
|
|
+ try (OutputStream os = connection.getOutputStream()) {
|
|
|
+ byte[] input = encryptedRequest.getBytes(StandardCharsets.UTF_8);
|
|
|
+ os.write(input, 0, input.length);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 4. 获取响应
|
|
|
+ int responseCode = connection.getResponseCode();
|
|
|
+ if (responseCode == HttpURLConnection.HTTP_OK) {
|
|
|
+ StringBuilder response = new StringBuilder();
|
|
|
+ try (BufferedReader br = new BufferedReader(
|
|
|
+ new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8))) {
|
|
|
+ String line;
|
|
|
+ while ((line = br.readLine()) != null) {
|
|
|
+ response.append(line);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ responseStr = response.toString();
|
|
|
+ logger.info("第三方原始响应: {}", responseStr);
|
|
|
+
|
|
|
+ // 解析外层响应(包含code, message, status, data)
|
|
|
+ Map<String, Object> outerResponse = JSON.parseObject(responseStr, Map.class);
|
|
|
+
|
|
|
+ // 检查业务状态码
|
|
|
+ String code = (String) outerResponse.get("code");
|
|
|
+ if (!"200".equals(code)) {
|
|
|
+ String message = (String) outerResponse.get("message");
|
|
|
+ throw new RuntimeException("第三方接口业务异常,code: " + code + ", message: " + message);
|
|
|
+ }
|
|
|
+ success = true;
|
|
|
+
|
|
|
+ // 获取data字段中的加密数据
|
|
|
+ Map<String, String> data = (Map<String, String>) outerResponse.get("data");
|
|
|
+ if (data == null) {
|
|
|
+ throw new RuntimeException("响应中缺少data字段");
|
|
|
+ }
|
|
|
+
|
|
|
+ String encryptedResponseText = data.get("text");
|
|
|
+ String responseSign = data.get("sign");
|
|
|
+
|
|
|
+ if (encryptedResponseText == null || responseSign == null) {
|
|
|
+ throw new RuntimeException("响应数据格式不正确,缺少text或sign字段");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 5. 验签和解密
|
|
|
+ boolean verifyResult = apiSecurityUtil.rsaVerify(
|
|
|
+ encryptedResponseText,
|
|
|
+ responseSign,
|
|
|
+ thirdPartyPublicKeyBase64
|
|
|
+ );
|
|
|
+
|
|
|
+ if (!verifyResult) {
|
|
|
+ throw new RuntimeException("响应验签失败,数据可能被篡改");
|
|
|
+ }
|
|
|
+
|
|
|
+ decryptedText = apiSecurityUtil.aesDecrypt(encryptedResponseText, aesKey);
|
|
|
+ logger.debug("解密后的响应: {}", decryptedText);
|
|
|
+
|
|
|
+ return JSON.parseObject(decryptedText, responseType);
|
|
|
+
|
|
|
+ } else {
|
|
|
+ throw new RuntimeException("HTTP请求失败,状态码: " + responseCode);
|
|
|
+ }
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+ exception = e;
|
|
|
+ logger.error("HTTP请求失败: {}", e.getMessage(), e);
|
|
|
+ throw new RuntimeException("HTTP请求失败: " + e.getMessage());
|
|
|
+ } finally {
|
|
|
+ if (connection != null) {
|
|
|
+ connection.disconnect();
|
|
|
+ }
|
|
|
+ // 异步记录日志
|
|
|
+ recordApiLog(url, requestJson, encryptedRequest, responseStr, decryptedText, success, exception);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public <T> T doPostNotCaseSensitive(String url, Object request, Class<T> responseType) {
|
|
|
+ HttpURLConnection connection = null;
|
|
|
+ String encryptedRequest = null;
|
|
|
+ String requestJson = null;
|
|
|
+ String responseStr = null;
|
|
|
+ String decryptedText = null;
|
|
|
+ boolean success = false;
|
|
|
+ Exception exception = null;
|
|
|
+ try {
|
|
|
+
|
|
|
+ String requestBody = JSON.toJSONString(request);
|
|
|
+ requestJson = requestBody;
|
|
|
+
|
|
|
+ // 1. 加密和签名(保持您原有的逻辑)
|
|
|
+ String originalRequest = JSON.toJSONString(request);
|
|
|
+ logger.info("调用第三方接口 URL: {}, 原始请求参数: {}", url, requestJson);
|
|
|
+
|
|
|
+ String encryptedData = apiSecurityUtil.aesEncrypt(requestJson, aesKey);
|
|
|
+ String sign = apiSecurityUtil.rsaSign(encryptedData, rsaPrivateKeyBase64);
|
|
|
+
|
|
|
+ Map<String, String> finalRequest = new HashMap<>();
|
|
|
+ finalRequest.put("text", encryptedData);
|
|
|
+ finalRequest.put("sign", sign);
|
|
|
+
|
|
|
+ // 保存加密后的请求参数用于日志
|
|
|
+ encryptedRequest = JSON.toJSONString(finalRequest);
|
|
|
+
|
|
|
+ // 2. 创建HTTP连接
|
|
|
+ URL urlObj = new URL(url);
|
|
|
+ connection = (HttpURLConnection) urlObj.openConnection();
|
|
|
+ connection.setRequestMethod("POST");
|
|
|
+ connection.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
|
|
|
+// connection.setRequestProperty("appKey", "xxxxxxxxx");
|
|
|
+// connection.setRequestProperty("appSecret", "xxxxxxxxxxxxxxxx");
|
|
|
+// connection.setRequestProperty("__tenant", "39fdfe6a-b1a9-ea14-7c44-e42efad2aca3");
|
|
|
connection.setConnectTimeout(10000); // 10秒连接超时
|
|
|
connection.setReadTimeout(30000); // 30秒读取超时
|
|
|
connection.setDoOutput(true);
|
|
|
|
|
|
// 3. 发送请求
|
|
|
try (OutputStream os = connection.getOutputStream()) {
|
|
|
- byte[] input = requestBody.getBytes(StandardCharsets.UTF_8);
|
|
|
+ byte[] input = encryptedRequest.getBytes(StandardCharsets.UTF_8);
|
|
|
os.write(input, 0, input.length);
|
|
|
}
|
|
|
|
|
|
@@ -327,7 +445,7 @@ public class HttpClientUtil {
|
|
|
|
|
|
// 3. 发送请求
|
|
|
try (OutputStream os = connection.getOutputStream()) {
|
|
|
- byte[] input = requestBody.getBytes(StandardCharsets.UTF_8);
|
|
|
+ byte[] input = encryptedRequest.getBytes(StandardCharsets.UTF_8);
|
|
|
os.write(input, 0, input.length);
|
|
|
}
|
|
|
|