【问题标题】:Sendgrid webhook verification fails alwaysSendgrid webhook 验证总是失败
【发布时间】:2023-01-04 07:13:02
【问题描述】:

我无法在我的应用程序中使用 Sendgrid 公钥验证。我已经配置了所有先决条件。 (添加了 API 密钥,启用了签名的 webhook 等)

这是我测试 webhook 的方法。

  1. 我在 Sendgrid 中注册了一个 webhook.site url 作为 webhook
  2. 我从 Sendgrid 调用 webhook,这样我就可以调用 webook.site
  3. 我将收到的请求作为 Curl 导出到 webhook.site。
  4. 我将其导入 Postman
  5. 在 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


    【解决方案1】:

    您将需要添加 与请求主体一起通过验证。当我在 Windows 操作系统中运行该服务时,它通过了,但在 unix 系统中失败了。我也尝试使用 System.getProperty("line.separator")。有谁知道 unix 系统的解决方案。

    【讨论】:

      猜你喜欢
      • 2016-10-15
      • 2016-01-18
      • 2016-03-25
      • 1970-01-01
      • 1970-01-01
      • 2011-06-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多