1.appId和secretKey定义
appId=APPID
secretKey =cfq4189auoo13y17ur9n2rl7v2tkz3iq
2.sign获取算法
md5(secretKey+参数json字符串+timestamp+secretKey)后的大写字母。
/**
* 参数按key进行排序
* @param obj
* @return
*/
public static String getParamStr(Object obj, List<String> ignoreKey) {
if (obj == null) {
return null;
}
Map<String, Object> map = object2Map(obj);
if (map == null || map.isEmpty()) {
return null;
}
//升序排序
Map<String, Object> sortMap = new TreeMap<>(String::compareTo);
sortMap.putAll(map);
StringBuilder signStr = new StringBuilder();
for (Map.Entry<String, Object> entry : sortMap.entrySet()) {
//跳过参数
if (CollectionUtils.isNotEmpty(ignoreKey) && ignoreKey.contains(entry.getKey())) {
continue;
}
signStr.append(entry.getKey()).append("=").append(String.valueOf(entry.getValue())).append("&");
}
//去掉最后一个&
signStr.deleteCharAt(signStr.length() - 1);
return signStr.toString();
}
public static Map<String, Object> object2Map(Object obj) {
if (obj == null) {
return null;
}
BeanMap beanMap = new org.apache.commons.beanutils.BeanMap(obj);
HashMap<String, Object> map = new HashMap<>();
for (Object key : beanMap.keySet()) {
// 去掉值为null的字段
if ("class".equals(key) || Objects.isNull(beanMap.get(key))) {
continue;
}
map.put((String) key, beanMap.get(key));
}
return map;
}
public static String sign(String paramJsonStr, String secretKey, String timestamp) throws NoSuchAlgorithmException {
StringBuilder sb = new StringBuilder();
sb.append(secretKey);//秘钥
sb.append(paramJsonStr);//参数字符串
sb.append(timestamp);//请求时间
sb.append(secretKey);//秘钥
return getMd5(sb.toString().getBytes(StandardCharsets.UTF_8)).toUpperCase();
}
public static String getMd5(byte[] buffer) throws NoSuchAlgorithmException {
String s;
char[] hexDigist = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(buffer);
// 16个字节的长整数
byte[] datas = md.digest();
char[] str = new char[2 * 16];
int k = 0;
for (int i = 0; i < 16; i++) {
byte b = datas[i];
str[k++] = hexDigist[b >>> 4 & 0xf];// 高4位
str[k++] = hexDigist[b & 0xf];// 低4位
}
s = new String(str);
return s;
}
3.接口Header请求头参数解释