CORS(교차 출처 리소스 공유)¶
확장 지원 기능: HTTPRouteCORS
이 기능은 확장 지원의 일부이며, 구현체가 HTTPRouteCORS 기능을 지원해야 한다. 지원 수준에 대한 자세한 내용은 적합성 가이드를 참고한다.
v1.5.0부터 표준 채널
HTTPRouteCORS 기능은 v1.5.0부터 표준 채널의 일부이다. 릴리스 채널에 대한
자세한 내용은 버전 관리 가이드를 참고한다.
HTTPRoute(HTTP 라우트) 리소스를 사용하여 CORS(교차 출처 리소스 공유)를 구성할 수 있다. CORS는 한 도메인에서 실행되는 웹 애플리케이션이 다른 도메인의 리소스에 대한 요청을 허용하거나 거부하는 보안 기능이다.
HTTPRouteRule의 CORS 필터를 사용하여 CORS 정책을 지정할 수 있다.
특정 출처의 요청 허용¶
다음 HTTPRoute는 https://app.example에서의 요청을 허용한다:
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
특정 출처 목록을 지정하는 대신, 단일 와일드카드("*")를 지정하여
모든 출처를 허용할 수도 있다:
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
목록에 반지정(semi-specified) 출처를 사용할 수도 있다.
와일드카드는 스킴 뒤, 호스트명의 시작 부분에 위치한다(예: https://*.bar.com):
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
자격 증명 허용¶
allowCredentials 필드는 브라우저가 CORS 요청에 자격 증명(쿠키 및 HTTP 인증 등)을
포함할 수 있는지 여부를 지정한다.
다음 규칙은 https://app.example에서 자격 증명을 포함한 요청을 허용한다:
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 필터를 사용하면 다음과 같은 기타 CORS 옵션도 지정할 수 있다:
allowMethods: CORS 요청에 허용되는 HTTP 메서드.allowHeaders: CORS 요청에 허용되는 HTTP 헤더.exposeHeaders: 클라이언트에 노출되는 HTTP 헤더.maxAge: 브라우저가 사전 요청(preflight) 응답을 캐시해야 하는 최대 시간(초 단위).
allowMethods, allowHeaders, exposeHeaders의 경우 특정 이름 목록 대신
단일 와일드카드("*")를 사용할 수도 있다.
종합 예시:
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