【问题标题】:AWS SignatureDoesNotMatch errorAWS SignatureDoesNotMatch 错误
【发布时间】:2017-01-02 07:57:04
【问题描述】:

我知道在 SO 上已经提出了大约 8 个类似标题的问题,但没有一个对我的问题有帮助。

所以,我想在这里发布我的问题:

我不断收到这个众所周知的 SignatureDoesNotMatch 错误,看起来非常简单明了,但我无法解决它,所以请帮忙。

这是我抛出此异常的 Java 代码:

public static void main(String[] args) {
        AWSCredentials credentials = null;
        try {
            credentials = new ProfileCredentialsProvider("default").getCredentials();
        } catch (Exception e) {
            throw new AmazonClientException("Cannot load the credentials from the credential profiles file. "
                + "Please make sure that your credentials file is at the correct "
                + "location (/Users/USERNAME/.aws/credentials), and is in valid format.", e);
        }

        AmazonElastiCacheClient client = new AmazonElastiCacheClient(credentials);
        client.setRegion(Region.getRegion(Regions.AP_NORTHEAST_2));
        DescribeCacheClustersRequest dccRequest = new DescribeCacheClustersRequest();
        dccRequest.setShowCacheNodeInfo(true);

        DescribeCacheClustersResult clusterResult = client.describeCacheClusters(dccRequest);

上述代码段的最后一行不断抛出以下堆栈跟踪:

Exception in thread "main" com.amazonaws.services.elasticache.model.AmazonElastiCacheException: The request signature we calculated does not match the signature you provided. Check your AWS Secret Access Key and signing method. Consult the service documentation for details.

The Canonical String for this request should have been
'POST
/

amz-sdk-invocation-id:84ad8256-0ff0-343d-2f8d-6794d303d9c3
amz-sdk-retry:3/246/485
host:elasticache.ap-northeast-2.amazonaws.com
user-agent:aws-sdk-java/1.11.75 Mac_OS_X/10.11.6 Java_HotSpot(TM)_64-Bit_Server_VM/25.102-b14/1.8.0_102
x-amz-date:20161231T043533Z

amz-sdk-invocation-id;amz-sdk-retry;host;user-agent;x-amz-date
2747a03a915959eb5776c08ca15deccb190033381677a3b9dbaf8fc42fa54a0c'

The String-to-Sign should have been
'AWS4-HMAC-SHA256
20161231T043533Z
20161231/ap-northeast-2/elasticache/aws4_request
3e08746ee0b6cb44c7dcbbef810682adade54dbcc143688a47890a7baa90577a' (Service: AmazonElastiCache; Status Code: 403; Error Code: SignatureDoesNotMatch; Request ID: 910c6c16-cf12-11e6-bf57-4b2610895ff0)
    at com.amazonaws.http.AmazonHttpClient$RequestExecutor.handleErrorResponse(AmazonHttpClient.java:1586)
    at com.amazonaws.http.AmazonHttpClient$RequestExecutor.executeOneRequest(AmazonHttpClient.java:1254)
    at com.amazonaws.http.AmazonHttpClient$RequestExecutor.executeHelper(AmazonHttpClient.java:1035)
    at com.amazonaws.http.AmazonHttpClient$RequestExecutor.doExecute(AmazonHttpClient.java:747)
    at com.amazonaws.http.AmazonHttpClient$RequestExecutor.executeWithTimer(AmazonHttpClient.java:721)
    at com.amazonaws.http.AmazonHttpClient$RequestExecutor.execute(AmazonHttpClient.java:704)
    at com.amazonaws.http.AmazonHttpClient$RequestExecutor.access$500(AmazonHttpClient.java:672)
    at com.amazonaws.http.AmazonHttpClient$RequestExecutionBuilderImpl.execute(AmazonHttpClient.java:654)
    at com.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:518)
    at com.amazonaws.services.elasticache.AmazonElastiCacheClient.doInvoke(AmazonElastiCacheClient.java:2725)
    at com.amazonaws.services.elasticache.AmazonElastiCacheClient.invoke(AmazonElastiCacheClient.java:2701)
    at com.amazonaws.services.elasticache.AmazonElastiCacheClient.describeCacheClusters(AmazonElastiCacheClient.java:1431)
    at sporadic.AmazonElastiCacheClientExample.main(AmazonElastiCacheClientExample.java:44)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)

我的规格: aws-java-sdk 是 1.11.75(截至 2016 年 12 月 30 日的最新版本)。 爪哇:1.8.0_102

此外,我使用 /.aws/credentials 中的相同凭据从 Chrome 登录 AWS 控制台,并且可以正常查看我的 Elasticache 实例。 所以我认为我的凭据没有任何问题。

请帮忙。 我还在 Github 上发布了我的问题:https://github.com/aws/aws-sdk-java/issues/967

【问题讨论】:

  • 相同的代码是否适用于不同的区域(试试 us-east-1)?它是否也适用于不同的 sigv4-only 区域(尝试 eu-central-1)?
  • 感谢 @jarmod 的评论,我分别尝试了 us-east-1 和 eu-central-1,均无效,并且抛出完全相同的堆栈跟踪,还有更多线索吗?
  • 我使用 AWS Java SDK 1.11.75 针对 Java 1.7.0_75、1.8.0_31 和 1.8.0_112 测试了您的代码,并且代码在所有情况下都运行良好。您可以在不同网络(例如 EC2 实例)上的另一台机器上运行它以消除代理/网络问题吗?
  • 再次感谢@jarmod 的帮助!但可悲的是,我尝试在我的另一台 MacBook Pro 上使用四种不同的 Wifi 网络,无论是办公室还是家里,都抱怨同样的错误......无论如何,我将继续尽我所能解决这个问题,这个与我的一个非常重要的项目有关。谢谢。
  • 我能够通过故意使用错误的秘密访问密钥来重现您的问题。您的访问密钥 ID 和秘密访问密钥都正确吗?

标签: java amazon-web-services amazon-elasticache


【解决方案1】:

我使用 AWS Java SDK 1.11.75 针对各种 Java 7 和 8 运行时测试了您的代码,并且代码在所有情况下都运行良好。所以,我认为代码很好。

但是,我能够通过故意使用错误的秘密访问密钥来重现该问题。您的访问密钥 ID 和秘密访问密钥都正确吗?

【讨论】:

    猜你喜欢
    • 2019-01-29
    • 2016-11-02
    • 2016-10-12
    • 1970-01-01
    • 2012-05-02
    • 2016-07-14
    • 2015-07-06
    • 2014-12-31
    • 2014-04-22
    相关资源
    最近更新 更多