回调问题处理
This commit is contained in:
1 parent
43dec4c94b
commit
1151dc2535
7 files changed
+99
-16
No files matched your search
@@ -4,6 +4,8 @@ import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.ruoyi.system.rental.RentalCallbackDemoController;
|
||||
import com.ruoyi.system.rental.RentalStore;
|
||||
import com.ruoyi.system.rental.RentalIdentity;
|
||||
import com.ruoyi.system.rental.RentalCrypto;
|
||||
import com.ruoyi.system.rental.RentalValues;
|
||||
import java.io.IOException;
|
||||
import java.nio.ByteBuffer;
|
||||
@@ -27,6 +29,9 @@ public class WebhookDebugController {
|
||||
private static final int MAX_BODY_BYTES = 512 * 1024;
|
||||
private final RentalStore store;
|
||||
private final ObjectMapper json;
|
||||
private final RentalIdentity identity;
|
||||
@Value("${rental.webhook-debug.credential-source:APP}")
|
||||
private String credentialSource = "APP";
|
||||
@Value("${rental.webhook-debug.api-key:}")
|
||||
private String apiKey;
|
||||
@Value("${rental.webhook-debug.api-secret:}")
|
||||
@@ -35,15 +40,21 @@ public class WebhookDebugController {
|
||||
@Value("${rental.webhook-debug.signature-path:}")
|
||||
private String signaturePath;
|
||||
|
||||
public WebhookDebugController(RentalStore store, ObjectMapper json) {
|
||||
public WebhookDebugController(RentalStore store, ObjectMapper json, RentalIdentity identity) {
|
||||
this.store = store;
|
||||
this.json = json;
|
||||
this.identity = identity;
|
||||
}
|
||||
|
||||
@PostMapping(value = "/api/webhook/debug", consumes = MediaType.APPLICATION_JSON_VALUE,
|
||||
produces = MediaType.TEXT_PLAIN_VALUE)
|
||||
public ResponseEntity<String> receive(HttpServletRequest request) throws IOException {
|
||||
if (apiKey == null || apiKey.isEmpty() || apiSecret == null || apiSecret.isEmpty()) {
|
||||
boolean appCredentials = "APP".equalsIgnoreCase(credentialSource);
|
||||
if (!appCredentials && !"CONFIGURED".equalsIgnoreCase(credentialSource)) {
|
||||
LOG.warn("租赁调试回调拒绝 reason=INVALID_CREDENTIAL_SOURCE");
|
||||
return ResponseEntity.status(503).body("invalid credential source");
|
||||
}
|
||||
if (!appCredentials && (apiKey == null || apiKey.isEmpty() || apiSecret == null || apiSecret.isEmpty())) {
|
||||
LOG.warn("租赁调试回调拒绝 reason=CREDENTIAL_NOT_CONFIGURED");
|
||||
return ResponseEntity.status(503).body("callback credentials not configured");
|
||||
}
|
||||
@@ -63,28 +74,65 @@ public class WebhookDebugController {
|
||||
return ResponseEntity.status(413).body("payload too large");
|
||||
}
|
||||
final JsonNode event;
|
||||
String verifiedKey = apiKey;
|
||||
String stage = "SIGNATURE";
|
||||
String authenticationReason = "BODY_ENCODING_INVALID";
|
||||
try {
|
||||
String body = StandardCharsets.UTF_8.newDecoder().onMalformedInput(CodingErrorAction.REPORT)
|
||||
.decode(ByteBuffer.wrap(buffer, 0, total)).toString();
|
||||
RentalCallbackDemoController.verify(apiKey, apiSecret, request.getHeader("X-Rent-Key"),
|
||||
Long.parseLong(request.getHeader("X-Rent-Timestamp")), request.getHeader("X-Rent-Nonce"),
|
||||
String key = request.getHeader("X-Rent-Key");
|
||||
String secret = apiSecret;
|
||||
authenticationReason = "KEY_MISSING";
|
||||
RentalValues.require(key != null && !key.isEmpty() && key.length() <= 128, "通知Key缺失或过长");
|
||||
if (appCredentials) {
|
||||
stage = "CREDENTIAL_LOOKUP";
|
||||
authenticationReason = "ACTIVE_APP_CREDENTIAL_NOT_FOUND";
|
||||
java.util.Map<String, Object> credential = store.one(
|
||||
"SELECT * FROM rent_credential WHERE api_key=? AND owner_type='APP' AND status='ACTIVE'"
|
||||
+ " AND (expires_at IS NULL OR expires_at>?)", key, RentalValues.now());
|
||||
RentalValues.require(credential != null, "未找到有效应用凭证");
|
||||
authenticationReason = "CREDENTIAL_DECRYPT_FAILED";
|
||||
secret = identity.secret(credential);
|
||||
verifiedKey = key;
|
||||
}
|
||||
stage = "SIGNATURE";
|
||||
authenticationReason = "KEY_MISMATCH";
|
||||
RentalValues.require(key.equals(verifiedKey), "通知Key不匹配");
|
||||
String nonce = request.getHeader("X-Rent-Nonce");
|
||||
authenticationReason = "NONCE_INVALID";
|
||||
RentalValues.require(nonce != null && nonce.matches("[A-Za-z0-9_-]{16,64}"), "通知nonce格式无效");
|
||||
authenticationReason = "TIMESTAMP_NOT_UNIX_MILLIS";
|
||||
String rawTimestamp = request.getHeader("X-Rent-Timestamp");
|
||||
RentalValues.require(rawTimestamp != null && rawTimestamp.matches("[0-9]{13}"), "通知时间戳格式无效");
|
||||
long timestamp = Long.parseLong(rawTimestamp);
|
||||
long now = System.currentTimeMillis();
|
||||
LOG.info("租赁调试回调认证参数检查 source={} keyFingerprint={} timestampSkewMs={} nonceLength={} signaturePresent={}",
|
||||
credentialSource, RentalCrypto.sha256(key).substring(0, 12), now - timestamp, nonce.length(),
|
||||
request.getHeader("X-Rent-Signature") != null);
|
||||
authenticationReason = "TIMESTAMP_OUT_OF_WINDOW";
|
||||
RentalValues.require(timestamp > now - 60000 && timestamp < now + 60000, "通知时间戳超窗");
|
||||
authenticationReason = "SIGNATURE_MISMATCH_CHECK_SECRET_PATH_AND_BODY";
|
||||
RentalCallbackDemoController.verify(verifiedKey, secret, key,
|
||||
timestamp, nonce,
|
||||
path, body, request.getHeader("X-Rent-Signature"), System.currentTimeMillis());
|
||||
stage = "EVENT_JSON";
|
||||
authenticationReason = "EVENT_JSON_INVALID";
|
||||
event = json.readTree(body);
|
||||
RentalValues.require(event != null && event.isObject() && event.path("event_no").isTextual()
|
||||
&& !event.path("event_no").asText().isEmpty()
|
||||
&& event.path("event_no").asText().length() <= 64, "通知事件无效");
|
||||
} catch (Exception ex) {
|
||||
// 不输出异常原文、认证请求头、Secret、签名及正文。
|
||||
LOG.warn("租赁调试回调拒绝 stage={} reason=INVALID_NOTIFICATION exceptionType={}",
|
||||
stage, ex.getClass().getSimpleName());
|
||||
return ResponseEntity.status("SIGNATURE".equals(stage) ? 401 : 400).body("invalid notification");
|
||||
LOG.warn("租赁调试回调拒绝 stage={} reason={} exceptionType={}",
|
||||
stage, authenticationReason, ex.getClass().getSimpleName());
|
||||
return ResponseEntity.status("CREDENTIAL_LOOKUP".equals(stage)
|
||||
&& !"ACTIVE_APP_CREDENTIAL_NOT_FOUND".equals(authenticationReason) ? 503
|
||||
: "EVENT_JSON".equals(stage) ? 400 : 401).body("invalid notification");
|
||||
}
|
||||
try {
|
||||
// 独立scope和数据库唯一键跨实例防重放;合法事件重投使用新nonce。
|
||||
// 此接口不执行记账,重复event_no允许观察;正式业务应另做事件幂等处理。
|
||||
store.insert("nonce", RentalValues.map("api_key", apiKey, "scope", "DEBUG_WEBHOOK",
|
||||
store.insert("nonce", RentalValues.map("api_key", verifiedKey, "scope", "DEBUG_WEBHOOK",
|
||||
"nonce", request.getHeader("X-Rent-Nonce"), "expires_at", RentalValues.after(180)));
|
||||
} catch (DuplicateKeyException ex) {
|
||||
LOG.warn("租赁调试回调拒绝 reason=NONCE_REPLAY");
|
||||
|
||||
+7
-3
@@ -112,9 +112,13 @@ public class RentalCallbackDemoController {
|
||||
/** 验证实际原文及WEBHOOK域;代理必须保留回调路径,不可二次序列化JSON。 */
|
||||
public static void verify(String expectedKey, String secret, String key, long timestamp,
|
||||
String nonce, String path, String body, String signature, long now) {
|
||||
require(expectedKey != null && !expectedKey.isEmpty() && secret != null && !secret.isEmpty()
|
||||
&& expectedKey.equals(key) && nonce != null && nonce.matches("[A-Za-z0-9_-]{16,64}")
|
||||
&& timestamp > now - 60000 && timestamp < now + 60000, "通知认证参数无效");
|
||||
require(expectedKey != null && !expectedKey.isEmpty() && secret != null && !secret.isEmpty(),
|
||||
"通知接收端凭证未配置");
|
||||
require(key != null && !key.isEmpty(), "通知缺少X-Rent-Key");
|
||||
require(expectedKey.equals(key), "通知API Key与接收端凭证不匹配");
|
||||
require(nonce != null && nonce.matches("[A-Za-z0-9_-]{16,64}"), "通知nonce格式无效");
|
||||
require(timestamp > now - 60000 && timestamp < now + 60000,
|
||||
"通知时间戳超出60秒窗口,请使用Unix毫秒并同步服务器时钟");
|
||||
require(RentalCrypto.equal(RentalCrypto.sign(secret, "WEBHOOK", key, timestamp,
|
||||
nonce, "POST", path, body), signature), "通知签名无效");
|
||||
}
|
||||
|
||||
@@ -368,6 +368,7 @@ public class RentalWorkers {
|
||||
String responseBody = RentalHttp.body(response);
|
||||
acked = "ok".equals(responseBody);
|
||||
if (!acked) {
|
||||
LOG.error("responseBody {}",responseBody);
|
||||
failure = "ACK_BODY_NOT_EXACT_OK";
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user