加签代码示例
更新时间:2026-07-06 15:44:21
1、加签字段signature值生成
1.1 加签规则描述
此接口默认开通,回送地址服务器需要启动一个 http 服务用于接收状态报告。用户在收到状态报告后需要自己写个方法去做处理收到的参数。
1.2 加签字段signature值生成java代码示例
package com.chuanglan.xxx.util;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import java.security.MessageDigest;
import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;
public class SignatureUtil {
public static void main(String[] args) throws Exception {
String req = "{\n" +
" \"nonce\": \"123456789\",\n" +
" \"timestamp\": \"1658974350\",\n" +
" \"signature\": \"f9233da1efacf7934b2d68d3f2411d55d0c9c2eb92be05c36cabe7c7b8c1d169\",\n" +
" \"app_id\": \"testAppId001\",\n" +
" \"template_id\": \"CLAIM202207260953094148600813568\",\n" +
" \"link_url\": \"https://www.chuanglan.com/\",\n" +
" \"link_type\": \"2\",\n" +
" \"mobiles\": \"[\\\"13333333333\\\",\\\"15555555555\\\"]\"\n" +
"}";
// 示例:app_id为testAppId001的app_secret的值:31ec4d8d507e510b52a5bd0793atg52a
// 注意:上面signature的值是不参与加签的
System.out.println("签名:"+ getSignature(req,"31ec4d8d507e510b52a5bd0793atg52a"));
// 加签后signature的值:38f9285dee3f0fe90bc4c3a5989bad6e19f7f7b7cd225a0542527466cee5f8c9
}
/**
* 计算签名
*/
public static String getSignature(String jsonString, String appSecret) throws Exception {
TreeMap<String, String> params = JSONObject.parseObject(jsonString, TreeMap.class);
StringBuilder sb = new StringBuilder();
Iterator<Map.Entry<String, String>> it = params.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<String, String> entry = it.next();
String value = entry.getValue();
if (StringUtils.isNotBlank(value) && !entry.getKey().equals("signature")) {
sb.append(value);
}
}
return encrypt(appSecret + sb.toString());
}
public static String encrypt(String str) throws Exception {
MessageDigest messageDigest = MessageDigest.getInstance("SHA-256");
messageDigest.update(str.getBytes("UTF-8"));
return byte2Hex(messageDigest.digest());
}
private static String byte2Hex(byte[] bytes) {
StringBuilder stringBuilder = new StringBuilder();
String temp;
for (byte aByte : bytes) {
temp = Integer.toHexString(aByte & 0xFF);
if (temp.length() == 1) {
stringBuilder.append("0");
}
stringBuilder.append(temp);
}
return stringBuilder.toString();
}
}
这篇文档对您有帮助吗?




