openAPi验签
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
package com.cool.store.utils;
|
package com.cool.store.utils;
|
||||||
|
|
||||||
|
import com.alibaba.fastjson.JSONObject;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
import javax.crypto.Mac;
|
import javax.crypto.Mac;
|
||||||
@@ -17,18 +18,18 @@ import java.util.stream.Collectors;
|
|||||||
public class OpenSignatureUtil {
|
public class OpenSignatureUtil {
|
||||||
private static final String HMAC_SHA256 = "HmacSHA256";
|
private static final String HMAC_SHA256 = "HmacSHA256";
|
||||||
|
|
||||||
public static String generateSign(Map<String, String> params, String appSecret) {
|
public static String generateSign(Map<String, Object> params, String appSecret) {
|
||||||
// 1. 分离固定参数和业务参数
|
// 1. 分离固定参数和业务参数
|
||||||
String appKey = params.get("appKey");
|
String appKey = params.get("appKey").toString();
|
||||||
String timestamp = params.get("timestamp");
|
String timestamp = params.get("timestamp").toString();
|
||||||
|
|
||||||
// 2. 创建不包含固定参数的临时Map用于排序
|
// 2. 创建不包含固定参数的临时Map用于排序
|
||||||
Map<String, String> sortedParams = new TreeMap<>(
|
Map<String, Object> sortedParams = new TreeMap<>(
|
||||||
params.entrySet().stream()
|
params.entrySet().stream()
|
||||||
.filter(e -> !"appKey".equals(e.getKey()))
|
.filter(e -> !"appKey".equals(e.getKey()))
|
||||||
.filter(e -> !"timestamp".equals(e.getKey()))
|
.filter(e -> !"timestamp".equals(e.getKey()))
|
||||||
.filter(e -> !"sign".equals(e.getKey()))
|
.filter(e -> !"sign".equals(e.getKey()))
|
||||||
.filter(e -> e.getValue() != null && !e.getValue().isEmpty())
|
.filter(e -> Objects.nonNull(e.getValue()))
|
||||||
.collect(Collectors.toMap(
|
.collect(Collectors.toMap(
|
||||||
Map.Entry::getKey,
|
Map.Entry::getKey,
|
||||||
Map.Entry::getValue
|
Map.Entry::getValue
|
||||||
@@ -40,7 +41,7 @@ public class OpenSignatureUtil {
|
|||||||
|
|
||||||
// 3.1 添加排序后的业务参数
|
// 3.1 添加排序后的业务参数
|
||||||
sortedParams.forEach((key, value) -> {
|
sortedParams.forEach((key, value) -> {
|
||||||
sb.append(key).append("=").append(value).append("&");
|
sb.append(key).append("=").append(JSONObject.toJSONString( value)).append("&");
|
||||||
});
|
});
|
||||||
|
|
||||||
// 3.2 添加固定参数(不参与排序)
|
// 3.2 添加固定参数(不参与排序)
|
||||||
|
|||||||
@@ -123,9 +123,9 @@ public class OpenApiValidateFilter implements Filter {
|
|||||||
|
|
||||||
// 2. 使用 Jackson 解析 JSON 并转为 TreeMap(自动按键排序)
|
// 2. 使用 Jackson 解析 JSON 并转为 TreeMap(自动按键排序)
|
||||||
ObjectMapper objectMapper = new ObjectMapper();
|
ObjectMapper objectMapper = new ObjectMapper();
|
||||||
SortedMap<String, String> params = objectMapper.readValue(
|
SortedMap<String, Object> params = objectMapper.readValue(
|
||||||
jsonBody,
|
jsonBody,
|
||||||
new TypeReference<TreeMap<String, String>>() {}
|
new TypeReference<TreeMap<String, Object>>() {}
|
||||||
);
|
);
|
||||||
params.put("appKey",appKey);
|
params.put("appKey",appKey);
|
||||||
params.put("timestamp", timestampStr);
|
params.put("timestamp", timestampStr);
|
||||||
|
|||||||
Reference in New Issue
Block a user