How do I create and validate a signature using the EventStreamAws4Signer class from the SNS Java SDK?

0

I've asked ChatGPT to give me an example on how to do this, it came up with the following:

    public void publishMessage(String topicArn, String message) {
        SnsClient snsClient = SnsClient.builder()
                .region(Region.US_WEST_2)
                .credentialsProvider(EnvironmentVariableCredentialsProvider.create())
                .build();

        PublishRequest publishRequest = PublishRequest.builder()
                .topicArn(topicArn)
                .message(message)
                .build();

        EventStreamAws4Signer signer = EventStreamAws4Signer.builder()
                .credentialsProvider(EnvironmentVariableCredentialsProvider.create())
                .build();

        SdkBytes payload = SdkBytes.fromByteArray(message.getBytes());

        String signature = signer.sign(publishRequest, payload).get("signature");

        snsClient.publish(builder -> builder
                .message(message)
                .topicArn(topicArn)
                .signature(signature)
                .build());
    }

In reality EventStreamAws4Signer doesn't have a builder() method, only a create() method. But the latter method doesn't provide a way to configure the access keys or use the instance profile access keys for signing.

How do I use the EventStreamAws4Signer to get a signature? I've looked at the code examples in the docs here: https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/java_sns_code_examples.html and also in the code examples on github here: https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/javav2/example_code/sns/src/main/java/com/example/sns

But couldn't find what i was looking for. I need to know how I can both create a signature and how to validate a signature. Thank you

已提问 1 年前272 查看次数
1 回答
0
  • this is not helpful. I need to know how to create my own signature string and how to verify signature strings extracted from the headers of other SNS notifications

您未登录。 登录 发布回答。

一个好的回答可以清楚地解答问题和提供建设性反馈,并能促进提问者的职业发展。

回答问题的准则

相关内容