简体   繁体   English

API网关自定义授权者

[英]API Gateway custom authorizer

I'm new to API Gateway. 我是API Gateway的新手。 I try to use the "custom authorizer". 我尝试使用“自定义授权者”。 I followed below document and used sample code that website provided. 我遵循以下文档,并使用了网站提供的示例代码。 https://docs.aws.amazon.com/apigateway/latest/developerguide/apigateway-use-lambda-authorizer.html https://docs.aws.amazon.com/apigateway/latest/developerguide/apigateway-use-lambda-authorizer.html

The "Lambda Authorizer of the TOKEN type" is work. “令牌类型的Lambda授权者”正在工作。

curl -v -H 'x-custom-auth: xxxxx" https://xxxxx.execute-api.us-west-1.amazonaws.com/Prod/

For the "Lambda Authorizer of the REQUEST type", I can input header, queryValue1, stageValue1 and accountId for testing via aws console. 对于“ REQUEST类型的Lambda授权者”,我可以输入标题,queryValue1,stageValue1和accountId以便通过aws控制台进行测试。

But... 但...

I'm confused about the "request type" and did not know how to pass the queryValue1, stageValue1 and accountId to API Gateway. 我对“请求类型”感到困惑,并且不知道如何将queryValue1,stageValue1和accountId传递给API Gateway。

Can anyone help me to figure it out? 谁能帮我弄清楚吗?

Regardless of which type of Authorizer you use, API Gateway will receive the same headers and parameters that you originally sent. 无论您使用哪种类型的授权器,API Gateway都会收到与您最初发送的相同的标头和参数。

Your Authorizer cannot modify the original request details (but it include an auth context which API Gateway can also read). 您的授权者无法修改原始请求的详细信息(但其中包含API网关也可以读取的身份验证context )。

In the example you're referencing: 在示例中,您引用的是:

if (headers.HeaderAuth1 === "headerValue1"
    && queryStringParameters.QueryString1 === "queryValue1"
    && stageVariables.StageVar1 === "stageValue1"
    && requestContext.accountId === "123456789012") {
    callback(null, generateAllow('me', event.methodArn));
}  else {
    callback("Unauthorized");
}

What they're saying is that the REQUEST authorizer is expecting specific values in the request object: 他们的意思是, REQUEST授权者期望请求对象中的特定值:

  • If all the values match, the authorizer will Allow the request to continue. 如果所有值都匹配,则授权者将Allow请求继续。 API Gateway will receive the same request object (with all the same parameters). API网关将接收相同的请求对象(具有所有相同的参数)。

  • If not all the values match, the authorizer will Deny the request returning 403 Unauthorized ; 如果不是所有值都匹配,则授权者将Deny请求,返回403 Unauthorized API Gateway will not receive the request. API网关将不会收到该请求。

Each of the properties in the example are sourced in the following ways: 示例中的每个属性都通过以下方式获得:

  • AccountId is set automatically by AWS AWS会自动设置AccountId
  • StageVar1 comes from the deployed API's stage settings (API Name > Stages > Stage Name > Stage Variables) StageVar1来自已部署的API的阶段设置(API名称>阶段>阶段名称>阶段变量)
  • HeaderAuth and QueryString1 are sent by the HTTP client (eg curl ) HeaderAuthQueryString1由HTTP客户端发送(例如curl

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM