使用 API 管理 API 安全

Last published : Oct 06, 2026
您可以访问 API 来创建、配置和部署 API 安全。
注意:
要了解如何使用 API 安全 API 配置此功能,请参阅 Nitro API 文档。
步骤 资源 URL
1 创建 API 定义 https://adm.cloud.com/{customerid}/apisec/nitro/v1/config/apidefs
2 添加 API 代理 https://adm.cloud.com/apiproxies
3 使用 API 代理部署 API 实例 https://adm.cloud.com/apiproxies/{customerid}/deployments
4 添加 API 策略 https://adm.cloud.com/{customerid}/apisec/nitro/v1/config/policies/{id}
每个 API 策略都有一个不同的 config_spec 对象。它是一个不透明对象,包含一个 JSON 字典,用于配置具有特定值的 policytype。
在此对象中,您可以使用以下选项选择 API 资源及其方法:
  • api-resource-paths - 指定在 API 定义中定义的 API 资源路径和方法。
    示例:
    {
    "endpoints": ["/pet", "/pet/findByStatus", "/pet/findByTags"],
       "get": true,
       "post": false,
       "put": false,
       "delete": false
    }
  • custom-rules - 指定可能不存在于 API 定义中的自定义 API 资源路径和方法。
    示例:
    {
    "endpoints": ["/pet/categories", "/pet/findByName"],
       "get": true,
       "post": false,
       "put": false,
       "delete": false
    }
通过此配置,策略会筛选与指定 API 资源路径匹配的传入流量请求。
有关每种策略类型的 config_spec 信息,请参阅 API 策略类型示例。

API 策略类型示例

本节介绍支持的 API 策略类型及其配置:

速率限制

以下是 Ratelimit 策略类型的配置示例。在 config_spec 对象中指定以下配置:
{
    "policytype": "Ratelimit",
    "config_spec": {
        "api-resource-paths": {
            "endpoints": ["/pet", "/pet/findByStatus", "/pet/findByTags"],
            "get": true,
            "post": false,
            "put": false,
            "delete": false
        },
        "custom-rules": {},
        "threshold": "10",
        "timeslice": "20000",
        "limittype": "BURSTY",
        "api-respondertype": "DROP",
        "header_name": "x-api-key",
        "per_client_ip": true
    },
    "order_index": 1,
    "policy_name": "ratelimit_policy"
}
有关每个属性的更多信息,请参阅 速率限制策略。

开放授权

以下是 JWT Auth validation 策略类型的 API 配置示例。在 config_spec 对象中指定以下配置:
{
    "policytype": "JWT Auth Validation",
    "config_spec": {
        "api-resource-paths": {
            "endpoints": ["/pet", "/pet/findByStatus", "/pet/findByTags"],
            "get": true,
            "post": true,
            "put": false,
            "delete": false
        },
        "custom-rules": {},
        "jwks-uri": "https://uri.petstore.com",
        "issuer": "https://issuer.petstore.com",
        "audience": "petstore",
        "introspect-uri": "https://introspect.uri.com",
        "clientid": "client",
        "clientsecret": "clientsecret",
        "claims-to-save": ["scope", "scope2"],
        "allowed-algorithms": {
            "hs256": true,
            "rs256": true,
            "rs512": true
        }
    },
    "order_index": 2,
    "policy_name": "Jwt_auth_policy"
}
有关每个属性的更多信息,请参阅 OAuth 策略

基本身份验证

以下是 BasicAuth 策略类型的 API 配置示例:
{
    "config_spec": {
        "api-resource-paths": {
            "delete": false,
            "endpoints": ["/pet", "/pet/findByStatus", "/pet/findByTags"],
            "get": true,
            "post": true,
            "put": false
        },
        "custom-rules": {}
    },
    "order_index": 3,
    "policy_name": "Auth_BaSIC",
    "policytype": "BasicAuth"
}
有关每个属性的更多信息,请参阅基本身份验证策略。

无身份验证

以下是 NoAuth 策略类型的 API 配置示例:
{
    "config_spec": {
        "api-resource-paths": {
            "delete": false,
            "endpoints": ["/pet", "/pet/findByStatus", "/pet/findByTags"],
            "get": true,
            "post": false,
            "put": false
        },
        "custom-rules": {}
    },
    "order_index": 4,
    "policy_name": "no_auth_policy",
    "policytype": "NoAuth"
}

Bot

以下是 Bot 策略类型的 API 配置示例:
{
    "config_spec": {
        "api-resource-paths": {
            "delete": false,
            "endpoints": ["/pet", "/pet/findByStatus", "/pet/findByTags"],
            "get": false,
            "post": false,
            "put": false
        },
        "bot-prof-name": "apisec_test_profile",
        "custom-rules": {}
    },
    "order_index": 5,
    "policy_name": "bot_policy",
    "policytype": "Bot"
}
有关每个属性的更多信息,请参阅Bot 策略。

WAF

以下是 WAF 策略类型的 API 配置示例:
{
    "config_spec": {
        "api-resource-paths": {
            "delete": false,
            "endpoints": ["/pet", "/pet/findByStatus", "/pet/findByTags"],
            "get": false,
            "post": false,
            "put": false
        },
        "waf-prof-name": "apisec_waf_profile",
        "custom-rules": {}
    },
    "order_index": 6,
    "policy_name": "waf_policy",
    "policytype": "WAF"
}
有关每个属性的更多信息,请参阅WAF 策略。

标头重写

以下是标头重写策略类型的 API 配置示例,请在 config_spec 对象中指定此配置:
{
    "policytype": "Header Rewrite",
    "config_spec": {
        "api-resource-paths": {
            "endpoints": ["/pet", "/pet/findByStatus", "/pet/findByTags"],
            "get": true,
            "post": true,
            "put": false,
            "delete": false
        },
        "custom-rules": {},
        "rewrite-policy-header-field-name": "org",
        "rewrite-policy-header-field-val": "Citrix",
        "rewrite-policy-header-field-new-val": "Citrite"
    },
    "order_index": 7,
    "policy_name": "header_rewrite_pol"
}
有关每个属性的更多信息,请参阅标头重写策略。

URI 路径重写

以下是 URI 路径重写策略类型的 API 配置示例:
{
    "config_spec": {
        "api-resource-paths": {
            "endpoints": ["/store/order", "/store/inventory"],
            "delete": false,
            "get": true,
            "post": true,
            "patch": false,
            "put": false
        },
        "custom-rules": {
            "delete": false,
            "endpoints": [],
            "get": false,
            "post": false,
            "patch": false,
            "put": true
        },
        "path-rewrite-params": [
        {
            "insert-segment-position": "beginning",
            "new-path-value": "v3",
            "old-path-value": "v2",
            "action-type": "replace path segment"
        },
        {
            "insert-segment-position": "beginning",
            "new-path-value": "begin",
            "action-type": "insert path segment"
        },
        {
            "insert-segment-position": "end",
            "new-path-value": "end",
            "action-type": "insert path segment"
        },
        {
            "insert-segment-position": "before",
            "new-path-value": "before",
            "old-path-value": "store",
            "action-type": "insert path segment"
        },
        {
            "insert-segment-position": "after",
            "new-path-value": "after",
            "old-path-value": "store",
            "action-type": "insert path segment"
        }
        ]
    },
        "order_index": 24,
        "policy_name": "eats_uripathrewrite",
        "policytype": "URI Path Rewrite”
}
有关每个属性的更多信息,请参阅 URI 路径重写策略。

授权

以下是 Authorization 策略类型的 API 配置示例。在 config_spec 对象中指定以下配置:
{
    "policytype": "Authorization",
    "config_spec": {
        "api-resource-paths": {
            "endpoints": ["/pet", "/pet/findByStatus", "/pet/findByTags"],
            "get": true,
            "post": true,
            "put": false,
            "delete": false
        },
        "custom-rules": {},
        "claims": [{
            "name": "scope",
            "values": ["value1", "value2"]
        }]
    },
    "order_index": 8,
    "policy_name": "authorization"
}
有关每个属性的更多信息,请参阅 授权策略。

拒绝

以下是 Deny 策略类型的 API 配置示例。在 config_spec 对象中指定以下配置:
{
    "policytype": "Deny",
    "config_spec": {
        "api-resource-paths": {
            "endpoints": ["/pet", "/pet/findByStatus", "/pet/findByTags"],
            "get": true,
            "post": true,
            "put": false,
            "delete": false
        },
        "custom-rules": {},
        "api-denytype": "RESPONDWITH"
    },
    "order_index": 9,
    "policy_name": "deny_policy"
}
在 api-denytype 中,您可以指定以下值之一:
  • RESPONDWITH
  • RESET
有关每个属性的更多信息,请参阅 拒绝规则。