我是 SendGrid 的新手,我可以使用 Spring Boot 使用它發送電子郵件,一個沒有任何安全性的示例 webhook 也可以使用。現在我想驗證 API 的安全性,我正在使用 ecdsa 驗證,但是每次我驗證時它都會給出 false。我用谷歌搜索了很多,但找不到作業程式。
@PostMapping("webhook")
public void webhookVerify(@RequestBody List<Webhook> webhook, @RequestHeader Map<String, String> headers) throws Exception {
System.out.println(verify(sendGridVerificationKey, webhook, headers.get("x-twilio-email-event-webhook-signature"), headers.get("x-twilio-email-event-webhook-timestamp"));
}
private boolean verify(final String publicKey, List<Webhook> webhook, final String signature, final String timestamp) throws Exception {
final EventWebhook ew = new EventWebhook();
final ECPublicKey ellipticCurvePublicKey = ew.ConvertPublicKeyToECDSA(publicKey);
return ew.VerifySignature(ellipticCurvePublicKey, payload, signature, timestamp);
}
任何人都能夠驗證相同?
uj5u.com熱心網友回復:
我一直在為同樣的問題苦苦掙扎,但最終能夠驗證簽名。在這種情況下,SendGrid V3 檔案不是很有用。關鍵是 Security.addProvider(new BouncyCastleProvider());在應用程式啟動的時候使用,你的payload應該是位元組格式,不要試圖改變或轉換。
這是一個對我有幫助的有效解決方案。
@PostMapping("webhook")
public void webhook(@RequestBody byte[] webhook, @RequestHeader Map<String, String> headers) throws Exception {
String signature = headers.get("x-twilio-email-event-webhook-signature");
String timeStamp = headers.get("x-twilio-email-event-webhook-timestamp");
if (verify(sendGridVerificationKey, webhook, signature, timeStamp)) {
String str = new String(webhook, StandardCharsets.UTF_8);
System.out.println("ECDSA signature verification true, Webhook = " str);
} else {
throw new InvalidSignatureValueException("ECDSA signature doesn't match");
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/335977.html
標籤:爪哇 弹簧靴 发送网格 sendgrid-api-v3
上一篇:帶有Hikari的SpringDataJPA:為什么hikari自動提交屬性設定為“true”?
下一篇:Thymeaf計算出的URL值
