兼容老的验签
This commit is contained in:
@@ -52,6 +52,40 @@ public class OpenSignatureUtil {
|
||||
return hmacSha256(sb.toString(), appSecret);
|
||||
}
|
||||
|
||||
public static String generateSignOld(Map<String, String> params, String appSecret) {
|
||||
// 1. 分离固定参数和业务参数
|
||||
String appKey = params.get("appKey");
|
||||
String timestamp = params.get("timestamp");
|
||||
|
||||
// 2. 创建不包含固定参数的临时Map用于排序
|
||||
Map<String, String> sortedParams = new TreeMap<>(
|
||||
params.entrySet().stream()
|
||||
.filter(e -> !"appKey".equals(e.getKey()))
|
||||
.filter(e -> !"timestamp".equals(e.getKey()))
|
||||
.filter(e -> !"sign".equals(e.getKey()))
|
||||
.filter(e -> e.getValue() != null && !e.getValue().isEmpty())
|
||||
.collect(Collectors.toMap(
|
||||
Map.Entry::getKey,
|
||||
Map.Entry::getValue
|
||||
))
|
||||
);
|
||||
|
||||
// 3. 构建参数字符串:业务参数(排序后) + 固定参数
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
// 3.1 添加排序后的业务参数
|
||||
sortedParams.forEach((key, value) -> {
|
||||
sb.append(key).append("=").append(value).append("&");
|
||||
});
|
||||
|
||||
// 3.2 添加固定参数(不参与排序)
|
||||
sb.append("appkey=").append(appKey)
|
||||
.append("×tamp=").append(timestamp);
|
||||
log.info("待签名字符串:{}", sb);
|
||||
// 4. 生成签名
|
||||
return hmacSha256(sb.toString(), appSecret);
|
||||
}
|
||||
|
||||
|
||||
private static String hmacSha256(String data, String key) {
|
||||
try {
|
||||
|
||||
Reference in New Issue
Block a user