【发布时间】:2023-01-04 07:13:02
【问题描述】:
我无法在我的应用程序中使用 Sendgrid 公钥验证。我已经配置了所有先决条件。 (添加了 API 密钥,启用了签名的 webhook 等)
这是我测试 webhook 的方法。
- 我在 Sendgrid 中注册了一个 webhook.site url 作为 webhook
- 我从 Sendgrid 调用 webhook,这样我就可以调用 webook.site
- 我将收到的请求作为 Curl 导出到 webhook.site。
- 我将其导入 Postman
- 在 Postman 中,我将 URL 更改为来自在我的本地主机中运行的后端服务的 URL,并调用来自 Postman 的调用。
这是我验证签名的代码。这是 Sendgrid 提供的内容的精确副本here。
public boolean VerifySignature(ECPublicKey publicKey, byte[] payload, String signature, String timestamp) throws NoSuchAlgorithmException, NoSuchProviderException, InvalidKeyException, SignatureException, IOException { // prepend the payload with the timestamp final ByteArrayOutputStream payloadWithTimestamp = new ByteArrayOutputStream(); payloadWithTimestamp.write(timestamp.getBytes()); payloadWithTimestamp.write(payload); // create the signature object final Signature signatureObject = Signature.getInstance("SHA256withECDSA", "BC"); signatureObject.initVerify(publicKey); signatureObject.update(payloadWithTimestamp.toByteArray()); // decode the signature final byte[] signatureInBytes = Base64.getDecoder().decode(signature); // verify the signature return signatureObject.verify(signatureInBytes); }现在,当从下面的控制器方法调用时,此方法始终返回 false。
@PostMapping("/sendgrid-callback") public boolean acceptSendgridCallback( @RequestBody String rawData, @RequestHeader("X-Twilio-Email-Event-Webhook-Timestamp") String timestamp, @RequestHeader("X-Twilio-Email-Event-Webhook-Signature") String signature ) throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchProviderException, SignatureException, IOException, InvalidKeyException { System.out.println("Req body = \n" + rawData); ECPublicKey ecdsaKey = eventWebhook.ConvertPublicKeyToECDSA ("public key taken from sendgrid"); boolean b = eventWebhook.VerifySignature(ecdsaKey, rawData, signature, timestamp); return b; }老实说,我找不到原因。
有人可以帮忙吗。
【问题讨论】:
标签: twilio webhooks sendgrid ecdsa sendgrid-api-v3