|
|
@ -1,14 +1,21 @@
|
|
|
|
package com.charge.util;
|
|
|
|
package com.charge.util;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import com.alibaba.fastjson.JSONArray;
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
|
import com.google.common.base.Joiner;
|
|
|
|
import com.google.common.base.Joiner;
|
|
|
|
|
|
|
|
|
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
|
import java.security.MessageDigest;
|
|
|
|
import java.security.MessageDigest;
|
|
|
|
import java.security.NoSuchAlgorithmException;
|
|
|
|
import java.security.NoSuchAlgorithmException;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.TreeMap;
|
|
|
|
import java.util.TreeMap;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
|
|
|
|
|
import com.alibaba.fastjson.JSONException;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import javax.servlet.ServletOutputStream;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* 签名工具
|
|
|
|
* 签名工具
|
|
|
|
*
|
|
|
|
*
|
|
|
@ -16,6 +23,19 @@ import java.util.TreeMap;
|
|
|
|
* @date 2020/3/6
|
|
|
|
* @date 2020/3/6
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public class SignUtils {
|
|
|
|
public class SignUtils {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 功能:判断一个字符串是不是JSON格式
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* @param str
|
|
|
|
|
|
|
|
* @return
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
public static boolean isJsonString(String str) {
|
|
|
|
|
|
|
|
if (str.indexOf("{") >= 0) return true;
|
|
|
|
|
|
|
|
if (str.indexOf("[") >= 0) return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* 参数签名
|
|
|
|
* 参数签名
|
|
|
|
* 示例:
|
|
|
|
* 示例:
|
|
|
@ -29,6 +49,13 @@ public class SignUtils {
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public static String paramsSign(JSONObject requestBody, String appSecret) {
|
|
|
|
public static String paramsSign(JSONObject requestBody, String appSecret) {
|
|
|
|
TreeMap<String, String> params = new TreeMap<>();
|
|
|
|
TreeMap<String, String> params = new TreeMap<>();
|
|
|
|
|
|
|
|
for (String key : new ArrayList<>(requestBody.keySet())) {
|
|
|
|
|
|
|
|
// 假设我们要移除以 "age" 开头的属性
|
|
|
|
|
|
|
|
Object value = requestBody.get(key);
|
|
|
|
|
|
|
|
if (isJsonString(value.toString())) {
|
|
|
|
|
|
|
|
requestBody.remove(key);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
//过滤掉key,appId字段,空属性及Map或List等复杂对象
|
|
|
|
//过滤掉key,appId字段,空属性及Map或List等复杂对象
|
|
|
|
requestBody.entrySet().stream().filter(
|
|
|
|
requestBody.entrySet().stream().filter(
|
|
|
|
p -> !"key".equals(p.getKey())
|
|
|
|
p -> !"key".equals(p.getKey())
|
|
|
@ -43,7 +70,10 @@ public class SignUtils {
|
|
|
|
});
|
|
|
|
});
|
|
|
|
//拼接appSecret
|
|
|
|
//拼接appSecret
|
|
|
|
String temp = Joiner.on("&").withKeyValueSeparator("=").join(params).concat("&").concat(appSecret);
|
|
|
|
String temp = Joiner.on("&").withKeyValueSeparator("=").join(params).concat("&").concat(appSecret);
|
|
|
|
return md5(temp).toUpperCase();
|
|
|
|
System.out.println(temp);
|
|
|
|
|
|
|
|
String MD5 = md5(temp).toUpperCase();
|
|
|
|
|
|
|
|
System.out.println(MD5);
|
|
|
|
|
|
|
|
return MD5;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
@ -55,10 +85,15 @@ public class SignUtils {
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public static String paramsAnKuaiSign(JSONObject requestBody, String appSecret) {
|
|
|
|
public static String paramsAnKuaiSign(JSONObject requestBody, String appSecret) {
|
|
|
|
TreeMap<String, String> params = new TreeMap<>();
|
|
|
|
TreeMap<String, String> params = new TreeMap<>();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
requestBody.entrySet().stream().filter(
|
|
|
|
requestBody.entrySet().stream().filter(
|
|
|
|
p -> p.getValue() != null
|
|
|
|
p -> p.getValue() != null
|
|
|
|
&& !(p.getValue() instanceof Map)
|
|
|
|
&& !(p.getValue() instanceof Map)
|
|
|
|
&& !(p.getValue() instanceof Iterable))
|
|
|
|
&& !(p.getValue() instanceof Iterable)
|
|
|
|
|
|
|
|
&& !(p.getValue() instanceof JSONObject)
|
|
|
|
|
|
|
|
&& !(p.getValue() instanceof JSONArray)
|
|
|
|
|
|
|
|
)
|
|
|
|
.forEach(p -> {
|
|
|
|
.forEach(p -> {
|
|
|
|
if (!p.getValue().equals("")) {
|
|
|
|
if (!p.getValue().equals("")) {
|
|
|
|
params.put(p.getKey(), p.getValue().toString());
|
|
|
|
params.put(p.getKey(), p.getValue().toString());
|
|
|
@ -105,4 +140,14 @@ public class SignUtils {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return mD5Str;
|
|
|
|
return mD5Str;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static void main(String[] args) {
|
|
|
|
|
|
|
|
String source = "{\"amount\":100,\"orderNo\":\"闽C12345\",\"payTime\":\"2020-03-06 10:57:22\",\"freeDetail\":\"[{\\\"code\\\":\\\"\\\",\\\"money\\\":100,\\\"time\\\":0,\\\"type\\\":0}]\",\"paySource\":\"EED96C219E83450A\",\"outOrderNo\":\"T20200306124536001\",\"parkId\":\"1000001\",\"payableAmount\":200,\"reqId\":\"748584ae47104b0ab239732767ddc679\",\"payType\":1006,\"payMethod\":6,\"appId\":\"EED96C219E83450A\",\"freeTime\":0,\"paymentExt\":\"{\\\"deviceNo\\\":\\\"123456\\\"}\",\"freeMoney\":100,\"ts\":1583464576264}";
|
|
|
|
|
|
|
|
JSONObject jo = JSONObject.parseObject(source);
|
|
|
|
|
|
|
|
String appSecret = "85d15350778b11e9bbaa506b4b2f6421";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
System.out.println(paramsSign(jo, appSecret));
|
|
|
|
|
|
|
|
//4402E3F7DB94CCCF034F9AFA86F5F9CD
|
|
|
|
|
|
|
|
//4402E3F7DB94CCCF034F9AFA86F5F9CD
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|