验签修改
This commit is contained in:
1 parent
9daba4a98e
commit
454da449c4
2 files changed
+10
-15
No files matched your search
+7
-13
@@ -33,7 +33,6 @@ import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.PublicKey;
|
||||
import java.security.Signature;
|
||||
import java.util.Base64;
|
||||
@@ -225,12 +224,15 @@ public class SysUsdtDemoController {
|
||||
&& status != null && "2".equals(String.valueOf(status));
|
||||
}
|
||||
|
||||
/** API Key和签名均使用常量时间比较,避免泄露匹配前缀。 */
|
||||
/**
|
||||
* 回调采用 RSA 单验签。创建请求加密与回调验签必须使用同一应用公钥,
|
||||
* API Key仅用于标识本次回调,不参与认证,避免密钥轮换后拒绝合法回调。
|
||||
*/
|
||||
private boolean verifyCallback(Map<String, ?> values, String apiKey, String signature) {
|
||||
String expectedApiKey = requiredEnvWithAlias("PAYMENT_API_KEY", "PAYMENT_API_KEK");
|
||||
if (!constantTimeEquals(expectedApiKey, apiKey) || StringUtils.isBlank(signature)) return false;
|
||||
if (StringUtils.isBlank(signature)) return false;
|
||||
try {
|
||||
PublicKey publicKey = RsaKeyUtil.loadPublicKey(requiredEnv("PAYMENT_CALLBACK_PUBLIC_KEY").replace("\\n", "\n"));
|
||||
PublicKey publicKey = RsaKeyUtil.loadPublicKey(
|
||||
requiredEnv("PAYMENT_RSA_PUBLIC_KEY").replace("\\n", "\n"));
|
||||
Signature verifier = Signature.getInstance("SHA256withRSA");
|
||||
verifier.initVerify(publicKey);
|
||||
verifier.update(PaymentSignatureService.canonicalText(values).getBytes(StandardCharsets.UTF_8));
|
||||
@@ -240,14 +242,6 @@ public class SysUsdtDemoController {
|
||||
}
|
||||
}
|
||||
|
||||
private boolean constantTimeEquals(String expected, String actual) {
|
||||
if (expected == null || actual == null) {
|
||||
return false;
|
||||
}
|
||||
return MessageDigest.isEqual(expected.getBytes(StandardCharsets.UTF_8),
|
||||
actual.getBytes(StandardCharsets.UTF_8));
|
||||
}
|
||||
|
||||
private String normalizeWalletUserName(String value) {
|
||||
if (StringUtils.isBlank(value)) {
|
||||
return "demo-user-" + UUID.randomUUID().toString().replace("-", "");
|
||||
|
||||
Reference in new issue
Block a user