新增了同步app视频,
This commit is contained in:
@@ -12,9 +12,12 @@ import org.apache.commons.io.IOUtils;
|
||||
|
||||
import javax.crypto.Mac;
|
||||
import javax.crypto.spec.SecretKeySpec;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.InputStreamReader;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Map;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Slf4j
|
||||
public class Base {
|
||||
@@ -125,39 +128,36 @@ public class Base {
|
||||
}
|
||||
|
||||
/**
|
||||
* 校验签名
|
||||
* @param request 请求对象
|
||||
* @param secretKey 密钥
|
||||
* @param mapper ObjectMapper 实例(用于 JSON 反序列化)
|
||||
* 校验签名(使用已反序列化的 dto 对象)
|
||||
* @param request HttpServletRequest(用于获取 appId 和 sign)
|
||||
* @param secretKey 签名用的密钥
|
||||
* @param dto 请求对象(如 addClinicalVideoApp)
|
||||
* @param mapper ObjectMapper 实例(用于序列化 dto)
|
||||
*/
|
||||
public static void checkSign(HttpServletRequest request, String secretKey, ObjectMapper mapper) {
|
||||
public void checkSign(HttpServletRequest request, String secretKey, Object dto, ObjectMapper mapper) {
|
||||
try {
|
||||
String appId = request.getHeader("appId");
|
||||
String sign = request.getHeader("sign");
|
||||
|
||||
if (appId == null || appId.isEmpty()) {
|
||||
throw new BusinessException("-1", "请求未授权");
|
||||
throw new BusinessException("-1", "请求未授权(缺少 appId)");
|
||||
}
|
||||
if (sign == null || sign.isEmpty()) {
|
||||
throw new BusinessException("-1", "缺少签名");
|
||||
}
|
||||
|
||||
// 读取请求体
|
||||
String body = IOUtils.toString(request.getInputStream(), StandardCharsets.UTF_8);
|
||||
if (body == null || body.isEmpty()) {
|
||||
throw new BusinessException("-1", "请求体为空");
|
||||
}
|
||||
|
||||
// 使用 TypeReference 明确泛型类型,避免警告
|
||||
Map<String, Object> params = mapper.readValue(body, new TypeReference<Map<String, Object>>() {});
|
||||
// 转换 dto 为 Map<String, Object> 用于签名生成
|
||||
@SuppressWarnings("unchecked")
|
||||
Map<String, Object> params = mapper.convertValue(dto, Map.class);
|
||||
|
||||
// 生成签名
|
||||
String serverSign = Base.genSignature(params, secretKey);
|
||||
String serverSign = genSignature(params, secretKey);
|
||||
System.out.println("客户端签名: " + sign);
|
||||
System.out.println("服务端签名: " + serverSign);
|
||||
|
||||
if (!sign.equals(serverSign)) {
|
||||
throw new BusinessException("-1", "签名错误");
|
||||
}
|
||||
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
|
||||
Reference in New Issue
Block a user