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": "'

相关内容

热门资讯

分享实测!微乐自建房辅助工具,... 分享实测!微乐自建房辅助工具,微乐小程序免费黑科技(开挂自建房)软件1、首先打开微乐自建房辅助工具辅...
推荐一款!微乐微信自建房辅助工... 推荐一款!微乐微信自建房辅助工具插件,微乐小程序免费黑科技(开挂自建房)插件1、任何微乐微信自建房辅...
实测揭晓!微乐自建房辅助工具插... 实测揭晓!微乐自建房辅助工具插件,微乐家乡麻将自建房辅助app(开挂自建房)攻略;所有人都在同一条线...
盘点一款!微乐小程序麻将自建房... 盘点一款!微乐小程序麻将自建房辅助工具插件,微乐家乡麻将自建房辅助app(开挂自建房)教程1、微乐小...
推荐十款!微乐自建房辅助工具脚... 推荐十款!微乐自建房辅助工具脚本,微乐小程序免费黑科技(开挂自建房)教程1、微乐自建房辅助工具脚本辅...
盘点几款!微乐自建房辅助工具免... 盘点几款!微乐自建房辅助工具免费app,微乐自建房脚本免费入口(开挂自建房)app1.微乐自建房辅助...
最新技巧!微信卡五星辅助器,微... 最新技巧!微信卡五星辅助器,微乐自建房脚本免费入口(开挂自建房)方法微信卡五星辅助器透视方法中分为三...
实操分享!微乐自建房辅助工具免... 实操分享!微乐自建房辅助工具免费app,微乐小程序免费黑科技(开挂自建房)软件1、进入游戏-大厅左侧...
技术分享!微信卡五星辅助器,微... 技术分享!微信卡五星辅助器,微乐自建房脚本免费入口(开挂自建房)工具1)微信卡五星辅助器有没有挂:进...
盘点十款!微乐自建房辅助工具,... 盘点十款!微乐自建房辅助工具,微乐家乡麻将自建房辅助app(开挂自建房)软件1、任何微乐家乡麻将自建...