首页 > 解决方案 > 如何限制 Cloudfront 仅访问我的域?

问题描述

我需要找到一个解决方案。基本上我有一个 .m3u8 视频,我想限制它只在我的域上播放。基本上人们现在在做什么,正在窃取我的视频并在他们的网站上播放,这会导致超载和大量带宽......

d23ek3kf.cloudfront.net/video.m3u8 > mydomain.com > 视频可访问

d23ek3kf.cloudfront.net/video.m3u8 > randomdomain.com > 视频无法访问

标签: amazon-web-servicesamazon-cloudfront

解决方案


此解决方案不会阻止任何人下载您的内容并将其上传到他们自己的站点,但它会阻止其他站点热链接到您的内容。

创建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' }],
    }
  });
};

推荐阅读