From 59ef2a4560ccb3bae2c2f627c0663c34fbc45e2e Mon Sep 17 00:00:00 2001 From: WendyYang Date: Sat, 11 Feb 2023 10:01:26 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E9=A1=B9=E7=9B=AE=E5=BA=94?= =?UTF-8?q?=E7=94=A8=E8=A1=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ningdatech/pmapi/irs/sign/IRSAPIRequest.java | 38 ++++++---------------- 1 file changed, 10 insertions(+), 28 deletions(-) diff --git a/pmapi/src/main/java/com/ningdatech/pmapi/irs/sign/IRSAPIRequest.java b/pmapi/src/main/java/com/ningdatech/pmapi/irs/sign/IRSAPIRequest.java index 85147d5..28e92eb 100644 --- a/pmapi/src/main/java/com/ningdatech/pmapi/irs/sign/IRSAPIRequest.java +++ b/pmapi/src/main/java/com/ningdatech/pmapi/irs/sign/IRSAPIRequest.java @@ -3,7 +3,7 @@ package com.ningdatech.pmapi.irs.sign; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.sun.org.apache.xerces.internal.impl.dv.util.Base64; -import javafx.util.Pair; +import org.apache.commons.lang3.tuple.Pair; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.methods.HttpEntityEnclosingRequestBase; @@ -124,8 +124,7 @@ public class IRSAPIRequest { req.addHeader("Content-Type", "application/json"); // 设置HTTP请求体 - HttpEntity entity = new ByteArrayEntity(stream, ContentType - .create(ContentType.APPLICATION_JSON.getMimeType(), "UTF-8")); + HttpEntity entity = new ByteArrayEntity(stream, ContentType.create(ContentType.APPLICATION_JSON.getMimeType(), "UTF-8")); req.setEntity(entity); // 执行请求 @@ -147,15 +146,12 @@ public class IRSAPIRequest { return strRes; } - private static String sign(byte[] stream) - throws Exception { + private static String sign(byte[] stream) throws Exception { // 获取消息验证码类的实例,算法选择"HmacSHA256" Mac mac = Mac.getInstance("HmacSHA256"); // 获取安全密钥 - Key secKey = new SecretKeySpec( - ProjectSecret.getBytes("UTF-8"), - mac.getAlgorithm()); + Key secKey = new SecretKeySpec(ProjectSecret.getBytes("UTF-8"), mac.getAlgorithm()); // 初始化 mac.init(secKey); @@ -168,15 +164,12 @@ public class IRSAPIRequest { } - private static String sign1(byte[] stream) - throws Exception { + private static String sign1(byte[] stream) throws Exception { // 获取消息验证码类的实例,算法选择"HmacSHA256" Mac mac = Mac.getInstance("HmacSHA256"); // 获取安全密钥 - Key secKey = new SecretKeySpec( - secretKey.getBytes("UTF-8"), - mac.getAlgorithm()); + Key secKey = new SecretKeySpec(secretKey.getBytes("UTF-8"), mac.getAlgorithm()); // 初始化 mac.init(secKey); @@ -190,8 +183,7 @@ public class IRSAPIRequest { } public static String binaryEncode(byte[] data) { - final char hexDigits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', - '9', 'a', 'b', 'c', 'd', 'e', 'f'}; + final char hexDigits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}; StringBuilder builder = new StringBuilder(); @@ -254,8 +246,6 @@ public class IRSAPIRequest { return Base64.encode(bytes); //return encoder.encodeBuffer(bytes); - } catch (FileNotFoundException e) { - e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { @@ -321,7 +311,7 @@ public class IRSAPIRequest { int eqIndex = param.indexOf("="); String key = param.substring(0, eqIndex); String value = param.substring(eqIndex + 1); - Pair pair = new Pair(key, value); + Pair pair = Pair.of(key, value); queryParamList.add(pair); } @@ -330,16 +320,8 @@ public class IRSAPIRequest { sortedParamList.stream().forEach(param -> { try { String key = URLEncoder.encode(param.getKey(), "utf-8"); - String value = URLEncoder.encode(Optional.ofNullable(param.getValue()).orElse(""), "utf-8") - .replaceAll("\\%2B", "%20") - .replaceAll("\\+", "%20") - .replaceAll("\\%21", "!") - .replaceAll("\\%27", "'") - .replaceAll("\\%28", "(") - .replaceAll("\\%29", ")") - .replaceAll("\\%7E", "~") - .replaceAll("\\%25", "%"); - encodeParamList.add(new Pair<>(key, value)); + String value = URLEncoder.encode(Optional.ofNullable(param.getValue()).orElse(""), "utf-8").replaceAll("\\%2B", "%20").replaceAll("\\+", "%20").replaceAll("\\%21", "!").replaceAll("\\%27", "'").replaceAll("\\%28", "(").replaceAll("\\%29", ")").replaceAll("\\%7E", "~").replaceAll("\\%25", "%"); + encodeParamList.add(Pair.of(key, value)); } catch (UnsupportedEncodingException e) { throw new RuntimeException("encoding error"); }