首页 > 解决方案 > 云端点是否可以仅使用基本 URL 来保护所有 API 访问?

问题描述

假设我的 api 位于 domain/_ah/api。我们有 domain/_ah/api/getUser、domain/_ah/api/stuff/getStuff、domain/_ah/api/stuff/moreStuff/postMoreStuff。

是否可以通过仅定义这样的内容来做到这一点?

  swagger: '2.0'
  info:
    title: "Cloud Endpoints + Cloud Run"
    description: "Sample API on Cloud Endpoints with a Cloud Run backend"
    version: "1.0.0"
  host: "domain"
  schemes:
    - "https"
  produces:
    - "application/json"
  x-google-backend:
    jwt_audience: "audience"
    address: "domain_backend"
    protocol: "h2"
  paths:
    /_ah/api/*:
      get, post, put, etc:
        description: "Protects Base URL"
        operationId: "authInfoFirebase"
        security: 
          - firebase: []

  securityDefinitions:
    firebase:
      authorizationUrl: ""
      flow: "implicit"
      type: "oauth2"
      x-google-issuer: "https://securetoken.google.com/<project_id>"
      x-google-jwks_uri: "https://www.googleapis.com/service_accounts/v1/metadata/x509/securetoken@system.gserviceaccount.com"
      x-google-audiences: "<project_id>"

标签: google-cloud-endpointsopenapigoogle-cloud-endpoints-v2

解决方案


恐怕 Cloud Endpoints 无法识别您指定的通配符。

引用文档:

“Endpoints 仅支持对应于整个路径段(由斜杠 / 分隔)的 URL 路径模板参数。不支持对应于部分路径段的 URL 路径模板参数。”[1]

通配符的一种解决方法是使用路径模板。您可以使用花括号 {} 将 URL 的一部分标记为路径参数,使用您的示例:

域/_ah/api/{value1}

域/_ah/api/{value1}/{value2}

域/_ah/api/{value1}/{value2}/{value3}

请注意不要重叠路径模板,如下例所示:

/items/{itemid} ---> 这是有效的

/items/{itemId}/subitem ----> 这是有效的

/items/cat ----> 这是无效的

[1] https://cloud.google.com/endpoints/docs/openapi/openapi-limitations#url_path_templating


推荐阅读