我在我的ApiGateway中使用Lambda代理和Cognito用戶池授權器。在Lambda函數中,我可以通過事件對象訪問路徑等變量。除此之外,我想訪問經過身份驗證的用戶的聲明。在文件上寫着,我應該使用:AWS ApiGateway Lambda代理訪問授權者
context.authorizer.claims.property
但我授權爲空,所以我得到
Cannot read property 'claims' of undefined
任何人只要有一個想法?
我在我的ApiGateway中使用Lambda代理和Cognito用戶池授權器。在Lambda函數中,我可以通過事件對象訪問路徑等變量。除此之外,我想訪問經過身份驗證的用戶的聲明。在文件上寫着,我應該使用:AWS ApiGateway Lambda代理訪問授權者
context.authorizer.claims.property
但我授權爲空,所以我得到
Cannot read property 'claims' of undefined
任何人只要有一個想法?
如果您指的是this part of the documentation,$context.authorizer.claims
是集成映射模板的一部分。它與處理程序的參數context
無關。使用Lambda Proxy integration
,您正在使用passthrough mapping template
。 (請參閱編輯)。請參見編輯。您可能會需要禁用Lambda Proxy integration
和使用類似這樣的映射模板:
{
"identity" : {
"sub" : "$context.authorizer.claims.sub",
"email" : "$context.authorizer.claims.email"
}
}
映射模板「打造」拉姆達的event
參數。因此,您可以通過參數event
訪問您的索賠部分。
exports.handler = (event, context, callback) => {
// TODO implement
callback(null, event.identity.email);
};
注意,我稍微修改了文檔和示例,以避免什麼context
可以是另一個困惑:
event
參數的一個關鍵在文檔的某些示例中< =我給它改名identity
編輯
正如doorstuck,the information is available using the proxy integration
接受的答案會工作,但它並不需要指出。當使用lambda代理集成您可以訪問授權的權利要求:
event.requestContext.authorizer.claims
你可以嘗試console.log(event);
,看看你得到了一個lambda代理整合CloudWatch的日誌信息。
不錯,當* not *使用Lambda代理集成時,「Method Requet passthrough」不包含它。我認爲會使用相同的映射模板。 –
感謝您的詳細解答。當我禁用代理集成時,我將無法訪問像路徑,查詢字符串等屬性? – SnowMax
您也可以通過映射模板來完成。我會建議選擇「Method Requet passthrough」模板並用你需要的模板來完成它。 –
作品!謝謝;) – SnowMax