无法读取API网关授权中的标题

时间:2020-02-10 15:32:07

标签: amazon-web-services aws-lambda aws-api-gateway

我已经为授权实现了Lambda函数,并且我在测试中遇到错误:标头为空。我可以检查是否在控制台(Cloudwatch)中打印标题。

这是我的处理程序的开始:

public class Authorizer implements RequestHandler<APIGatewayProxyRequestEvent, AuthorizerResponse> {

    public AuthorizerResponse handleRequest(APIGatewayProxyRequestEvent request, Context context) {
        Map<String, String> headers = request.getHeaders();
        System.out.println("headers: " + headers);
        String authorization = headers.get("Authorization");
...

这是结果: enter image description here

关于授权者,我是这样设置的: enter image description here

最后,我以这种方式设置了Api网关:

方法请求 enter image description here

集成请求 enter image description here

我怎么了?

谢谢。

1 个答案:

答案 0 :(得分:0)

让我在这里回答您的问题。

您正在使用令牌类型的Lambda授权器。这将传递“ Authorization”标头值作为令牌。您可以看到以下示例Lambda事件数据-

{ 
  type: 'TOKEN',
  methodArn:'arn:aws:execute-api:$AWS_REGION:$ACCOUNT_ID:$API_ID/$STAGE/$RESOURCE/',
  authorizationToken: 'allow' 
}

您可能必须相应地修改代码才能检索此令牌。

或者,您可以使用基于REQUEST的Lambda授权器功能。这会将输入作为标头,查询字符串参数,stageVariables和$ context变量的组合发送给Lambda授权者。然后,您可以使用现有代码从请求中检索标头。

关于在“方法请求”和“集成请求”中设置映射模板和“授权”标头,除非您计划将其也传递给集成Lambda函数,否则不需要它。