Skip to content

跨域资源共享(CORS)

原文:https://gateway-api.sigs.k8s.io/guides/user-guides/http-cors/

HTTPRoute 资源 可用于配置跨域资源共享(CORS)。CORS 是一项安全特性,用于允许或拒绝一个域名下运行的 Web 应用向另一个域名的资源发起请求。

HTTPRouteRule 中的 CORS filter 可用于指定 CORS 策略。

允许来自特定来源(Origin)的请求

下面的 HTTPRoute 允许来自 https://app.example 的请求:

yaml
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
  name: cors-allow-credentials
spec:
  parentRefs:
  - name: same-namespace
  rules:
  - matches:
    - path:
        type: PathPrefix
        value: /cors-behavior-creds-false
    backendRefs:
    - name: infra-backend-v1
      port: 8080
    filters:
    - cors:
        allowOrigins:
        - https://app.example
        allowCredentials: false
      type: CORS

除了指定一组具体的来源以外,你也可以使用单个通配符"*")来允许任何来源

yaml
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
  name: cors-allow-credentials
spec:
  parentRefs:
  - name: same-namespace
  rules:
  - matches:
    - path:
        type: PathPrefix
        value: /cors-behavior-creds-false
    backendRefs:
    - name: infra-backend-v1
      port: 8080
    filters:
    - cors:
        allowOrigins:
        - "*"
        allowCredentials: false
      type: CORS

在列表中也允许使用"半具体"的来源——通配符可以出现在协议之后且主机名开头处,例如 https://*.bar.com

yaml
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
  name: cors-allow-credentials
spec:
  parentRefs:
  - name: same-namespace
  rules:
  - matches:
    - path:
        type: PathPrefix
        value: /cors-behavior-creds-false
    backendRefs:
    - name: infra-backend-v1
      port: 8080
    filters:
    - cors:
        allowOrigins:
        - https://www.baz.com
        - https://*.bar.com
        - https://*.foo.com
        allowCredentials: false
      type: CORS

允许凭证(Credentials)

allowCredentials 字段指明浏览器是否允许在 CORS 请求中携带凭证(如 cookie 和 HTTP 身份认证信息)。

下面这条规则允许来自 https://app.example携带凭证的请求:

yaml
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
  name: cors-allow-credentials
spec:
  parentRefs:
  - name: same-namespace
  rules:
  - matches:
    - path:
        type: PathPrefix
        value: /cors-behavior-creds-true
    backendRefs:
    - name: infra-backend-v1
      port: 8080
    filters:
    - cors:
        allowOrigins:
        - https://app.example
        allowCredentials: true
      type: CORS

其它 CORS 选项

CORS filter 还允许你指定其它 CORS 选项,例如:

  • allowMethods:CORS 请求允许使用的 HTTP 方法。
  • allowHeaders:CORS 请求允许出现的 HTTP Header。
  • exposeHeaders暴露给客户端的 HTTP Header。
  • maxAge:浏览器缓存预检响应的最大时长(单位:秒)。

对于 allowMethodsallowHeadersexposeHeaders,同样可以使用单个通配符"*")代替具体的名称列表。

下面是一份较为完整的示例:

yaml
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
  name: cors-allow-credentials
spec:
  parentRefs:
  - name: same-namespace
  rules:
  - matches:
    - path:
        type: PathPrefix
        value: /cors-behavior-creds-true
    backendRefs:
    - name: infra-backend-v1
      port: 8080
    filters:
    - cors:
        allowOrigins:
        - "https://www.foo.com"
        - "https://*.bar.com"
        allowMethods:
        - GET
        - OPTIONS
        allowHeaders:
        - "*"
        exposeHeaders:
        - "x-header-3"
        - "x-header-4"
        allowCredentials: true
        maxAge: 3600
      type: CORS

基于 MIT 协议发布