【问题标题】:Generating AWS SQS signature from java从 java 生成 AWS SQS 签名
【发布时间】:2021-09-07 02:22:34
【问题描述】:

由于公司的一些代理问题,我需要使用 Spring RestTemplate(绑定代理)而不是 aws sdk 或 spring cloud aws 消息从 AWS SQS 获取消息。

我需要生成一个包含这样的授权签名的标头字符串;

curl --location --request GET 'https://sqs.us-east-2.amazonaws.com/523535599964/MyAmazingQueue?Action=ReceiveMessage' \
--header 'X-Amz-Date: 20210623T133108Z' \
--header 'Authorization: AWS4-HMAC-SHA256 Credential=AKIAXTZJHJFOEKI4UF45/20210623/us-east-2/sqs/aws4_request, SignedHeaders=host;x-amz-date, Signature=2d926124ce07ca41c0f56af3bdddc81df19444df189b727ff02015f620cdfc6c'

这是由 Postman 生成的。

我需要用 java 生成这个签名并绑定到 HttpHeaders 对象。

HttpHeaders headers = new HttpHeaders();
headers.add("Authorization", <generatedAuthString>);
headers.add("X-Amz-Date", <generatedAnother>);
.
.
HttpEntity<String> entity = new HttpEntity<>(null, headers);

response = restTemplate.exchange(url, HttpMethod.GET, entity, String.class);

我更喜欢使用 rest 模板,因为使用 aws sdk 在添加如下所示的代理时不起作用。应用程序启动时出现超时错误。


public AmazonSQSAsync amazonSQSAsync() {
    ClientConfiguration clientConfiguration= new ClientConfiguration();
    clientConfiguration.setProxyHost(proxyurl);
    clientConfiguration.setProxyPort(proxyport);
    return AmazonSQSAsyncClientBuilder.standard().withRegion(region)
            .withCredentials(new AWSStaticCredentialsProvider(new BasicAWSCredentials(awsAccessKey, awsSecretKey)))
            .build();
}

我尝试了这个 stackoverflow 问题中的每个项目,但我得到了 AccessDeniedException 或 SignatureDoesNotMatch 异常。

How to generate Signature in AWS from Java

如果你能帮忙就太好了。 谢谢。

【问题讨论】:

标签: java amazon-web-services message-queue amazon-sqs aws-sqs-fifo


【解决方案1】:

我终于解决了:)

AmazonSQSClientBuilder builder = AmazonSQSClientBuilder.standard();
AmazonSQS sqs = builder.withClientConfiguration(
        PredefinedClientConfigurations.defaultConfig()
                .withProxyHost("**")
                .withProxyPort(****).withProxyPassword("***").withProxyUsername("****"))
        .withCredentials(new AWSStaticCredentialsProvider(credentials))
        .withRegion(Regions.US_EAST_2)
        .build();

我使用了spring cloud aws的aws sdk 我使用了 ClientConfiguration 的 PredefinedClientConfigurations。 谢谢大家!

【讨论】:

    猜你喜欢
    • 2016-06-29
    • 1970-01-01
    • 1970-01-01
    • 2022-01-18
    • 2019-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-20
    相关资源
    最近更新 更多