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 年前檢視次數 271 次
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

您尚未登入。 登入 去張貼答案。

一個好的回答可以清楚地回答問題並提供建設性的意見回饋,同時有助於提問者的專業成長。

回答問題指南