AWS CDK user pool authorizer

前端 未结 7 1413
滥情空心
滥情空心 2021-02-12 23:31

I\'m trying to create an API gateway using the AWS-CDK and protect the REST endpoints with a Cognito user pool authorizer.

I cannot find any examples how one would do th

7条回答
  •  孤独总比滥情好
    2021-02-13 00:01

    For the weirdos using the Java version of the CDK (like me), you can utilize the setters on the Cfn constructs:

    final UserPool userPool = ...
    final RestApi restApi = ...
    final LambdaIntegration integration = ...
    final Method method = restApi.getRoot().addMethod("GET", integration);
    
    final CfnAuthorizer cognitoAuthorizer = new CfnAuthorizer(this, "CfnCognitoAuthorizer",
            CfnAuthorizerProps.builder()
                    .name("CognitoAuthorizer")
                    .restApiId(restApi.getRestApiId())
                    .type("COGNITO_USER_POOLS")
                    .providerArns(Arrays.asList(userPool.getUserPoolArn()))
                    .identitySource("method.request.header.Authorization")
                    .build());
    
    final CfnMethod cfnMethod = (CfnMethod) method.getNode().getDefaultChild();
    cfnMethod.setAuthorizationType("COGNITO_USER_POOLS");
    cfnMethod.setAuthorizerId(cognitoAuthorizer.getRef());
    

提交回复
热议问题