以下代码示例演示了如何向 CloudFront Functions 查看器响应事件添加 HTTP 安全标头。
- 适用于 CloudFront Functions 的 JavaScript 运行时 2.0
-
注意
查看 GitHub,了解更多信息。查找完整示例,并了解如何在 CloudFront Functions examples
存储库中进行设置和运行。 async function handler(event) { var response = event.response; var headers = response.headers; // Set HTTP security headers // Since JavaScript doesn't allow for hyphens in variable names, we use the dict["key"] notation headers['strict-transport-security'] = { value: 'max-age=63072000; includeSubdomains; preload'}; headers['content-security-policy'] = { value: "default-src 'none'; img-src 'self'; script-src 'self'; style-src 'self'; object-src 'none'; frame-ancestors 'none'"}; headers['x-content-type-options'] = { value: 'nosniff'}; headers['x-frame-options'] = {value: 'DENY'}; headers['x-xss-protection'] = {value: '1; mode=block'}; headers['referrer-policy'] = {value: 'same-origin'}; // Return the response to viewers return response; }
有关 AWS SDK 开发人员指南和代码示例的完整列表,请参阅 将 CloudFront 与 AWS SDK 配合使用。本主题还包括有关入门的信息以及有关先前的 SDK 版本的详细信息。