简体   繁体   English

从 Java 生成 AWS SQS 签名

[英]Generating AWS SQS signature from java

I need to get message from AWS SQS with using Spring RestTemplate(binded a proxy), instead of aws sdk or spring cloud aws messaging, because of some proxy problem of the company.由于公司的一些代理问题,我需要使用 Spring RestTemplate(绑定代理)而不是 aws sdk 或 spring cloud aws 消息从 AWS SQS 获取消息。

I need to generate a header string includes Authorization signature like that;我需要生成一个包含授权签名的标头字符串;

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'

This is generated by Postman.这是由邮递员生成的。 在此处输入图片说明

I need to generate this signaturewith java and bind to HttpHeaders object.我需要用 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);

I prefered using rest template, because using aws sdk while adding a proxy like below, doesnt work.我更喜欢使用 rest 模板,因为在添加如下代理时使用 aws sdk 不起作用。 Im getting timeout error when application is starting.应用程序启动时出现超时错误。


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();
}

I tried every item in this stackoverflow question, but i got AccessDeniedException or SignatureDoesNotMatch exceptinon.我尝试了这个 stackoverflow 问题中的每个项目,但我得到了 AccessDeniedException 或 SignatureDoesNotMatch 异常。

How to generate Signature in AWS from Java 如何从 Java 在 AWS 中生成签名

Would be great if you could help.如果你能帮忙就太好了。 Thank you.谢谢你。

I solved finally :)我终于解决了:)

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

I used aws sdk instated of spring cloud aws I used PredefinedClientConfigurations instated of ClientConfiguration.我使用了 spring cloud aws 的 aws sdk 我使用了 ClientConfiguration 的 PredefinedClientConfigurations 。 thanks all!谢谢大家!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM