교차 출처 리소스 공유(CORS)는 웹페이지에서 실행되는 XMLHttpRequest(XHR) 호출이 다른 출처의 리소스와 상호작용할 수 있도록 허용하는 표준 메커니즘입니다. CORS가 없으면 모든 브라우저에서 적용되는 동일 출처 정책으로 인해 교차 출처 요청이 차단됩니다. CORS에 대한 자세한 배경 정보는 Mozilla Developer Network(MDN) 참조 문서를 확인하세요.
CORS 표준에서는 웹 클라이언트가 CORS 실행 전 OPTIONS를 발급하여 요청된 웹 서버에서 CORS를 지원하는지 확인해야 합니다. 웹 서버에서 CORS 요청을 지원하면 적절한 CORS 응답 헤더가 있는 OPTIONS 요청을 처리합니다.
백엔드 애플리케이션에서 CORS 처리
백엔드가 CORS를 지원하는 경우 ESP 또는 ESPv2가 CORS를 백엔드에 전달하도록 구성할 수 있습니다. 아래와 같이 API의 OpenAPI 사양에서 패스 스루를 사용 설정할 수 있습니다.
백엔드가 CORS를 지원하지 않는 경우 CORS 시작 플래그를 지정하여 CORS 요청을 처리하도록 ESP 또는 ESPv2를 구성할 수 있습니다. 이 접근 방식에서 CORS 실행 전 요청은 백엔드 애플리케이션에 전달되지 않습니다. 또한 ESP 또는 ESPv2는 필요할 때 구성된 CORS 헤더를 백엔드 서비스의 응답에 추가합니다.
[[["이해하기 쉬움","easyToUnderstand","thumb-up"],["문제가 해결됨","solvedMyProblem","thumb-up"],["기타","otherUp","thumb-up"]],[["이해하기 어려움","hardToUnderstand","thumb-down"],["잘못된 정보 또는 샘플 코드","incorrectInformationOrSampleCode","thumb-down"],["필요한 정보/샘플이 없음","missingTheInformationSamplesINeed","thumb-down"],["번역 문제","translationIssue","thumb-down"],["기타","otherDown","thumb-down"]],["최종 업데이트: 2025-09-04(UTC)"],[[["\u003cp\u003eCORS (Cross-origin resource sharing) allows web pages to interact with resources from different origins, bypassing the same-origin policy enforced by browsers.\u003c/p\u003e\n"],["\u003cp\u003eWeb servers supporting CORS respond to preflight \u003ccode\u003eOPTIONS\u003c/code\u003e requests with appropriate CORS headers, as required by the CORS standard.\u003c/p\u003e\n"],["\u003cp\u003eYou can configure ESP or ESPv2 to pass CORS requests to your backend if the backend supports CORS by enabling pass-through in the OpenAPI specification.\u003c/p\u003e\n"],["\u003cp\u003eIf your backend doesn't support CORS, ESP or ESPv2 can be configured to handle CORS requests using specific startup flags, preventing preflight requests from reaching the backend.\u003c/p\u003e\n"],["\u003cp\u003eWhen handling CORS via ESPv2, the preflight \u003ccode\u003eOPTIONS\u003c/code\u003e requests must include a valid \u003ccode\u003eOrigin\u003c/code\u003e and \u003ccode\u003eAccess-Control-Request-Method\u003c/code\u003e header to be handled properly.\u003c/p\u003e\n"]]],[],null,["# Enabling CORS support for Endpoints\n\nOpenAPI \\| gRPC\n\n\u003cbr /\u003e\n\nCORS (Cross-origin resource sharing) is a standard mechanism that allows XMLHttpRequest (XHR) calls executed in a web page to interact with resources from different origins. Without CORS, the [same-origin policy](https://developer.mozilla.org/docs/Web/Security/Same-origin_policy) enforced by all browsers would prevent cross-origin requests. For more background information on CORS, see the [Mozilla Developer Network (MDN) reference documentation](https://developer.mozilla.org/docs/Web/HTTP/CORS).\n\nThe CORS standard requires that web clients issue a CORS preflight `OPTIONS` to determine if the requested web server supports CORS. If a web server supports CORS request, it will handle the `OPTIONS` request with proper CORS response headers.\n\nHandle CORS in your backend application\n---------------------------------------\n\nIf your backend supports CORS, you can configure ESP or ESPv2 to pass the CORS request to your backend.\nyou can enable pass-through in the OpenAPI spec for your API as shown below: \n\n```text\nswagger: \"2.0\"\nhost: \"my-cool-api.endpoints.my-project-id.cloud.goog\"\nx-google-endpoints:\n- name: \"my-cool-api.endpoints.my-project-id.cloud.goog\"\n allowCors: True\n```\n| **Warning:** the `name` field under `x-google-endpoints` must match the `host` field value. Both fields should use the endpoint service name.\n\nHandle CORS with ESP or ESPv2\n-----------------------------\n\nIf your backend does not support CORS, you can configure ESP or ESPv2 to handle CORS requests by specifying CORS startup flags. In this approach, [CORS Preflight requests](https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS#preflighted_requests) will not pass through to your backend application. Additionally, ESP or ESPv2 will append the configured CORS headers to responses from the backend service when appropriate.\n\nThe following CORS startup options are available for ESP and ESPv2\n\n- [ESP CORS startup flags](/endpoints/docs/openapi/specify-proxy-startup-options#adding_cors_support_to_esp)\n- [ESPv2 CORS startup flags](/endpoints/docs/openapi/specify-esp-v2-startup-options#cors)\n\n\u003cbr /\u003e\n\n| **Warning:** if CORS is handled by ESPv2, the `OPTIONS` CORS preflight request must have a valid `Origin` and `Access-Control-Request-Method` headers, otherwise it will not be handled properly."]]