AWS 静态网站 + cloudfront 签名 cookie

AWS static website + cloudfront signed cookies

我有一个由 S3 存储桶 (b1) 支持的静态网站,带有 Cloudfront 网络分发。分配上的行为允许访问此存储桶。

我有另一个 S3 存储桶 (b2),其中包含私有内容,配置为仅通过云端 URL 访问(使用 S3OriginConfig 和行为进行配置)。

b1 和 b2 都配置了 publicReadAccess 阻塞和 BlockPublicAccess.BLOCK_ALL 设置。

我已经配置了一个云端 Origin Access Identity 来访问 public 和私人 s3 存储桶内容并且它有效。

例如:http://myapp.cloudfront.net/private_content.jpg 映射到 b2,因为 *.jpg 路径模式被配置为访问保存 jpg 文件的私有存储桶 b2,并且 http://myapp.cloudfront.net 将我带到为分布。

我想使用本文档中概述的签名 cookie 来防止在未经适当授权的情况下访问私人内容: https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/private-content-setting-signed-cookie-custom-policy.html

具体来说,文档中的第 2 点:

  1. You develop your application to determine whether a user should have access to your content and, if so, to send three Set-Cookie headers to the viewer. (Each Set-Cookie header can contain only one name-value pair, and a CloudFront signed cookie requires three name-value pairs.) You must send the Set-Cookie headers to the viewer before the viewer requests your private content. If you set a short expiration time on the cookie, you might also want to send three more Set-Cookie headers in response to subsequent requests, so that the user continues to have access.

也就是说,要求是只有经过身份验证的观众才能访问私人内容。

如何使用静态网站(我的是使用 HTML + JS 文件的单页应用程序)做到这一点?

其他详细信息:

问题:

  1. 确保只有经过身份验证的用户才能访问私有内容的推荐方法是什么? (欢迎提出更简单、更安全的建议)
  2. 如果采用 Set-Cookie / cloudfront signed cookies 方法,我是否应该使用路径路由(这也会简化 CORS)将 API 也带到 cloudfront 下?

经过大量挖掘和研究,我发现了这份 Amazon 文档 link,它为我要解决的问题类型提供了架构。

Kruse, O.(2019 年,8 月 16 日)。 Authorization@Edge 使用 cookie:保护您的 Amazon CloudFront 内容不被未经身份验证的用户下载。 2021 年 4 月 15 日检索自 https://aws.amazon.com/blogs/networking-and-content-delivery/authorizationedge-using-cookies-protect-your-amazon-cloudfront-content-from-being-downloaded-by-unauthenticated-users/

来自文章:

The building blocks of the sample solution For the sample solution, we use the following main building blocks:

  • A private S3 bucket to host the SPA.
  • A CloudFront distribution to serve the SPA to users.
  • A Lambda@Edge function to inspect the JSON Web Tokens (JWTs) that are included in cookies in incoming requests. The function either allows a request or redirects it to authenticate, based on whether the user is already signed in.
  • A Lambda@Edge function that sets the correct cookies when a user signs in. The user’s browser automatically sends these cookies in subsequent requests, which makes the sign-in persistent across requests.
  • A Cognito user pool with a hosted UI setup that allows users to complete sign-in.

The solution uses the standard “Authorization code with PCKE” OAuth2 grant, which is supported by Cognito.

如果您对上述文章中的代码示例感兴趣,那就是available here on Github

Amazon 的另一个博客 post 使用不同的技术在 URL.

中嵌入 JSON Web 令牌 (JWT)

Tomic, A., & Worrell, C.(2018 年,1 月 29 日)。 Authorization@Edge – 如何使用 Lambda@Edge 和 JSON Web 令牌来增强 Web 应用程序安全性。 2021 年 4 月 15 日检索自 https://aws.amazon.com/blogs/networking-and-content-delivery/authorizationedge-how-to-use-lambdaedge-and-json-web-tokens-to-enhance-web-application-security/

以上文章的代码示例为available here on Github

总而言之,您可以使用 Lambda@Edge 来授权私人内容,并使用签名的 cookie 或签名的 URLs,如上所述。