微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

将授权人和 cors 添加到 CloudFormation json

如何解决将授权人和 cors 添加到 CloudFormation json

我有这个资源:

"MyUserAuthorizer": {
      "Type": "AWS::Serverless::Function","Properties": {
        "Handler": "MyProject::MyProject.Functions::UserAuthorizer","Runtime": "dotnetcore3.1","CodeUri": "","Description": "authorizer","MemorySize": 256,"Timeout": 30,"Role": null,"Policies": [
          "AWSLambdaFullAccess"
        ]
      }
    }

我想添加一个 AWS::ApiGateway::Authorizer 资源,该资源将使用此 MyUserAuthorizer lambda。 我试过这个:

"Auth": {
  "Type" : "AWS::ApiGateway::Authorizer","Properties" : {
      "AuthorizerCredentials" : null,"AuthorizerResultTtlInSeconds" : 300,"IdentitySource": "method.request.header.Authorization","Name" : "Auth","Type" : "TOKEN"
    }
  }

,如何将其连接到 lambda 授权器函数? 我在 json 中只有其他 lambda 函数。我应该有 api 网关定义吗? 如何为所有 lambda 函数添​​加 CORS 支持

解决方法

将您的函数与授权方连接:

            MYRestApiAuthorizer:
            Type: AWS::ApiGateway::Authorizer
            Properties:
                RestApiId: !Ref HelloCORSRestApi
                Name: "MyAuthorizer"
                Type: TOKEN
                IdentitySource: "method.request.header.Authorization"
                AuthorizerUri: !GetAtt MyUserAuthorizer.Arn

启用 CORS:

    .....
    
    HelloCORSRestApiResource:
        Type: 'AWS::ApiGateway::Resource'
        Properties:
        RestApiId: !Ref HelloCORSRestApi
        ParentId: !GetAtt 
            - HelloCORSRestApi
            - RootResourceId
        PathPart: hello


    HelloCORSRestApiRequestGET:
        Type: 'AWS::ApiGateway::Method'
        Properties:
        AuthorizationType: CUSTOM
        AuthorizerId: !MYRestApiAuthorizer
        HttpMethod: GET
        Integration:
            Type: AWS
            IntegrationHttpMethod: POST
            Uri: ....
            IntegrationResponses:
            - StatusCode: 200
                ResponseParameters:
                    method.response.header.Access-Control-Allow-Headers: "'Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token'"
                    method.response.header.Access-Control-Allow-Methods: "'GET,POST,PUT,DELETE,OPTIONS'"
                    method.response.header.Access-Control-Allow-Origin: "'*'"          

        ResourceId: !Ref HelloCORSRestApiResource
        RestApiId: !Ref HelloCORSRestApi
        MethodResponses:
            - StatusCode: 200
            ResponseParameters:
                method.response.header.Access-Control-Allow-Headers: true
                method.response.header.Access-Control-Allow-Methods: true
                method.response.header.Access-Control-Allow-Origin: true 

.....

    OptionsMethod:
        Type: AWS::ApiGateway::Method
        Properties:
            AuthorizationType: NONE
            RestApiId: !Ref HelloCORSRestApi
            ResourceId: !Ref: HelloCORSRestApiResource
            HttpMethod: OPTIONS
            Integration:
            IntegrationResponses:
            - StatusCode: 200
                ResponseParameters:
                method.response.header.Access-Control-Allow-Headers: "'Content-Type,X-Amz-Security-Token'"
                method.response.header.Access-Control-Allow-Methods: "'<UPDATE_HTTP_METHODS_HERE>'"
                method.response.header.Access-Control-Allow-Origin: "'*'"
                ResponseTemplates:
                application/json: ''
            PassthroughBehavior: when_no_match
            RequestTemplates:
                application/json: '{"statusCode": 200}'
            Type: MOCK
            MethodResponses:
            - StatusCode: 200
            ResponseModels:
                application/json: 'Empty'
            ResponseParameters:
                method.response.header.Access-Control-Allow-Headers: false
                method.response.header.Access-Control-Allow-Methods: false
                method.response.header.Access-Control-Allow-Origin: false

如果您想查看here

,还有更多内容

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。