|
@@ -3,7 +3,7 @@ package com.ningdatech.pmapi.irs.sign; |
|
|
import com.alibaba.fastjson.JSON; |
|
|
import com.alibaba.fastjson.JSON; |
|
|
import com.alibaba.fastjson.JSONObject; |
|
|
import com.alibaba.fastjson.JSONObject; |
|
|
import com.sun.org.apache.xerces.internal.impl.dv.util.Base64; |
|
|
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.HttpEntity; |
|
|
import org.apache.http.HttpResponse; |
|
|
import org.apache.http.HttpResponse; |
|
|
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase; |
|
|
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase; |
|
@@ -124,8 +124,7 @@ public class IRSAPIRequest { |
|
|
req.addHeader("Content-Type", "application/json"); |
|
|
req.addHeader("Content-Type", "application/json"); |
|
|
|
|
|
|
|
|
// 设置HTTP请求体 |
|
|
// 设置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); |
|
|
req.setEntity(entity); |
|
|
|
|
|
|
|
|
// 执行请求 |
|
|
// 执行请求 |
|
@@ -147,15 +146,12 @@ public class IRSAPIRequest { |
|
|
return strRes; |
|
|
return strRes; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
private static String sign(byte[] stream) |
|
|
|
|
|
throws Exception { |
|
|
|
|
|
|
|
|
private static String sign(byte[] stream) throws Exception { |
|
|
// 获取消息验证码类的实例,算法选择"HmacSHA256" |
|
|
// 获取消息验证码类的实例,算法选择"HmacSHA256" |
|
|
Mac mac = Mac.getInstance("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); |
|
|
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" |
|
|
// 获取消息验证码类的实例,算法选择"HmacSHA256" |
|
|
Mac mac = Mac.getInstance("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); |
|
|
mac.init(secKey); |
|
@@ -190,8 +183,7 @@ public class IRSAPIRequest { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public static String binaryEncode(byte[] data) { |
|
|
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(); |
|
|
StringBuilder builder = new StringBuilder(); |
|
|
|
|
|
|
|
@@ -254,8 +246,6 @@ public class IRSAPIRequest { |
|
|
return Base64.encode(bytes); |
|
|
return Base64.encode(bytes); |
|
|
//return encoder.encodeBuffer(bytes); |
|
|
//return encoder.encodeBuffer(bytes); |
|
|
|
|
|
|
|
|
} catch (FileNotFoundException e) { |
|
|
|
|
|
e.printStackTrace(); |
|
|
|
|
|
} catch (IOException e) { |
|
|
} catch (IOException e) { |
|
|
e.printStackTrace(); |
|
|
e.printStackTrace(); |
|
|
} finally { |
|
|
} finally { |
|
@@ -321,7 +311,7 @@ public class IRSAPIRequest { |
|
|
int eqIndex = param.indexOf("="); |
|
|
int eqIndex = param.indexOf("="); |
|
|
String key = param.substring(0, eqIndex); |
|
|
String key = param.substring(0, eqIndex); |
|
|
String value = param.substring(eqIndex + 1); |
|
|
String value = param.substring(eqIndex + 1); |
|
|
Pair<String, String> pair = new Pair<String, String>(key, value); |
|
|
|
|
|
|
|
|
Pair<String, String> pair = Pair.of(key, value); |
|
|
queryParamList.add(pair); |
|
|
queryParamList.add(pair); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@@ -330,16 +320,8 @@ public class IRSAPIRequest { |
|
|
sortedParamList.stream().forEach(param -> { |
|
|
sortedParamList.stream().forEach(param -> { |
|
|
try { |
|
|
try { |
|
|
String key = URLEncoder.encode(param.getKey(), "utf-8"); |
|
|
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) { |
|
|
} catch (UnsupportedEncodingException e) { |
|
|
throw new RuntimeException("encoding error"); |
|
|
throw new RuntimeException("encoding error"); |
|
|
} |
|
|
} |
|
|