0Pricing
OAuth2 & OpenID Connect Deep Dive · 강의

교차 출처 리소스 공유(CORS)

보호된 리소스에 접근하는 단일 페이지 애플리케이션을 중심으로 OAuth2 및 OIDC 환경에서 CORS 정책을 이해해 보세요.

교차 출처 리소스 공유(CORS)은(는) CoddyKit의 무료 OAuth2 & OpenID Connect Deep Dive 강의입니다. 이것은 4개 중 2번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 AI 튜터와 함께 브라우저에서 직접 실습할 수 있습니다. 이 강의는 OAuth2 & OpenID Connect Deep Dive 학습 경로의 일부이며, 진행 상황이 웹과 CoddyKit 앱에 동기화됩니다. OAuth2 & OpenID Connect Deep Dive 강의에는 총 4개의 강의가 포함되어 있습니다.

이 강의의 일부는 아직 번역되지 않았으며 영어로 표시됩니다.

CORS & Secure Web Apps

Welcome! Today we'll explore Cross-Origin Resource Sharing (CORS), a crucial security feature for modern web applications.

CORS allows web browsers to securely handle requests between a client-side application (like a Single-Page Application, SPA) and an API server when they're on different domains.

This is especially vital when your SPA uses OAuth2 or OpenID Connect to access protected resources.

The Same-Origin Policy

To understand CORS, we first need to know about the Same-Origin Policy (SOP).

  • SOP is a fundamental browser security feature.
  • It prevents web pages from making requests to a different domain than the one that served the page.
  • For example, a script from app.example.com cannot directly make an AJAX request to api.anothersite.com.

This protects users from malicious scripts trying to steal data from other sites you're logged into.

Why CORS is Needed

While SOP is great for security, it creates a problem for modern web architectures.

Many applications, especially SPAs, need to fetch data from APIs hosted on a different domain or port. For instance:

  • Your SPA is at https://my-app.com
  • Your API is at https://api.my-app.com
  • Your OAuth2 Authorization Server is at https://auth.my-app.com

CORS provides a controlled way to relax the SOP, allowing these cross-origin requests while maintaining security.

Simple CORS Requests

Some cross-origin requests are considered 'simple' by browsers. These don't trigger a special preflight check.

A request is simple if it meets all these conditions:

  • Method is GET, HEAD, or POST.
  • Only specific allowed headers (e.g., Accept, Accept-Language, Content-Language, Content-Type).
  • Content-Type header is limited to application/x-www-form-urlencoded, multipart/form-data, or text/plain.

If simple, the browser sends the request directly, and the server includes CORS headers in its response.

Preflight for Complex Requests

Most requests in modern SPAs, especially those involving OAuth2 tokens, are not simple.

A 'complex' request requires a browser to send a 'preflight' OPTIONS request before the actual request:

  • Methods: PUT, DELETE, or custom methods.
  • Headers: Custom headers (like Authorization for access tokens).
  • Content-Type: application/json (common for APIs).

The server must respond to this OPTIONS request with appropriate CORS headers, indicating if the actual request is allowed.

Key CORS Response Headers

The server communicates its CORS policy through specific HTTP response headers:

  • Access-Control-Allow-Origin: Specifies which origins are allowed to access the resource. Can be * (wildcard, generally discouraged for security) or a specific origin like https://my-app.com.
  • Access-Control-Allow-Methods: Lists the HTTP methods (e.g., GET, POST, PUT, DELETE) allowed for the resource.
  • Access-Control-Allow-Headers: Indicates which HTTP headers (e.g., Authorization, Content-Type) are allowed in the actual request.

These headers are crucial for the browser to permit the cross-origin request.

CORS & Credentials

The Access-Control-Allow-Credentials header is another important CORS header.

When set to true, it tells the browser that the server permits cookies, HTTP authentication, or client-side SSL certificates to be included with the cross-origin request.

For OAuth2/OIDC, access tokens are typically sent in the Authorization header, not as cookies. However, if you're using session cookies for authentication or identity management alongside tokens, this header becomes relevant.

CORS in OAuth2/OIDC Flows

CORS is essential at several points in OAuth2/OIDC:

  • Token Exchange: Your SPA might need to exchange an authorization code for an access token at the Authorization Server's token endpoint. This is a cross-origin POST request.
  • API Access: Once your SPA has an access token, it uses it to call a Resource Server's API (e.g., GET /userinfo, POST /orders). This is also a cross-origin request.

The Authorization Server and Resource Server must be configured correctly to allow these requests from your SPA's origin.

Best Practices for CORS

To ensure secure OAuth2/OIDC implementations with CORS:

  • Specific Origins: Always specify exact origins (e.g., https://my-app.com) in Access-Control-Allow-Origin, never use * in production.
  • Limit Methods & Headers: Only allow the HTTP methods and headers that your client applications genuinely need.
  • Configure Servers: Ensure your Authorization Server and Resource Server are correctly configured to send the necessary CORS headers.
  • Handle Preflights: Make sure your server can respond to OPTIONS requests for preflight checks.

Misconfigured CORS can lead to security vulnerabilities, allowing unauthorized access.

CORS Configuration Check

Your SPA at https://my-app.com needs to make a POST request with an Authorization header and Content-Type: application/json to your API at https://api.my-app.com/data. Which CORS response headers should the API server include to allow this?

Recap: CORS & Security

You've learned about Cross-Origin Resource Sharing (CORS) and its vital role in securing modern web applications, especially those leveraging OAuth2 and OpenID Connect.

  • CORS relaxes the browser's Same-Origin Policy.
  • It enables secure communication between different origins.
  • Preflight requests for 'complex' API calls are common with OAuth2 tokens.
  • Proper server-side configuration of CORS headers (Access-Control-Allow-Origin, Access-Control-Allow-Methods, Access-Control-Allow-Headers) is key.

Always follow best practices to prevent misconfigurations that could expose your protected resources.

자주 묻는 질문

“교차 출처 리소스 공유(CORS)” 강의는 무료인가요?

네 — “교차 출처 리소스 공유(CORS)” 전체 내용을 이 웹사이트에서 무료로 읽을 수 있습니다. 인터랙티브하게 실습하려면(내장 코드 에디터와 24/7 AI 튜터), CoddyKit PRO로 업그레이드하면 OAuth2 & OpenID Connect Deep Dive 강의 전체를 잠금 해제할 수 있습니다. OAuth2 & OpenID Connect Deep Dive 강의에는 총 4개의 강의가 포함되어 있습니다.

“교차 출처 리소스 공유(CORS)”에서 뭘 배우나요?

보호된 리소스에 접근하는 단일 페이지 애플리케이션을 중심으로 OAuth2 및 OIDC 환경에서 CORS 정책을 이해해 보세요. 브라우저에서 직접 실행하는 실습 코드로 OAuth2 & OpenID Connect Deep Dive을(를) 배우며, 24/7 AI 튜터가 강의를 진행하면서 질문에 답변해줍니다.

OAuth2 & OpenID Connect Deep Dive을(를) 시작하는 데 경험이 필요한가요?

사전 경험은 필요하지 않습니다. CoddyKit의 OAuth2 & OpenID Connect Deep Dive은(는) 초급자부터 고급 학습자까지를 위해 구성되어 있으므로, 여기서 시작하거나 처음부터 시작할 수 있으며 자신의 속도대로 진행할 수 있습니다. 이것은 4개 중 2번째 강의입니다.

“교차 출처 리소스 공유(CORS)” 강의는 얼마나 걸리나요?

대부분의 CoddyKit 강의는 약 5~10분이 소요됩니다. 각 강의는 간결하고 인터랙티브하여 꾸준한 진행이 가능하며, 웹과 앱에서 중단한 부분부터 바로 시작할 수 있습니다.

이 OAuth2 & OpenID Connect Deep Dive 강의에서 코드를 작성하고 실행할 수 있나요?

네. 모든 OAuth2 & OpenID Connect Deep Dive 강의에는 내장 코드 에디터가 포함되어 있으므로, 브라우저에서 바로 실제 코드를 작성하고 실행한 후 즉시 AI 피드백을 받을 수 있습니다 — 로컬 설정이 필요 없습니다.

이 강의의 모든 강의

  1. 동의 및 사용자 경험
  2. 교차 출처 리소스 공유(CORS)
  3. 프런트 채널과 백 채널 로그아웃 비교
  4. mTLS를 활용한 발신자 제약 토큰
← OAuth2 & OpenID Connect Deep Dive(으)로 돌아가기