简体   繁体   English

如何使用来自云前端分发的签名 cookie 访问受限媒体文件,该分发使用与应用程序域不同的域?

[英]How to access restricted media files using signed cookies from cloud front distribution which uses a different domain from the application domain?

Can we assign cookies from the application domain to a different domain.我们可以将应用程序域中的 cookie 分配给不同的域吗? For example we need to access the restricted media files which use the d1fzlamzw9yswb.cloudfront.net distribution, but if we run the application in localhost or in any other domain it does not allow us to assign the cookies to d1fzlamzw9yswb.cloudfront.net programmatically.例如,我们需要访问使用 d1fzlamzw9yswb.cloudfront.net 分发版的受限媒体文件,但是如果我们在 localhost 或任何其他域中运行应用程序,则不允许我们以编程方式将 cookie 分配给 d1fzlamzw9yswb.cloudfront.net。 It normally sets the cookies to the application domain.它通常将 cookie 设置为应用程序域。

Somehow by manually assigning the cookies from the browser to d1fzlamzw9yswb.cloudfront.net we were able to access the content without any issue.不知何故,通过将浏览器中的 cookie 手动分配给 d1fzlamzw9yswb.cloudfront.net,我们能够毫无问题地访问内容。

So if the application runs in another domain (localhost or any other) how can we assign the cookies to a different domain where we need to access the restricted media files?因此,如果应用程序在另一个域(本地主机或任​​何其他域)中运行,我们如何将 cookie 分配给我们需要访问受限媒体文件的不同域? What is the best way to do this?做这个的最好方式是什么?

Setting cookies for different domains is obviously not allowed.显然不允许为不同的域设置 cookie。

However, one can use an apex domain in a signed cookie, for example, .example.com .但是,可以在签名 cookie 中使用顶级域,例如.example.com If properly set this cookie will always be sent to any request made to example.com, but also to any of its subdomains, eg, cloudfront.example.com .如果设置正确,此 cookie 将始终发送到向 example.com 发出的任何请求,但也会发送到其cloudfront.example.com域,例如cloudfront.example.com So we can take advantage of this and have our backend service, which is running on backend.example.com create and set a signed cookie for .example.com .因此,我们可以利用这一点,让我们在backend.example.com上运行的后端服务为.example.com创建并设置签名 cookie。 Later when the frontend requests a file form cloudfront.example.com it will automatically include the signed cookie with the request.稍后,当前端请求一个文件格式cloudfront.example.com它会自动在请求中包含签名的 cookie。

This approach requires having a custom domain for a Cloud Front distribution.此方法需要为 Cloud Front 分发提供自定义域。 Here docs how to do that这里文档如何做到这一点

Best, Stefan最好的,斯蒂芬

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

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