简体   繁体   English

Sendgrid webhook 验证总是失败

[英]Sendgrid webhook verification fails always

I cannot get the Sendgrid public key verification to work in my application.我无法在我的应用程序中使用 Sendgrid 公钥验证。 I already have all the prerequisites configured.我已经配置了所有先决条件。 (API key is added, Signed webhook is enabled etc) (添加了 API 密钥,启用了签名的 webhook 等)

This is my approach to test the webhook.这是我测试 webhook 的方法。

  1. I register a webhook.site url as the webhook in Sendgrid我将 webhook.site url 注册为Sendgrid中的 webhook
  2. I invoke the webhook from Sendgrid so that I get the call to webook.site我从 Sendgrid 调用 webhook,这样我就可以调用 webook.site
  3. I export the request received to webhook.site as a Curl.我将收到的请求作为 Curl 导出到 webhook.site。
  4. I import it into Postman我将其导入 Postman
  5. In Postman, I change the URL to a one from a backend service that is running in my localhost and invoke the call from Postman.在 Postman 中,我将 URL 更改为在我的本地主机中运行的后端服务中的一个,并调用来自 Postman 的调用。

Here is my code to verify the signature.这是我验证签名的代码。 This is a exact copy of what Sendgrid has provided here .这是 Sendgrid 在此处提供的内容的精确副本。

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);
}

Now this method always returns false when it is called from below controller method.现在,当从 controller 方法下方调用此方法时,它总是返回 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;
}

I am unable to find the cause for that honestly.老实说,我找不到原因。

Can someone help here.有人可以帮忙吗。

You will need to add the \r\n with the request body to pass the verification.您需要在请求正文中添加 \r\n 才能通过验证。 It passes for me when I run the service in Windows OS but it fails in unix system.当我在 Windows 操作系统中运行该服务时,它通过了我,但它在 unix 系统中失败了。 I tried using System.getProperty("line.separator") also.Does anyone know the solution for unix system.我也尝试使用 System.getProperty("line.separator")。有谁知道 unix 系统的解决方案。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 验证 SendGrid 的签名事件 Webhook? - Verifying SendGrid's Signed Event Webhook? 如何在 sendgrid 中接收单个发件人的验证令牌 - How to receive a verification token for single sender in sendgrid SendGrid 邮件始终处于待处理状态 - SendGrid mails are always in pending status 如何从 Sendgrid.php 文件中获取正确的响应/错误日志? 即使失败,“fetch”目前始终是 200 响应 - How to get correct responses/error logs from Sendgrid.php file? even if it fails, "fetch" is always a 200 response currently SendGrid webhook 事件可以包含来自个性化的 custom_args 吗? - Can SendGrid webhook events include custom_args from personalizations? 如何让 Sendgrid Inbound Parse Webhook 在生产中的 Rails 应用程序中工作? - How to get Sendgrid Inbound Parse Webhook working in rails app on production? 通过使用 SendGrid 的单独 email 验证注册定制 - Sign up customization with separate email verification using SendGrid Firebase:Email 验证链接总是过期,即使验证有效 - Firebase: Email verification link always expired even though verification works Sendgrid 入站 webhook 数据在 Flask 中完全空白,但可以处理从请求发送的请求? - Sendgrid inbound webhook data entirely blank in Flask but works with a request sent from requests? 本地化 TemplateId InputParameter 值在 Azure B2C 自定义策略与 SendGrid 自定义 email 验证 - Localize TemplateId InputParameter value in Azure B2C custom policy with SendGrid custom email verification
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM