簡體   English   中英

從 Java 生成 AWS SQS 簽名

[英]Generating AWS SQS signature from java

由於公司的一些代理問題,我需要使用 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'

這是由郵遞員生成的。 在此處輸入圖片說明

我需要用 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 異常。

如何從 Java 在 AWS 中生成簽名

如果你能幫忙就太好了。 謝謝你。

我終於解決了:)

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 。 謝謝大家!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM