amazon-web-services - 如何限制 Cloudfront 仅访问我的域?
问题描述
我需要找到一个解决方案。基本上我有一个 .m3u8 视频,我想限制它只在我的域上播放。基本上人们现在在做什么,正在窃取我的视频并在他们的网站上播放,这会导致超载和大量带宽......
d23ek3kf.cloudfront.net/video.m3u8 > mydomain.com > 视频可访问
d23ek3kf.cloudfront.net/video.m3u8 > randomdomain.com > 视频无法访问
解决方案
此解决方案不会阻止任何人下载您的内容并将其上传到他们自己的站点,但它会阻止其他站点热链接到您的内容。
创建Lambda@Edge查看器请求触发器。这允许您在检查缓存之前检查请求,并允许继续处理或返回生成的响应。
'use strict';
exports.handler = (event, context, callback) => {
// extract the request object
const request = event.Records[0].cf.request;
// extract the HTTP `Referer` header if present
// otherwise an empty string to simplify the matching logic
const referer = (request.headers['referer'] || [ { value: '' } ])[0].value;
// verify that the referring page is yours
// replace example.com with your domain
// add other conditions with logical or ||
if(referer.startsWith('https://example.com/') ||
referer.startsWith('http://example.com/'))
{
// return control to CloudFront and allow the request to continue normally
return callback(null,request);
}
// if we get here, the referring page is not yours.
// generate a 403 Forbidden response
// you can customize the body, but the size is limited to ~40 KB
return callback(null, {
status: '403',
body: 'Access denied.',
headers: {
'cache-control': [{ key: 'Cache-Control', value: 'private, no-cache, no-store, max-age=0' }],
'content-type': [{ key: 'Content-Type', value: 'text/plain' }],
}
});
};
推荐阅读
- jmespath - 使用 JMESPath 进行不区分大小写的搜索
- python - 如何在 Pandas 数据框中创建一个包含与 groupby 相同值的新列?
- spring-batch - 如何将 Spring Batch(无 Spring Boot)与 Spring Cloud Data Flow Server 集成
- javascript - 为什么对象属性返回“未定义”?
- python - 读取 CSV 文件并提取特定数据
- java - 无法在 Java Web 项目中运行 Java 文件
- python - Java 中的 try catch 结构与 Python 中的 try except 相比如何?
- javascript - console.log 没有正确显示 React 状态
- python - Boto3 wait_until_exists 可用图像(对象没有属性)
- svg - 旋转的矩形与文本 bbox 不匹配