我在为我用 Node.js 编写的 AWS lambda 函数设置基本身份验证时遇到问题。
问题:
AWS lambda 函数是附加服务的代理。这个函数只转发整个请求并给用户整个响应。这就是为什么我需要强制使用Authentication标头,我希望有传递凭据的提示窗口:https : //developer.mozilla.org/en-US/docs/Web/HTTP/Authentication
除了我的 lambda 函数的代理部分之外,我还专注于身份验证问题,并编写了以下代码:
export const proxy = async (event) => {
const authorizationHeader = event.headers.Authorization;
if (typeof authorizationHeader === undefined) {
throw new Error("Unauthorized");
}
...
};
Run Code Online (Sandbox Code Playgroud)
service:
name: proxy-auth-test
plugins:
- serverless-webpack
provider:
name: aws
runtime: nodejs8.10
memorySize: 128
timeout: 10
functions:
proxy-async:
handler: handler.proxy
events:
- http:
method: get
path: api/proxy
resources:
Resources:
GatewayResponse:
Type: 'AWS::ApiGateway::GatewayResponse'
Properties:
ResponseParameters:
gatewayresponse.header.WWW-Authenticate: "'Basic'"
ResponseType: UNAUTHORIZED
RestApiId: …Run Code Online (Sandbox Code Playgroud)