AWSCDKAPI网关:使用MockIntegration以实现CORS策略的子域名。
创始人
2024-09-24 01:31:04
0

在 AWS CDK 项目中使用 Python 编写代码来实现子域名的 CORS 策略。

首先,需要导入 AWS CDK 中的相关库和模块:

from aws_cdk import (
    aws_apigateway as apigateway,
    aws_lambda as lambda_,
    aws_iam as iam,
    core,
)

接着,创建自定义域名对象:

domain_name = apigateway.DomainName(
    self, "DomainName",
    domain_name=domain_name,
    certificate=certificate,
    security_policy=apigateway.SecurityPolicy.TLS_1_2
)

定义 API Gateway 的 rest API:

api = apigateway.RestApi(
    self, "APIGateway",
    rest_api_name="subdomain-cors-api",
    domain_name=domain_name,
    default_cors_preflight_options={
        "allow_origins": apigateway.Cors.ALL_ORIGINS, 
        "allow_methods": apigateway.Cors.ALL_METHODS, 
        "allow_headers": ["Content-Type", "X-Amz-Date", "Authorization", "X-Api-Key"]
    }
)

创建 Lambda 函数并添加到 API Gateway 中:

handler = lambda_.Function(
    self, "Handler",
    code=lambda_.Code.asset("lambda"),
    handler="handler.handle_request",
    runtime=lambda_.Runtime.PYTHON_3_8,
)

integration = apigateway.LambdaIntegration(
    handler=handler,
    proxy=True,
)

api.root.add_method(
    "ANY", 
    integration, 
    method_responses=[{
        "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,
        }
    }], 
    integration_responses=[{
        "statusCode": "200",
        "responseParameters": {
            "method.response.header.Access-Control-Allow-Headers": "'Content-Type,X-Amz-Date,Authorization,X-Api-Key'",
            "method.response.header.Access-Control-Allow-Methods": "'

相关内容

热门资讯

分享一款!微信卡五星辅助器,微... 分享一款!微信卡五星辅助器,微乐家乡麻将自建房辅助app(开挂自建房)app1、每一步都需要思考,不...
终于清楚!微乐自建房辅助工具插... 终于清楚!微乐自建房辅助工具插件,微乐家乡麻将自建房辅助app(开挂自建房)插件亲,关键说明,微乐家...
玩家爆料!微乐小程序自建房辅助... 玩家爆料!微乐小程序自建房辅助工具插件,微乐家乡麻将自建房辅助app(开挂自建房)app1、很好的工...
今日百科!微乐自建房辅助工具脚... 今日百科!微乐自建房辅助工具脚本,微乐家乡麻将自建房辅助app(开挂自建房)器1、微乐家乡麻将自建房...
程序员教你!微乐自建房辅助工具... 程序员教你!微乐自建房辅助工具脚本,微乐小程序免费黑科技(开挂自建房)方法1、微乐自建房辅助工具脚本...
技术分享!微乐自建房辅助工具脚... 技术分享!微乐自建房辅助工具脚本,微乐家乡麻将自建房辅助app(开挂自建房)器1、下载好微乐家乡麻将...
重大来袭!微乐小程序麻将自建房... 重大来袭!微乐小程序麻将自建房辅助工具插件,微乐家乡麻将自建房辅助app(开挂自建房)脚本运微乐小程...
玩家必看科普!微乐自建房辅助工... 玩家必看科普!微乐自建房辅助工具挂,微乐家乡麻将自建房辅助app(开挂自建房)技巧1)微乐家乡麻将自...
必看攻略!微乐家乡麻将自建房辅... 必看攻略!微乐家乡麻将自建房辅助app,微乐家乡麻将自建房辅助app(开挂自建房)脚本小薇(辅助器软...
科技新动态!微乐小程序麻将自建... 科技新动态!微乐小程序麻将自建房辅助工具插件,微乐自建房脚本免费入口(开挂自建房)方法1)微乐小程序...