小编Ama*_*iss的帖子

AWS Lambda@edge 将 cookie 设置为原始响应

我的目标是保护 aws s3 存储桶链接,我试图通过使用 cloudfront 作为可访问 s3 存储桶的链接来解决这个问题,因此,当用户尝试访问 cloudfront 链接时,如果有的话,就会有一个基本的身份验证他们的浏览器中没有 cookie,但如果有 cookie,则会检查该 cookie 中的身份验证值并授予用户访问权限。PS:这不是一个网站,我的任务是保护 s3 存储桶链接。

这是我的尝试,使用 lambda@edge,根据查看者的请求,如果用户未登录,则有身份验证页面,否则,他们被允许访问,它可以工作,但我无法设置 cookie,因为在 aws 文档、cloudfront 中的某处删除标头文件中的 set-cookie:CloudFront 从转发到您的源的请求中删除 Cookie 标头,并从返回给您的查看者的响应中删除 Set-Cookie 标头

这是我的代码:

'use strict';

// returns a response error
const responseError = {
                status: '401',
                statusDescription: 'Unauthorized',
                headers: {
                    'www-authenticate': [{key: 'WWW-Authenticate', value:'Basic'}]
                }
};




exports.handler = (event, context, callback) => {
    // Get request and request headers
    console.log(event.Records[0]);
    const request = event.Records[0].cf.request;
    const response = event.Records[0].cf.response;
    const headers = request.headers;



    // …
Run Code Online (Sandbox Code Playgroud)

cookies amazon-s3 amazon-web-services amazon-cloudfront aws-lambda-edge

5
推荐指数
0
解决办法
2530
查看次数