gpt4 book ai didi

javascript - AWS CDK 用户池授权者

转载 作者:行者123 更新时间:2023-12-02 16:02:16 24 4
gpt4 key购买 nike

我正在尝试使用 AWS-CDK 创建 API 网关,并使用 Cognito 用户池授权方保护 REST 端点。

我找不到任何示例如何做到这一点。我认为它应该看起来像这样,但也许我需要的方法不存在?

const cdk       = require('@aws-cdk/cdk');
const lambda = require('@aws-cdk/aws-lambda');
const apigw = require('@aws-cdk/aws-apigateway');

const path = require('path');

//
// Define the stack:
class MyStack extends cdk.Stack {
constructor (parent, id, props) {
super(parent, id, props);

var tmethodHandler = new lambda.Function(this, 'test-lambda', {
runtime: lambda.Runtime.NodeJS810,
handler: 'index.handler',
code: lambda.Code.directory( path.join( __dirname, 'lambda')),
});

var api = new apigw.RestApi(this, 'test-api');

const tmethod = api.root.addResource('testmethod');

const tmethodIntegration = new apigw.LambdaIntegration(tmethodHandler);

tmethod.addMethod('GET', getSessionIntegration, {
authorizationType: apigw.AuthorizationType.Cognito,
authorizerId : 'crap!!!?'
});

}
}

class MyApp extends cdk.App {
constructor (argv) {
super(argv);

new MyStack(this, 'test-apigw');
}
}

console.log(new MyApp(process.argv).run());

最佳答案

截至 2019 年 9 月,@bgdnip 答案并未完全翻译为 typescript。我让它与以下内容一起工作:

const api = new RestApi(this, 'RestAPI', {
restApiName: 'Rest-Name',
description: 'API for journey services.',
});

const putIntegration = new LambdaIntegration(handler);

const auth = new CfnAuthorizer(this, 'APIGatewayAuthorizer', {
name: 'customer-authorizer',
identitySource: 'method.request.header.Authorization',
providerArns: [providerArn.valueAsString],
restApiId: api.restApiId,
type: AuthorizationType.COGNITO,
});

const post = api.root.addMethod('PUT', putIntegration, { authorizationType: AuthorizationType.COGNITO });
const postMethod = post.node.defaultChild as CfnMethod;
postMethod.addOverride('Properties.AuthorizerId', { Ref: auth.logicalId });

这是来自https://docs.aws.amazon.com/cdk/latest/guide/cfn_layer.html#cfn_layer_resource_props

十月更新

上述内容已经过时且不必要,可以通过 aws-cdk 1.12.0 实现以下内容

const api = new RestApi(this, 'RestAPI', {
restApiName: 'Rest-Name',
description: 'API for journey services.',
});

const putIntegration = new LambdaIntegration(handler);

const auth = new CfnAuthorizer(this, 'APIGatewayAuthorizer', {
name: 'customer-authorizer',
identitySource: 'method.request.header.Authorization',
providerArns: [providerArn.valueAsString],
restApiId: api.restApiId,
type: AuthorizationType.COGNITO,
});

const post = api.root.addMethod('PUT', putIntegration, {
authorizationType: AuthorizationType.COGNITO,
authorizer: { authorizerId: auth.ref }
});

关于javascript - AWS CDK 用户池授权者,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52726914/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com