AWS Cloudfront:Returns 使用签名 Cookie 拒绝访问

AWS Cloufront : Returns Access Denied using Signed Cookies

我可以使用带有自定义域的 url 签名的云端访问我的原始对象。 当我尝试使用云端签名的 cookie 时,问题就来了,然后我从云端收到了 403 拒绝访问响应。

<Error>
   <Code>AccessDenied</Code>
   <Message>Access denied</Message>
</Error>

我用的完全一样:

以及我用于签名的云端和原始配置 url。

下面是我使用的nodejs代码。真不知道怎么回事

非常感谢任何帮助!

谢谢!

var moment = require('moment');

var AWS = require('aws-sdk');
var keyPairId = 'APKAIPZ5BRDMEXAMPLE';

var privateKey = '-----BEGIN RSA PRIVATE KEY-----\n' +
   'MIIEowIBAAKCAQEAsL+Tz/soZcDQLXcgJE89h5RHfZY+ddNIVI/3T0OvjOOFVRLp\n' +
   '0gN+06cwXWsvlmeEbnh5XdklA38H4p3wt5mxqip4YmuvbW6i+8UALHxPW0LTmiz0\n' +
   's4J6HB0eXhm3cbFV60i1DNat+W2q5miqlXqxKSdg+UbUGlFA5CbR\n' +
   '-----END RSA PRIVATE KEY-----';

var signer = new AWS.CloudFront.Signer(keyPairId, privateKey);
var expireTime = moment().add(3600, 'sec').unix();

exports.handler = (event, context, callback) => {

   var domain = 'mydomain.com';
   var resource = 'https://cdn.mydomain.com/*';
   var attributes = '; domain=' + domain + '; path=/; secure; httpOnly'

   var options = {policy : '{"Statement":[{"Resource":"' + resource + 
   '","Condition":{"DateLessThan":{"AWS:EpochTime":' + expireTime + 
    '}}}]}'};

    signer.getSignedCookie(options, function(err, data) {

        if (err) {
        //do somehing

        } else {

           context.done(null, {

            'Set-Cookie': 'CloudFront-Policy=' + data["CloudFront-Policy"] + attributes,
            'SEt-Cookie': 'CloudFront-Signature=' + data["CloudFront-Signature"] + attributes,
            'SeT-Cookie': 'CloudFront-Key-Pair-Id=' + data["CloudFront-Key-Pair-Id"] + attributes


       });

    }
});
};

不要使用 moment(),而是使用 Math() 函数来计算您的过期时间,这样就可以了!

var expireTime = Math.round((new Date()).getTime() / 1000) + 3600;