CORS(Cross-Origin Resource Sharing)

ยท 2976 words ยท 6 minute read

CORS(Cross-Origin Resource Sharing) ๐Ÿ”—

์›น ๋ณด์•ˆ ์ •์ฑ… ์ค‘ Same-Origin Policy๋Š” ํ•œ ์ถœ์ฒ˜(Origin)์—์„œ ๋กœ๋“œ๋œ ๋ฌธ์„œ๋‚˜ ์Šคํฌ๋ฆฝํŠธ๊ฐ€ ๋‹ค๋ฅธ ์ถœ์ฒ˜ ์ž์›๊ณผ ์ƒํ˜ธ์ž‘์šฉํ•˜์ง€ ๋ชปํ•˜๋„๋ก ์ œ์•ฝํ•œ๋‹ค. ์–ธ๊ธ‰ํ•œ ์ถœ์ฒ˜(Origin)๋Š” ๋‘ ํŽ˜์ด์ง€์˜ ํ”„๋กœํ† ์ฝœ, ํ˜ธ์ŠคํŠธ, ํฌํŠธ๊ฐ€ ๊ฐ™์œผ๋ฉด ๋™์ผ ์ถœ์ฒ˜๋กœ ๊ฐ„์ฃผํ•œ๋‹ค.

ํ•˜์ง€๋งŒ ์ด๋Ÿฌํ•œ ๋ณด์•ˆ ์ •์ฑ…์œผ๋กœ ์ธํ•ด ํƒ€ ์‚ฌ์ดํŠธ๋กœ๋ถ€ํ„ฐ ๋ฐ›์•„์˜ค๋Š” ๋ฆฌ์†Œ์Šค๋‚˜ ์›น ํฐํŠธ, CDN ๋“ฑ์˜ ์‚ฌ์šฉ์— ๋ฌธ์ œ๊ฐ€ ๋˜๊ณ  ์žˆ์–ด CORS(Cross-Origin Resource Sharing)์ด๋ผ๋Š” ์ถ”๊ฐ€ ์ •์ฑ…์ด ๋‚˜์˜ค๊ฒŒ ๋˜์—ˆ๋‹ค.

CORS ์š”์ฒญ ๐Ÿ”—

CORS ์š”์ฒญ์—๋Š” Simple/Preflight, Credential/Non-Credential์˜ ์กฐํ•ฉ์œผ๋กœ ์ด 4๊ฐ€์ง€ ์š”์ฒญ์ด ์กด์žฌํ•œ๋‹ค. ๋ธŒ๋ผ์šฐ์ €๊ฐ€ ์š”์ฒญ ๋‚ด์šฉ์„ ๋ถ„์„ํ•˜์—ฌ 4๊ฐ€์ง€ ๋ฐฉ์‹ ์ค‘ ํ•ด๋‹นํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ์„œ๋ฒ„์— ์š”์ฒญ์„ ๋‚ ๋ฆฌ๋ฏ€๋กœ ํ”„๋กœ๊ทธ๋ž˜๋จธ๊ฐ€ ๋ชฉ์ ์— ๋งž๋Š” ๋ฐฉ์‹์„ ์„ ํƒํ•ด ๊ทธ ์กฐ๊ฑด์— ๋งž๊ฒŒ ์ฝ”๋”ฉํ•ด์•ผ ํ•œ๋‹ค.

Simple Requests ๐Ÿ”—

๋ช‡๋ช‡ ์š”์ฒญ(Request)๋“ค์€ CORS preflight๋ฅผ ํŠธ๋ฆฌ๊ฑฐํ•˜์ง€ ์•Š๋Š”๋‹ค. MDN ์ž๋ฃŒ์™€ ํ‹ฐ๋งฅ์Šค ์ถœ์ฒ˜์ž๋ฃŒ์—์„œ๋Š” ์ด๋ฅผ ๋‘๊ณ  Simple Requests๋ผ๊ณ  ๊ตฌ๋ถ„ํ•˜์ง€๋งŒ CORS๋ฅผ ์ •์˜ํ•œ ์‹ค์ œ Fetch ์ŠคํŽ™์—์„œ๋Š” Simple Requests๋ผ๋Š” ์šฉ์–ด๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค. CORS preflight๋ฅผ ํŠธ๋ฆฌ๊ฑฐ ํ•˜์ง€ ์•Š๋Š” ์š”์ฒญ(ํŽธ์˜์ƒ MDN์—์„œ ‘simple requests’๋ผ๊ณ  ๋ช…๋ช…ํ–ˆ๋˜)์€ ์•„๋ž˜์˜ ์กฐ๊ฑด๋“ค์„ ๋ชจ๋‘ ๋งŒ์กฑํ•˜๋Š” ์š”์ฒญ์„ ๊ฐ€๋ฆฌํ‚จ๋‹ค.

  1. GET/POST/HEAD ๋ฉ”์„œ๋“œ๋งŒ์„ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค.
  2. User Agent์— ์˜ํ•ด ์ž๋™์œผ๋กœ ์„ค์ •๋œ ํ—ค๋”, Fetch ์ŠคํŽ™์—์„œ “forbidden header name"์ด๋ผ๊ณ  ์ •์˜๋œ ํ—ค๋”๋“ค์„ ์ œ์™ธํ•˜๊ณ  “CORS-safelisted request-header”๋ผ๊ณ  Fetch ์ŠคํŽ™์— ์ •์˜๋œ ์•„๋ž˜์˜ ํ—ค๋”๋งŒ์ด ์ง์ ‘์ ์œผ๋กœ ์š”์ฒญ ์•ˆ์— ์„ค์ •๋  ์ˆ˜ ์žˆ๋‹ค.
    • Accept
    • Accept-Language
    • Content-Language
    • Content-Type
    • DPR
    • Downlink
    • Save-Data
    • Viewport-Width
    • Width
  3. Content-Type์ด ์•„๋ž˜ ์ค‘ ํ•˜๋‚˜์—ฌ์•ผ ํ•œ๋‹ค.
    • application/x-www-form/urlencoded
    • multipart/form-data
    • text/plain (๋”ฐ๋กœ ์ง€์ •ํ•˜์ง€ ์•Š์„ ์‹œ์— default)
  4. Request ์•ˆ์— ReadableStream ๊ฐ์ฒด๊ฐ€ ์—†์–ด์•ผ ํ•œ๋‹ค.
  5. ์š”์ฒญ ์•ˆ์— ์žˆ๋Š” XMLHttpRequestUpload ๊ฐ์ฒด์— ๋Œ€ํ•œ ์ด๋ฒคํŠธ ๋ฆฌ์Šค๋„ˆ๊ฐ€ ์—†์–ด์•ผ ํ•œ๋‹ค. (ํ•ด๋‹น ๊ฐ์ฒด๋Š” XMLHttpRequest.upload ํ”„๋กœํผํ‹ฐ๋ฅผ ์ด์šฉํ•ด ์ ‘๊ทผ ๊ฐ€๋Šฅํ•˜๋‹ค.)

์ด๋Ÿฌํ•œ Simple Request ๋ฐฉ์‹์—์„œ ํด๋ผ์ด์–ธํŠธ๋Š” ์„œ๋ฒ„๋กœ ์š”์ฒญ์„ ํ•œ ๋ฒˆ ๋ณด๋‚ด๊ณ , ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์„œ๋ฒ„๋„ ํšŒ์‹ ์„ ํ•œ ๋ฒˆ ๋ณด๋‚ด๋Š” ๊ฒƒ์œผ๋กœ ์š”์ฒญ์— ๋Œ€ํ•œ ์‘๋‹ต์ด ์ข…๋ฃŒ๋œ๋‹ค.

์•„๋ž˜๋Š” Simple requests๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์˜ˆ์ œ์ด๋‹ค. ์•„๋ž˜ ์ฝ”๋“œ๊ฐ€ http://foo.example ์„œ๋ฒ„๋กœ๋ถ€ํ„ฐ ์ œ๊ณต๋˜์–ด http://bar.other๋ผ๋Š” ์™ธ๋ถ€ ๋„๋ฉ”์ธ์œผ๋กœ๋ถ€ํ„ฐ ๋ฆฌ์†Œ์Šค๋ฅผ ๋ฐ›์•„์˜ค๋ ค ํ•œ๋‹ค๋Š” ์ƒํ™ฉ์„ ๊ฐ€์ •ํ•ด๋ณด์ž.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
var invocation = new XMLHttpRequest();
var url = 'http://bar.other/resources/public-data/';

function callOtherDomain() {
  if(invocation) {
    invocation.open('GET', url, true);
    invocation.onreadystatechange = handler;
    invocation.send();
  }
}

์œ„ ์ฝ”๋“œ๋ฅผ ํ†ตํ•ด ์›น ๋ธŒ๋ผ์šฐ์ €๊ฐ€ ์„œ๋ฒ„๋กœ Request๋ฅผ ๋ณด๋‚ด๊ณ  ์„œ๋ฒ„๋กœ๋ถ€ํ„ฐ Response๋ฅผ ๋ฐ›๋Š” ๊ณผ์ •์„ ์•„๋ž˜์™€ ๊ฐ™์ด ๊ฐ„๋žตํ•˜๊ฒŒ ๋‚˜ํƒ€๋‚ผ ์ˆ˜ ์žˆ๋‹ค.

An example of CORS Simple Request

์ด ๋•Œ, ์‹ค์ œ Request์™€ Response๊ฐ€ ์–ด๋–ค ์‹์œผ๋กœ ์˜ค๊ณ  ๊ฐ€๋Š”์ง€ ์•„๋ž˜ ํ…์ŠคํŠธ๋ฅผ ๋ณด์ž. Request์˜ Origin, Response์˜ Access-Control-Allow-Origin ๋ถ€๋ถ„์„ ์ค‘์‹ฌ์œผ๋กœ ์‚ดํŽด๋ณด์ž.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
GET /resources/public-data/ HTTP/1.1
Host: bar.other
User-Agent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-US; rv:1.9.1b3pre) Gecko/20081130 Minefield/3.1b3pre
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Connection: keep-alive
Referer: http://foo.example/examples/access-control/simpleXSInvocation.html
Origin: http://foo.example

๋จผ์ €, ์œ„๋Š” ์˜ˆ์ œ ์ฝ”๋“œ๊ฐ€ ์„œ๋ฒ„๋กœ ๋ณด๋‚ด๋Š” Request๋ฅผ ๋‚˜ํƒ€๋‚ด๋ฉฐ Origin ํ—ค๋”๋ถ€๋ถ„์€ ์ปจํ…์ธ ๊ฐ€ http://foo.example๋กœ๋ถ€ํ„ฐ ์˜ค๋Š” ๊ฒƒ์ด๋ผ๊ณ  ์„œ๋ฒ„์—๊ฒŒ ์•Œ๋ฆฌ๋Š” ์—ญํ• ์„ ํ•œ๋‹ค.

1
2
3
4
5
6
7
8
HTTP/1.1 200 OK
Date: Mon, 01 Dec 2008 00:23:53 GMT
Server: Apache/2.0.61
Access-Control-Allow-Origin: *
Keep-Alive: timeout=2, max=100
Connection: Keep-Alive
Transfer-Encoding: chunked
Content-Type: application/xml

์œ„๋Š” ์„œ๋ฒ„๊ฐ€ ํด๋ผ์ด์–ธํŠธ๋กœ ๋ณด๋‚ด๋Š” Response๋ฅผ ๋‚˜ํƒ€๋‚ด๋ฉฐ, Access-Control-Allow-Origin: *์€ ๋ชจ๋“  ๋„๋ฉ”์ธ์˜ cross-site ๋ฐฉ์‹์œผ๋กœ๋ถ€ํ„ฐ ์•ก์„ธ์Šค๊ฐ€ ๊ฐ€๋Šฅํ•˜๋‹ค๊ณ  ํด๋ผ์ด์–ธํŠธ์—๊ฒŒ ์•Œ๋ฆฌ๋Š” ์—ญํ• ์„ ํ•œ๋‹ค. ํ•˜์ง€๋งŒ ๋งŒ์•ฝ ์ด ํ—ค๋”๊ฐ€

1
Access-Control-Allow-Origin: http://foo.example

์ฒ˜๋Ÿผ ์™”๋‹ค๋ฉด http://foo.example์„ ์ œ์™ธํ•œ ๋‹ค๋ฅธ ๋„๋ฉ”์ธ๋“ค์—์„œ๋Š” cross-site ๋ฐฉ์‹์œผ๋กœ ํ•ด๋‹น ๋ฆฌ์†Œ์Šค์— ์ ‘๊ทผํ•  ์ˆ˜ ์—†๋‹ค๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•œ๋‹ค.

Preflighted Requests ๐Ÿ”—

Simple Requests์™€ ๋‹ค๋ฅด๊ฒŒ “preflighted” requests(์‚ฌ์ „ ์ „๋‹ฌ ์š”์ฒญ)๋Š” ๋จผ์ € OPTIONS ๋ฉ”์„œ๋“œ๋ฅผ ์ด์šฉํ•˜์—ฌ HTTP request๋ฅผ ๋จผ์ € ๋ณด๋‚ด ์‹ค์ œ ์š”์ฒญ์ด ๋ณด๋‚ด๊ธฐ์— ์•ˆ์ „ํ•œ์ง€ ํ™•์ธํ•œ๋‹ค. ์•„๋ž˜ ์กฐ๊ฑด๋“ค ์ค‘ ํ•˜๋‚˜๋ผ๋„ ๋งŒ์กฑํ•˜๋ฉด Preflighted Requests๋กœ ๊ฐ„์ฃผํ•œ๋‹ค.

  1. Request๊ฐ€ ์•„๋ž˜ ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.
    • PUT
    • DELETE
    • CONNECT
    • OPTIONS
    • TRACE
    • PATCH
  2. Simple requests์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ User Agent์˜ ์ž๋™ ์„ค์ •๋œ ํ—ค๋”๋ฅผ ์ œ์™ธํ•˜๊ณ , “CORS-safelisted request-header"๋ฅผ ํฌํ•จํ•œ๋‹ค.
    • Accept
    • Accept-Language
    • Content-Language
    • Content-Type
    • DPR
    • Downlink
    • Save-Data
    • Viewport-Width
    • Width
  3. Content-Type ํ—ค๋” ๊ฐ’์ด ์•„๋ž˜๋ฅผ ์ œ์™ธํ•œ ๋‹ค๋ฅธ ๊ฐ’์ธ ๊ฒฝ์šฐ
    • application/x-www-form-url
    • multipart/form-data
    • text/plain
  4. Request ์•ˆ์— ์žˆ๋Š” XMLHttpRequestUpload ๊ฐ์ฒด์— ํ•œ ๊ฐœ ์ด์ƒ์˜ ์ด๋ฒคํŠธ ๋ฆฌ์Šค๋„ˆ๊ฐ€ ๋“ฑ๋ก๋œ ๊ฒฝ์šฐ
  5. ReadableStream์ด Request ์•ˆ์—์„œ ์‚ฌ์šฉ๋œ ๊ฒฝ์šฐ

์•„๋ž˜๋Š” preflighted ์š”์ฒญ์„ ์œ„ํ•œ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์˜ˆ์ œ์ด๋‹ค.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
var invocation = new XMLHttpRequest();
var url = 'http://bar.other/resources/post-here/';
var body = '<?xml version="1.0"?><person><name>Arun</name></person>';

function callOtherDomain(){
  if(invocation)
    {
      invocation.open('POST', url, true);
      invocation.setRequestHeader('X-PINGOTHER', 'pingpong');
      invocation.setRequestHeader('Content-Type', 'application/xml');
      invocation.onreadystatechange = handler;
      invocation.send(body);
    }
}

์œ„ ์˜ˆ์ œ ์ฝ”๋“œ์—์„œ๋Š” XML body๋ฅผ ๋ณด๋‚ด๊ธฐ ์œ„ํ•ด POST ๋ฐฉ์‹์„ ์‚ฌ์šฉํ•˜๊ณ  X-PINGOTHER: pingpong์ด๋ผ๋Š” customized request ํ—ค๋”๋ฅผ ์‚ฌ์šฉํ–ˆ๋‹ค. ๋˜ํ•œ, application/xml Content-Type์„ ์‚ฌ์šฉํ•จ์œผ๋กœ์จ ์œ„์—์„œ ๋ช…์‹œ๋œ 3๊ฐ€์ง€ Content-Type ์™ธ์— ํ•ด๋‹นํ•˜์—ฌ ํ•ด๋‹น request๊ฐ€ preflighted ํƒ€์ž…์ด๋ผ๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค.

์ด์ œ, ์ด preflighted request๊ฐ€ ์„œ๋ฒ„๋กœ ๋ณด๋‚ด์งˆ ๋•Œ ์–ด๋–ค ์‹์œผ๋กœ ์š”์ฒญ๊ณผ ์‘๋‹ต์ด ์˜ค๊ณ ๊ฐ€๋Š”์ง€ ์•„๋ž˜ ๊ทธ๋ฆผ์„ ํ†ตํ•ด ๊ฐœ๊ด„์ ์œผ๋กœ ์‚ดํŽด๋ณด์ž.

CORS Preflighted_request

์œ„ ๊ทธ๋ฆผ์—์„œ ์ฃผ์˜ํ•ด์•ผํ•  ๊ฒƒ์€ ์•„๋ž˜์˜ ์‹ค์ œ REQUEST/REPONSE ์ฝ”๋“œ์—์„œ ๋ณด๊ฒ ์ง€๋งŒ ์‹ค์ œ POST request ์•ˆ์—๋Š” Access-Control-Request-* ํ—ค๋”๊ฐ€ ์—†๋‹ค๋Š” ์ ์ด๋‹ค. ํ•ด๋‹น ํ—ค๋”๋“ค์€ ๋ชจ๋‘ OPTIONS request์—์„œ๋งŒ ํ•„์š”ํ•˜๋‹ค. ๋˜ํ•œ, preflighted request์˜ ๊ฒฝ์šฐ ๋ฉ”์ธ Request๋ฅผ ๋ณด๋‚ด๊ธฐ ์ „์— Preflighted Request๋ฅผ ํ•œ๋ฒˆ ๋” ๋ณด๋‚ธ๋‹ค๋Š” ์ ์ด ์ฃผ์˜ํ•˜์ž.

์•„๋ž˜๋Š” ์œ„ ๋‹ค์ด์–ด๊ทธ๋žจ์— ๋Œ€ํ•œ ์‹ค์ œ Request & Response ๋‚ด์šฉ์ด๋‹ค.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
OPTIONS /resources/post-here/ HTTP/1.1
Host: bar.other
User-Agent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-US; rv:1.9.1b3pre) Gecko/20081130 Minefield/3.1b3pre
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Connection: keep-alive
Origin: http://foo.example
Access-Control-Request-Method: POST
Access-Control-Request-Headers: X-PINGOTHER, Content-Type


HTTP/1.1 200 OK
Date: Mon, 01 Dec 2008 01:15:39 GMT
Server: Apache/2.0.61 (Unix)
Access-Control-Allow-Origin: http://foo.example
Access-Control-Allow-Methods: POST, GET
Access-Control-Allow-Headers: X-PINGOTHER, Content-Type
Access-Control-Max-Age: 86400
Vary: Accept-Encoding, Origin
Content-Encoding: gzip
Content-Length: 0
Keep-Alive: timeout=2, max=100
Connection: Keep-Alive
Content-Type: text/plain

๋จผ์ €, preflighted request์™€ ๊ทธ ์‘๋‹ต์— ๋Œ€ํ•ด ์‚ดํŽด๋ณด์ž. Access-Control-Request-Method ํ•ด๋”๋Š” ์„œ๋ฒ„์—๊ฒŒ ์‹ค์ œ Request๊ฐ€ ๋ณด๋‚ด์กŒ์„ ๋•Œ ํ•ด๋‹น Request์˜ ๋ฉ”์„œ๋“œ์™€ X-PINGOTHER, Content-Type ๋“ฑ์˜ custom header๋“ค์„ ํ•จ๊ป˜ ์ „์†กํ•  ๊ฒƒ์ด๋ผ๊ณ  ๋ฏธ๋ฆฌ ์•Œ๋ฆฐ๋‹ค. ์„œ๋ฒ„๋Š” ํด๋ผ์ด์–ธํŠธ๋กœ๋ถ€ํ„ฐ ์ด๋Ÿฌํ•œ ์ •๋ณด๋ฅผ ๋ฏธ๋ฆฌ preflighted request๋ฅผ ํ†ตํ•ด ์ „๋‹ฌ๋ฐ›๊ณ  ์‹ค์ œ request๋ฅผ ๋ฐ›์„ ๊ฒƒ์ธ์ง€๋ฅผ ๊ฒฐ์ •ํ•œ ๋’ค ์•Œ๋ ค์ค€๋‹ค. ์œ„์— ๋‚˜ํƒ€๋‚œ Reponse ์ฝ”๋“œ ์ค‘ ์œ ์‹ฌํ•ด์•ผํ•  ๋ถ€๋ถ„์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

1
2
3
4
Access-Control-Allow-Origin: http://foo.example
Access-Control-Allow-Methods: POST, GET
Access-Control-Allow-Headers: X-PINGOTHER, Content-Type
Access-Control-Max-Age: 86400

์„œ๋ฒ„๋Š” preflighted request์— ๋Œ€ํ•œ ์‘๋‹ต์„ ํ†ตํ•ด ํด๋ผ์ด์–ธํŠธ๋กœ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ ๋ฉ”์„œ๋“œ์™€ ํ—ค๋”, ๊ทธ๋ฆฌ๊ณ  ํ•ด๋‹น ๋ฆฌ์†Œ์Šค ์ ‘๊ทผ์„ ์œ„ํ•ด ํ—ˆ์šฉ๋œ origin์„ http://foo.example๋กœ ์ œํ•œํ•˜์—ฌ ๋ณด๋‚ด์ฃผ๊ณ  ์žˆ๋‹ค. ๋งˆ์ง€๋ง‰์œผ๋กœ Access-Control-Max-Age๋Š” ํ•ด๋‹น reponse๊ฐ€ ๋˜๋‹ค๋ฅธ preflight request๋ฅผ ๋ณด๋‚ด์ง€ ์•Š๊ณ  ์–ผ๋งˆ ๋™์•ˆ ์บ์‹œ๋˜์–ด ์žˆ๋Š”์ง€๋ฅผ ํด๋ผ์ด์–ธํŠธ์—๊ฒŒ ์•Œ๋ ค์ฃผ๋Š” ์—ญํ• ์„ ํ•œ๋‹ค. ์—ฌ๊ธฐ์„œ 86400์€ 86400์ดˆ๋ฅผ ๋‚˜ํƒ€๋‚ด์–ด 24์‹œ๊ฐ„๋™์•ˆ cached response๊ฐ€ ์œ ํšจํ•˜๋‹ค๊ณ  ์•Œ๋ฆฐ๋‹ค.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
POST /resources/post-here/ HTTP/1.1
Host: bar.other
User-Agent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-US; rv:1.9.1b3pre) Gecko/20081130 Minefield/3.1b3pre
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Connection: keep-alive
X-PINGOTHER: pingpong
Content-Type: text/xml; charset=UTF-8
Referer: http://foo.example/examples/preflightInvocation.html
Content-Length: 55
Origin: http://foo.example
Pragma: no-cache
Cache-Control: no-cache

<?xml version="1.0"?><person><name>Arun</name></person>


HTTP/1.1 200 OK
Date: Mon, 01 Dec 2008 01:15:40 GMT
Server: Apache/2.0.61 (Unix)
Access-Control-Allow-Origin: http://foo.example
Vary: Accept-Encoding, Origin
Content-Encoding: gzip
Content-Length: 235
Keep-Alive: timeout=2, max=99
Connection: Keep-Alive
Content-Type: text/plain

[Some GZIP'd payload]

Requests with credentials ๐Ÿ”—

๋งˆ์ง€๋ง‰์œผ๋กœ Credential, Non-Credential์„ ๊ตฌ๋ถ„ํ•  CORS Request ์ข…๋ฅ˜์— ๋Œ€ํ•ด ๊ธฐ์ˆ ํ•œ๋‹ค. ์ด “credentialed” requests๋Š” HTTP Cookie์™€ HTTP Authentication information์˜ ์ทจ์•ฝ์ ์— ๋Œ€๋น„ํ•˜์—ฌ ๋งŒ๋“ค์–ด์ง„ request ํƒ€์ž…์ด๋‹ค. ๊ธฐ๋ณธ์œผ๋กœ ์›น ๋ธŒ๋ผ์šฐ์ €๋Š” cross-site XMLHttpRequest์™€ Fetch invocation์—์„œ credential์„ ๋ณด๋‚ด์ง€ ์•Š๋Š”๋‹ค.

์•„๋ž˜ ์ฝ”๋“œ๋ฅผ ์˜ˆ๋กœ ๋“ค์–ด๋ณด์ž. http://bar.other/๋กœ๋ถ€ํ„ฐ ๋ฐ›์€ ์ปจํ…์ธ ๊ฐ€ ์ฟ ํ‚ค๋ฅผ ์„ค์ •ํ•˜๋Š” ๋ฆฌ์†Œ์Šค๋ผ๊ณ  ๊ฐ€์ •ํ•˜๊ณ , ์•„๋ž˜ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์ฝ”๋“œ๊ฐ€ http://foo.example๋‚ด์—์„œ ๋™์ž‘ํ•˜๋Š” ์ฝ”๋“œ๋ผ๊ณ  ์ƒ๊ฐํ•˜์ž.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
var invocation = new XMLHttpRequest();
var url = 'http://bar.other/resources/credentialed-content/';

function callOtherDomain(){
  if(invocation) {
    invocation.open('GET', url, true);
    invocation.withCredentials = true;
    invocation.onreadystatechange = handler;
    invocation.send();
  }
}

CORS Credential Response

์œ„์ฒ˜๋Ÿผ ์ƒˆ๋กœ ๊ฐ€์ ธ์˜ฌ ์ปจํ…์ธ ์—์„œ ์บ์‹œ๋ฅผ ์„ค์ •ํ•˜๋Š” ๋“ฑ์˜ ํ–‰์œ„๋ฅผ ํ•  ๋•Œ ๋ฐ˜๋“œ์‹œ withCredentials๋ฅผ ์„ค์ •ํ•ด์ค˜์•ผ ํ•˜๋ฉฐ, ํ•ด๋‹น ํ—ค๋”๊ฐ€ ์„ค์ •๋˜์—ˆ๋‹ค๋ฉด ์›น ๋ธŒ๋ผ์šฐ์ €๋Š” ์„œ๋ฒ„๋กœ๋ถ€ํ„ฐ ๋ฐ›์€ response ์•ˆ์— Access-Control-Allow-Credentials: true๊ฐ€ ์—†๋Š” ๊ฒฝ์šฐ๋Š” ๋ชจ๋‘ ๊ฑฐ์ ˆํ•ด๋ฒ„๋ฆฐ๋‹ค.

์ถœ์ฒ˜ ๐Ÿ”—