新增apiKey和相关逻辑
This commit is contained in:
1 parent
bc772b8b15
commit
051f78b59c
9 files changed
+77
-15
No files matched your search
@@ -1,10 +1,13 @@
|
||||
package com.ruoyi.system.service;
|
||||
|
||||
import javax.crypto.Mac;
|
||||
import javax.crypto.spec.SecretKeySpec;
|
||||
import java.security.MessageDigest;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Map;
|
||||
import java.util.TreeMap;
|
||||
|
||||
/** 支付 API 签名:按键排序拼接非空参数后追加 secret,MD5 小写。 */
|
||||
/** 支付 API 签名:按键排序拼接非空参数后使用 secret 计算 HMAC-SHA256。 */
|
||||
public final class PaymentSignatureService {
|
||||
private PaymentSignatureService() { }
|
||||
public static String sign(Map<String, ?> values, String secret) {
|
||||
@@ -16,12 +19,21 @@ public final class PaymentSignatureService {
|
||||
text.append(entry.getKey()).append('=').append(entry.getValue());
|
||||
}
|
||||
}
|
||||
text.append(secret);
|
||||
try {
|
||||
byte[] digest = MessageDigest.getInstance("MD5").digest(text.toString().getBytes("UTF-8"));
|
||||
Mac mac = Mac.getInstance("HmacSHA256");
|
||||
mac.init(new SecretKeySpec(secret.getBytes("UTF-8"), "HmacSHA256"));
|
||||
byte[] digest = mac.doFinal(text.toString().getBytes("UTF-8"));
|
||||
StringBuilder result = new StringBuilder();
|
||||
for (byte b : digest) result.append(String.format("%02x", b & 0xff));
|
||||
return result.toString();
|
||||
} catch (Exception ex) { throw new IllegalStateException("MD5 unavailable", ex); }
|
||||
} catch (Exception ex) { throw new IllegalStateException("HMAC-SHA256 unavailable", ex); }
|
||||
}
|
||||
|
||||
/** 使用常量时间比较签名,避免普通字符串比较泄露匹配前缀信息。 */
|
||||
public static boolean verify(Map<String, ?> values, String secret, String signature) {
|
||||
if (secret == null || signature == null) return false;
|
||||
byte[] expected = sign(values, secret).getBytes(StandardCharsets.US_ASCII);
|
||||
byte[] actual = signature.toLowerCase().getBytes(StandardCharsets.US_ASCII);
|
||||
return MessageDigest.isEqual(expected, actual);
|
||||
}
|
||||
}
|
||||
@@ -233,6 +233,10 @@ public class AppApiKeysServiceImpl extends ServiceImpl<AppApiKeysMapper, AppApiK
|
||||
byte[] secretBytes = new byte[32];
|
||||
new SecureRandom().nextBytes(secretBytes);
|
||||
String secret = Base64.getUrlEncoder().withoutPadding().encodeToString(secretBytes);
|
||||
byte[] keyBytes = new byte[24];
|
||||
new SecureRandom().nextBytes(keyBytes);
|
||||
String apiKey = "ak_" + Base64.getUrlEncoder().withoutPadding().encodeToString(keyBytes);
|
||||
appApiKeys.setApiKey(apiKey);
|
||||
appApiKeys.setApiSecret(secret);
|
||||
appApiKeys.setUserId(app.getUserId());
|
||||
if (appApiKeys.getStatus() == null) appApiKeys.setStatus(1);
|
||||
@@ -240,7 +244,7 @@ public class AppApiKeysServiceImpl extends ServiceImpl<AppApiKeysMapper, AppApiK
|
||||
appApiKeys.setUpdatedAt(DateUtils.getNowDate());
|
||||
appApiKeys.setDeletedAt(null);
|
||||
if (appApiKeysMapper.insertAppApiKeys(appApiKeys) != 1) throw new CustomException("API Key 创建失败");
|
||||
return new AppApiKeyCreateResult(appApiKeys.getId(), appApiKeys.getAppId(), secret);
|
||||
return new AppApiKeyCreateResult(appApiKeys.getId(), appApiKeys.getAppId(), apiKey, secret);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -26,9 +26,13 @@ public class PaymentCallbackWorker {
|
||||
if (order.getNotifyUrl() == null || order.getNotifyUrl().trim().isEmpty()) continue;
|
||||
HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_JSON);
|
||||
String body = "{\"order_id\":\"" + order.getOrderId() + "\",\"trade_id\":\"" + order.getTradeId() + "\",\"status\":" + order.getStatus() + ",\"transaction_id\":\"" + (order.getBlockTransactionId() == null ? "" : order.getBlockTransactionId()) + "\"}";
|
||||
AppApiKeysPlus key = keys.getOne(new LambdaQueryWrapper<AppApiKeysPlus>().eq(AppApiKeysPlus::getAppId, order.getAppId()).eq(AppApiKeysPlus::getStatus, 1));
|
||||
AppApiKeysPlus key = keys.getOne(new LambdaQueryWrapper<AppApiKeysPlus>().eq(AppApiKeysPlus::getAppId, order.getAppId())
|
||||
.eq(AppApiKeysPlus::getStatus, 1).isNull(AppApiKeysPlus::getDeletedAt)
|
||||
.and(wrapper -> wrapper.isNull(AppApiKeysPlus::getExpiredAt).or().gt(AppApiKeysPlus::getExpiredAt, new Date()))
|
||||
.last("limit 1"));
|
||||
java.util.Map<String, Object> signatureValues = new java.util.HashMap<String, Object>(); signatureValues.put("order_id", order.getOrderId()); signatureValues.put("trade_id", order.getTradeId()); signatureValues.put("status", order.getStatus()); signatureValues.put("transaction_id", order.getBlockTransactionId());
|
||||
headers.set("X-Payment-Signature", key == null ? "" : PaymentSignatureService.sign(signatureValues, key.getApiSecret()));
|
||||
headers.set("X-Payment-Api-Key", key == null ? "" : key.getApiKey());
|
||||
ResponseEntity<String> response = http.postForEntity(order.getNotifyUrl(), new HttpEntity<String>(body, headers), String.class);
|
||||
order.setCallbackNum(order.getCallbackNum() == null ? 1L : order.getCallbackNum() + 1L);
|
||||
if (response.getStatusCode().is2xxSuccessful() && "ok".equalsIgnoreCase(response.getBody() == null ? "" : response.getBody().trim())) order.setCallbackConfirm(1L);
|
||||
|
||||
Reference in new issue
Block a user