【问题标题】:Serving private content from CloudFront with Signed Cookies使用签名 Cookie 从 CloudFront 提供私有内容
【发布时间】:2015-07-05 06:57:28
【问题描述】:

Cloudfront 支持用于提供私人内容的签名 cookie,但我找不到任何有关如何执行此操作的示例。

我找到了有关如何使用 Java AWS API 而不是 Cookie 对 URL 进行签名的示例,有人可以分享他们这样做的经验吗?这是保护从 CloudFront 提供的多种媒体形式的最佳方式。

我们的网站有用户上传的图片和视频,然后可以通过在我们网站上的搜索查看,我想确保这些图片只能由我们的网站提供,不能复制以供以后使用。

【问题讨论】:

    标签: amazon-web-services amazon-cloudfront


    【解决方案1】:

    我们能够使用自定义策略引入签名 cookie 这个库

    http://www.jets3t.org/

    您需要三个由您的应用创建的 cookie,如此处所述 http://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/private-content-setting-signed-cookie-custom-policy.html

    请仔细阅读。尤其是关于如何创建策略的部分。

    这三个 cookie 是:

    1. CloudFront 策略
    2. CloudFront 签名
    3. CloudFront-Key-Pair-Id

    首先创建一个策略

    Date expirationTime = (new LocalDate()).plusYears(1).toDate();
    String customPolicy = CloudFrontService.buildPolicyForSignedUrl(basePath, expirationTime, null, null);
    
    //and assign it to a cookie
    
    Cookie signedCookiePolicy = new Cookie("CloudFront-Policy", ServiceUtils.toBase64(customPolicy.getBytes()));
    signedCookiePolicy.setMaxAge(365 * 24 * 60 * 60);
    signedCookiePolicy.setPath("/");
    response.addCookie(signedCookiePolicy);
    

    签名是棘手的部分,但一旦你使用了这个 jets3t 东西,所有工具都可用

    byte[] signatureBytes = EncryptionUtil.signWithRsaSha1(getDerPrivateKey(), customPolicy.getBytes("UTF-8"));
    String signature = ServiceUtils.toBase64(signatureBytes).replace('+', '-').replace('=', '_').replace('/', '~');
    Cookie signedCookieSignagture = new Cookie("CloudFront-Signature",cdnSignService.signBaseUrl(basePath, expirationTime));
    signedCookieSignagture.setMaxAge(365 * 24 * 60 * 60);
    signedCookieSignagture.setPath("/");
    response.addCookie(signedCookieSignagture);
    

    第三个 cookie 仅保存您的 AWS 账户的 key-id。

    Cookie signedCookieKeyPairId = new Cookie("CloudFront-Key-Pair-Id","YOUR_AWS_CF_KEY_ID");
    signedCookieKeyPairId.setMaxAge(365 * 24 * 60 * 60);
    signedCookieKeyPairId.setPath("/");
    response.addCookie(signedCookieKeyPairId);
    

    以上内容仅向您介绍了使用正确的库来创建签名 cookie 的概念。它不能单独执行或运行。

    很好,这是我的第一个溢出贡献..

    【讨论】:

      【解决方案2】:

      在 AWS JAVA 开发工具包版本 1.10.73 中,为带有自定义策略的签名 Cookie 引入了 CloudFrontCookieSigner 类。使用此类和方法,我们可以生成 cookie。

      1. CloudFront 签名
      2. CloudFront 策略
      3. CloudFront-Key-Pair-Id

        请注意,Java 仅支持 DER 格式的 SSL 证书,因此您需要将 PEM 格式的文件转换为 DER 格式。

        为此,您可以使用 openssl:

      从 .pem 生成 .der 文件的命令

      openssl pkcs8 -topk8 -nocrypt -in origin.pem -inform PEM -out new.der -outform DER 
      

      参考:-http://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/CFPrivateDistJavaDevelopment.html

      String privateKeyFilecdr = "/home/ec2-user/cookie.der";
      String distributionDomain = "xxxxxxxx.cloudfront.net";
      String s3ObjectKey = "signed-cookie.png";
      String distributionid = "X4X4X4Y5Y5"; //Cloud Front Distribution id
      String KeyFileId = "MPSIKFGHLMNSTOP" //AWS PEM KEY FILE ID
      Date expiresOn = DateUtils.parseISO8601Date("2012-11-14T22:20:00.000Z");
      
      String policyResourcePath = "https://" + distributionDomain + "/" + s3ObjectKey;
      File privateKeyFile = new File(privateKeyFilecdr);
      
      CookiesForCannedPolicy cookies = null;
      
      try {
      
          cookies = CloudFrontCookieSigner.getCookiesForCannedPolicy(policyResourcePath, KeyFileId, privateKeyFile, expiresOn);
              //  @SuppressWarnings({ "resource", "deprecation" })
                  HttpClient client = new DefaultHttpClient();
                   HttpGet httpGet = new HttpGet(SignerUtils.generateResourcePath(Protocol.https, distributionDomain, 
                           s3ObjectKey));
      
      
                  httpGet.addHeader("Cookie", cookies.getExpires().getKey() + "=" + 
                    cookies.getExpires().getValue());
                   httpGet.addHeader("Cookie", cookies.getSignature().getKey() + "=" + 
                    cookies.getSignature().getValue());
                   httpGet.addHeader("Cookie", cookies.getKeyPairId().getKey() + "=" + 
                    cookies.getKeyPairId().getValue());
                   HttpResponse responsevalues = client.execute(httpGet);
                  // System.out.println(responsevalues);
      
              } catch (InvalidKeySpecException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              }
      

      【讨论】:

        猜你喜欢
        • 2023-03-21
        • 2014-04-17
        • 2020-08-05
        • 2011-09-14
        • 2021-12-28
        • 2011-04-18
        • 2023-03-17
        • 2018-01-07
        • 2011-01-18
        相关资源
        最近更新 更多