如何从 AWS API Gateway 自定义授权方检索 Spring Boot 中的上下文对象?

Posted

技术标签:

【中文标题】如何从 AWS API Gateway 自定义授权方检索 Spring Boot 中的上下文对象?【英文标题】:How to retrieve the context object in spring boot from AWS API Gateway custom authoriser? 【发布时间】:2021-08-24 10:11:59 【问题描述】:

我正在 AWS ECS 中运行 Spring Boot 应用程序的 Docker 映像。 API 通过 AWS API Gateway 使用 VPC Link 公开。

我有一个自定义授权者。

Authorizer 的输出如下:


"principalId": "",
"policyDocument": 
    "Version": "2012-10-17",
    "Statement": [
        "Action": "execute-api:Invoke",
        "Effect": "Allow",
        "Resource": "*"
    ]
,
"context": 
    "someKey": "1,2,3"

我想访问 Spring Boot 控制器端点中的上下文对象。如何访问它?

在 Lambda 中,我可以使用事件对象访问它。但是,我不知道如何在这里检索。

API Gateway 中路​​径/查询参数中“映射自”的值作为“event.requestContext.authorizer.context.someKey”有效吗?

【问题讨论】:

【参考方案1】:

在 API Gateway 中特定 API 的“集成请求”部分,我们必须进行映射。 我在“集成请求”的“HTTP 标头”部分创建了一个映射。

Name: userId(You can have any name)
Mapped from: context.authorizer.someKey

context.authorizer.xxxx

xxxx - 表示您希望从授权上下文对象接收的密钥。

我在相应的 Spring Boot 控制器端点中收到了这个值作为 HttpServletRequest 的一部分。

字符串用户 = httpServletRequest.getHeaders("userId");

【讨论】:

以上是关于如何从 AWS API Gateway 自定义授权方检索 Spring Boot 中的上下文对象?的主要内容,如果未能解决你的问题,请参考以下文章