Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Seminar4] CORS #10

Open
yeeun0702 opened this issue Nov 11, 2024 · 0 comments
Open

[Seminar4] CORS #10

yeeun0702 opened this issue Nov 11, 2024 · 0 comments

Comments

@yeeun0702
Copy link
Collaborator

๐Ÿ’กCORS๋ž€ ?

CORS(Cross-Origin Resource Sharing)๋Š” ์ถœ์ฒ˜๊ฐ€ ๋‹ค๋ฅธ ์ž์›๋“ค์„ ๊ณต์œ ํ•œ๋‹ค๋Š” ๋œป์œผ๋กœ, ํ•œ ์ถœ์ฒ˜์— ์žˆ๋Š” ์ž์›์—์„œ ๋‹ค๋ฅธ ์ถœ์ฒ˜์— ์žˆ๋Š” ์ž์›์— ์ ‘๊ทผํ•˜๋„๋ก ํ•˜๋Š” ๊ฐœ๋…์ž…๋‹ˆ๋‹ค. ์ง์—ญํ•˜๋ฉด, ๊ต์ฐจ๋˜๋Š” ์ถœ์ฒ˜ ์ž์›๋“ค์˜ ๊ณต์œ ์ž…๋‹ˆ๋‹ค. ๋‹ค๋ฅธ ์ถœ์ฒ˜์— ์žˆ๋Š” ์ž์›์„ ์š”์ฒญํ•œ๋‹ค๊ณ  ํ•˜๋ฉด, ์ด๋ฅผ ๊ต์ฐจ ์ถœ์ฒ˜ ์š”์ฒญ์ด๋ผ๊ณ  ๋ถ€๋ฆ…๋‹ˆ๋‹ค.

์ •๋ฆฌํ•˜์ž๋ฉด,ย CORS๋ž€ ๋„๋ฉ”์ธ์ด ๋‹ค๋ฅธ ์„œ๋ฒ„๋ผ๋ฆฌ ๋ฆฌ์†Œ์Šค๋ฅผ ์ฃผ๊ณ  ๋ฐ›์„ ๋•Œ ๋ณด์•ˆ์„ ์œ„ํ•ด ์„ค์ •๋œ ์ •์ฑ…์ด๋ผ๊ณ  ์ƒ๊ฐํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.
image

๋‹ค๋ฅธ ์ถœ์ฒ˜์˜ ์ž์›์— ์ ‘๊ทผํ•ด์„œ ๋ฆฌ์†Œ์Šค๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค.

๐Ÿ”Ž CORS์™€ ๊ด€๋ จํ•ด์„œ ์šฐ๋ฆฌ๊ฐ€ ๊ฐœ๋ฐœ์„ ํ•  ๋•Œ ๋งˆ์ฃผํ•˜๋Š” ๋ฌธ์ œ !!
์›น ํ”„๋ก ํŠธ ์—”๋“œ์™€ api ์„œ๋ฒ„๋ฅผ ๋”ฐ๋กœ ๊ตฌ์„ฑํ•˜๋Š” ๊ฒฝ์šฐ, ํ”„๋ก ํŠธ ์—”๋“œ์—์„œ ๋‹ค๋ฅธ origin์˜ api ์„œ๋ฒ„๋กœ ์š”์ฒญ์„ ๋ณด๋‚ด๋ฉด ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค.

๐Ÿ”Žย ํ•˜์ง€๋งŒ, Postman์ด๋‚˜ ์„œ๋ฒ„์—์„œ ๊ฐ™์€ ์š”์ฒญ์„ ๋ณด๋‚ด๋ฉด ํ†ต๊ณผ๋œ๋‹ค! ์™œ ๊ทธ๋Ÿด๊นŒ?
Postman์ด๋‚˜ ๋ฐฑ์—”๋“œ์—์„œ๋Š” CORS ์ •์ฑ…์„ ๋”ฐ๋ฅด์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. CORS๋Š” ์˜ค๋กœ์ง€ ๋ธŒ๋ผ์šฐ์ €(ํฌ๋กฌ, ์—ฃ์ง€, ์‚ฌํŒŒ๋ฆฌ๊ฐ™์€ ํ”„๋ก ํŠธ์—”๋“œ)๊ฐ€ ์ ์šฉํ•˜๋Š” ๋ณด์•ˆ ์ •์ฑ…์ด๊ธฐ ๋•Œ๋ฌธ์— ์„œ๋ฒ„ ์‚ฌ์ด์—์„œ๋Š” ์•„๋ฌด๋Ÿฐ ๋ฌธ์ œ ์—†์ด ๋ฐ์ดํ„ฐ๋ฅผ ์ฃผ๊ณ ๋ฐ›์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๐Ÿ”Ž ์ถœ์ฒ˜๋ž€ ?
์š”์ฒญ์„ ๋ณด๋‚ด๋Š” ์›น์‚ฌ์ดํŠธ์™€ ์š”์ฒญ์„ ๋ฐ›๋Š” API ์„œ๋ฒ„์˜ ์ฃผ์†Œ

image

์œ„์˜ ๊ตฌ์„ฑ์š”์†Œ ์ค‘์—์„œย Protocol + Host + Portย 3๊ฐ€์ง€๊ฐ€ ๊ฐ™์œผ๋ฉด ๋™์ผ ์ถœ์ฒ˜(Origin)๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค.ย 
๋‹ค๋ฅธ ๊ฒฝ์šฐ์— ๊ต์ฐจ ์ถœ์ฒ˜(Cross-Origin) ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค.


๐Ÿ’กCORS์˜ ๋“ฑ์žฅ๋ฐฐ๊ฒฝ

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

๊ทธ๋Ÿฌ๋‚˜ ์›น ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์ด ๋ณต์žกํ•ด์ง€๊ณ  ๋ถ„์‚ฐํ™”๋˜๋ฉด์„œ, ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ํ”„๋ก ํŠธ์—”๋“œ์™€ ๋ฐฑ์—”๋“œ๋ฅผ ๋ถ„๋ฆฌํ•˜์—ฌ ๊ฐœ๋ฐœํ•˜๋Š” ๋ฐฉ์‹์ด ๋ณดํŽธํ™”๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, https://example.com์—์„œ ํ˜ธ์ŠคํŒ…๋˜๋Š” ํ”„๋ก ํŠธ์—”๋“œ๊ฐ€ https://api.example.com์—์„œ ์ œ๊ณต๋˜๋Š” API์™€ ํ†ต์‹ ํ•˜๋Š” ๊ตฌ์กฐ๊ฐ€ ์ผ๋ฐ˜ํ™”๋˜์—ˆ์Šต๋‹ˆ๋‹ค.

์ด ๊ฒฝ์šฐ, ๋™์ผ ์ถœ์ฒ˜ ์ •์ฑ…์— ์˜ํ•ด ๋‹ค๋ฅธ ์„œ๋ธŒ๋„๋ฉ”์ธ์— ์ ‘๊ทผ์ด ์ฐจ๋‹จ๋˜๋Š” ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์„œ๋กœ ๋‹ค๋ฅธ ์ถœ์ฒ˜ ๊ฐ„์˜ ๋ฆฌ์†Œ์Šค ๊ณต์œ ๋ฅผ ์•ˆ์ „ํ•˜๊ฒŒ ํ—ˆ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์ƒˆ๋กœ์šด ๋ฉ”์ปค๋‹ˆ์ฆ˜์ด ํ•„์š”ํ•ด์กŒ์Šต๋‹ˆ๋‹ค.

๋”ฐ๋ผ์„œ CORS๊ฐ€ ๋“ฑ์žฅํ–ˆ์Šต๋‹ˆ๋‹ค. !!

CORS๋Š” ์›น ๋ณด์•ˆ์˜ ํ•„์ˆ˜ ์š”์†Œ์ธ ๋™์ผ ์ถœ์ฒ˜ ์ •์ฑ…์˜ ํ•œ๊ณ„๋ฅผ ๋ณด์™„ํ•˜๊ณ , ๋™์‹œ์— ๋‹ค๋ฅธ ์ถœ์ฒ˜์˜ ๋ฆฌ์†Œ์Šค์™€ ์•ˆ์ „ํ•˜๊ฒŒ ์ƒํ˜ธ์ž‘์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.

๐Ÿ”Ž ๊ทธ๋Ÿฐ๋ฐ ๋งŒ์•ฝ CORS ์ •์ฑ…์ด ์—†๋‹ค๋ฉด ?

CORS ์ •์ฑ…์ด ์—†๊ณ  ๋ชจ๋“  ๋‹ค๋ฅธ ์ถœ์ฒ˜ ์š”์ฒญ์ด ๊ฐ€๋Šฅํ•œ ๋ธŒ๋ผ์šฐ์ €๋ฅผ ์ƒ๊ฐํ•ด๋ด…์‹œ๋‹ค.

  1. CSRF(Cross-Site Request Forgery) ๊ณต๊ฒฉ ์œ„ํ—˜ ์ฆ๊ฐ€
    ๋‹ค๋ฅธ ์‚ฌ์ดํŠธ์—์„œ ์‚ฌ์šฉ์ž์˜ ๊ถŒํ•œ์œผ๋กœ ๋ฏผ๊ฐํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ๋ณ€๊ฒฝํ•˜๊ฑฐ๋‚˜ ์ ‘๊ทผํ•˜๋Š” ์š”์ฒญ์„ ๋ณด๋‚ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ์‚ฌ์šฉ์ž๊ฐ€ ์€ํ–‰ ์›น์‚ฌ์ดํŠธ์— ๋กœ๊ทธ์ธํ•œ ์ƒํƒœ์—์„œ ์•…์˜์ ์ธ ์›น์‚ฌ์ดํŠธ๊ฐ€ ํ•ด๋‹น ์‚ฌ์šฉ์ž์˜ ๊ถŒํ•œ์œผ๋กœ ์€ํ–‰ ์„œ๋ฒ„์— ์š”์ฒญ์„ ๋ณด๋‚ด ๊ณ„์ขŒ ์ด์ฒด๋ฅผ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

  2. ๊ฐœ์ธ ์ •๋ณด ์œ ์ถœ
    ๋‹ค๋ฅธ ๋„๋ฉ”์ธ์—์„œ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋ฅผ ํ†ตํ•ด ์ค‘์š”ํ•œ ๋ฐ์ดํ„ฐ(์˜ˆ: ์‚ฌ์šฉ์ž ์„ธ์…˜, ์ธ์ฆ ํ† ํฐ ๋“ฑ)์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ๋˜๋ฉด ์•…์„ฑ ์‚ฌ์ดํŠธ๊ฐ€ ์‚ฌ์šฉ์ž์˜ ๊ฐœ์ธ ์ •๋ณด๋ฅผ ์ˆ˜์ง‘ํ•˜๊ฑฐ๋‚˜, ๋ฏผ๊ฐํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ํ›”์น  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

  3. API ๋‚จ์šฉ
    ์•…์„ฑ ์‚ฌ์ดํŠธ๊ฐ€ API์— ์ž์œ ๋กญ๊ฒŒ ์ ‘๊ทผํ•˜์—ฌ ์„œ๋น„์Šค ์ž์›์„ ๋ฌด๋‹จ์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์„ ๋ฐฉ์ง€ํ•  ์ˆ˜ ์—†๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ด๋Š” ์„œ๋น„์Šค ๋น„์šฉ ์ฆ๊ฐ€์™€ API ์˜ค์šฉ์œผ๋กœ ์ด์–ด์งˆ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

  4. ๋ณด์•ˆ ์ •์ฑ… ๋ฌด๋ ฅํ™”
    CORS๋Š” ๋ธŒ๋ผ์šฐ์ €์—์„œ ์„œ๋ฒ„ ๊ฐ„์˜ ์š”์ฒญ์„ ๊ด€๋ฆฌํ•˜๊ณ  ์ œ์–ดํ•˜๋Š” ์ค‘์š”ํ•œ ๋ณด์•ˆ ๋ฉ”์ปค๋‹ˆ์ฆ˜์ž…๋‹ˆ๋‹ค. CORS๊ฐ€ ์—†์œผ๋ฉด ์„œ๋ฒ„์˜ ์˜๋„์™€ ์ƒ๊ด€์—†์ด ๋‹ค์–‘ํ•œ ๋„๋ฉ”์ธ์—์„œ ์„œ๋ฒ„์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋˜์–ด ๋ณด์•ˆ์ด ์ทจ์•ฝํ•ด์ง‘๋‹ˆ๋‹ค.

CORS ์ •์ฑ…์ด ์—†๋‹ค๋ฉด ์›น ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ๋ณด์•ˆ ์ˆ˜์ค€์ด ํฌ๊ฒŒ ๋‚ฎ์•„์ ธ ์•…์˜์ ์ธ ๊ณต๊ฒฉ์— ๋งค์šฐ ์ทจ์•ฝํ•ด์ง‘๋‹ˆ๋‹ค. CORS๋Š” ์ด๋Ÿฌํ•œ ๋ฌธ์ œ๋ฅผ ์™„ํ™”ํ•˜์—ฌ, ์›น ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์ด ์•ˆ์ „ํ•˜๊ฒŒ ์ƒํ˜ธ์ž‘์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ๋„์™€์ฃผ๋Š” ์ค‘์š”ํ•œ ๋ณด์•ˆ ๊ธฐ๋Šฅ์ž…๋‹ˆ๋‹ค.


๐Ÿ’กCORS ๋™์ž‘ ๊ณผ์ •

๐Ÿ“Œ CORS ๊ธฐ๋ณธ ํ๋ฆ„

  1. ๋ธŒ๋ผ์šฐ์ €๊ฐ€ ์š”์ฒญ์„ ์ „์†กํ•˜๋ฉด์„œ, ์š”์ฒญ ์ถœ์ฒ˜(Origin)์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ Origin ํ—ค๋”์— ๋‹ด์•„ ์„œ๋ฒ„๋กœ ๋ณด๋ƒ…๋‹ˆ๋‹ค.
    • Origin ํ—ค๋”๋Š” ์š”์ฒญ์ด ์–ด๋Š ์ถœ์ฒ˜(๋„๋ฉ”์ธ)์—์„œ ์‹œ์ž‘๋˜์—ˆ๋Š”์ง€ ์„œ๋ฒ„์— ์•Œ๋ฆฌ๋Š” ์—ญํ• ์„ ํ•ฉ๋‹ˆ๋‹ค.
  2. ์„œ๋ฒ„๋Š” ์š”์ฒญ์„ ํ™•์ธํ•˜๊ณ  ์‘๋‹ต์„ ๋ณด๋‚ผ ๋•Œ, ํ•ด๋‹น ์š”์ฒญ์ด ํ—ˆ์šฉ๋œ ์ถœ์ฒ˜์ธ์ง€ ์—ฌ๋ถ€๋ฅผ Access-Control-Allow-Origin ํ—ค๋”๋ฅผ ํ†ตํ•ด ๋ช…์‹œํ•ฉ๋‹ˆ๋‹ค.
    • ์„œ๋ฒ„๊ฐ€ ์š”์ฒญ์„ ํ—ˆ์šฉํ•œ๋‹ค๋ฉด, Access-Control-Allow-Origin ํ—ค๋”์— ์š”์ฒญํ•œ ์ถœ์ฒ˜(์˜ˆ: https://aaa.com)๋ฅผ ์„ค์ •ํ•˜๊ฑฐ๋‚˜, ๋ชจ๋“  ์ถœ์ฒ˜๋ฅผ ํ—ˆ์šฉํ•˜๋Š” ์™€์ผ๋“œ์นด๋“œ(``)๋ฅผ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  3. ๋ธŒ๋ผ์šฐ์ €๋Š” ์„œ๋ฒ„ ์‘๋‹ต์˜ CORS ํ—ค๋”๋ฅผ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค.
    • ๋งŒ์•ฝ ์„œ๋ฒ„์˜ Access-Control-Allow-Origin ๊ฐ’์ด ๋ธŒ๋ผ์šฐ์ €์˜ ์š”์ฒญ ์ถœ์ฒ˜์™€ ์ผ์น˜ํ•œ๋‹ค๋ฉด, ๋ธŒ๋ผ์šฐ์ €๋Š” ํ•ด๋‹น ์‘๋‹ต์„ ํ—ˆ์šฉํ•˜๊ณ  ๋ฆฌ์†Œ์Šค๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.
    • ์ผ์น˜ํ•˜์ง€ ์•Š๊ฑฐ๋‚˜ Access-Control-Allow-Origin ํ—ค๋”๊ฐ€ ์—†์œผ๋ฉด, ๋ธŒ๋ผ์šฐ์ €๋Š” CORS ์˜ค๋ฅ˜๋ฅผ ๋ฐœ์ƒ์‹œํ‚ค๊ณ  ์‘๋‹ต์„ ์ฐจ๋‹จํ•ฉ๋‹ˆ๋‹ค.

๐Ÿ“Œ ์กฐ๊ธˆ ๋” ์ž์„ธํ•˜๊ฒŒ 3๊ฐ€์ง€ ์‹œ๋‚˜๋ฆฌ์˜ค๊ฐ€ ์กด์žฌ !! CORS ์š”์ฒญ์˜ ์œ ํ˜•

1๏ธโƒฃ Simple Request (๋‹จ์ˆœ ์š”์ฒญ)

  • Simple Request๋Š” ๋ง ๊ทธ๋Œ€๋กœ ๋‹จ์ˆœํ•œ ์š”์ฒญ์œผ๋กœ, ๋ธŒ๋ผ์šฐ์ €๊ฐ€ ๋ฐ”๋กœ ์š”์ฒญ์„ ์ „์†กํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒฝ์šฐ์ž…๋‹ˆ๋‹ค.
  • ์ด ์š”์ฒญ์€ ๋ณดํ†ต ์ถ”๊ฐ€์ ์ธ ๋ณด์•ˆ ๊ฒ€์ฆ(์‚ฌ์ „ ์š”์ฒญ)์„ ๊ฑฐ์น˜์ง€ ์•Š๊ณ  ์„œ๋ฒ„๋กœ ์ง์ ‘ ์ „์†ก๋ฉ๋‹ˆ๋‹ค.

โœ…Simple Request ์กฐ๊ฑด

  • ์š”์ฒญ์˜ HTTP ๋ฉ”์„œ๋“œ๊ฐ€ GET, POST, HEAD ์ค‘ ํ•˜๋‚˜์—ฌ์•ผ ํ•ฉ๋‹ˆ๋‹ค.
  • Content-Type ํ—ค๋”๋Š” text/plain, application/x-www-form-urlencoded, multipart/form-data ์ค‘ ํ•˜๋‚˜๋งŒ ํฌํ•จํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
  • ์ปค์Šคํ…€ ํ—ค๋”(์‚ฌ์šฉ์ž ์ •์˜ ํ—ค๋”)๋ฅผ ํฌํ•จํ•˜์ง€ ์•Š์•„์•ผ ํ•ฉ๋‹ˆ๋‹ค.

๐ŸƒSimple Request ์˜ˆ์‹œ

GET /api/resource HTTP/1.1
# HTTP GET ์š”์ฒญ์œผ๋กœ api.example.com ์„œ๋ฒ„์˜ /api/resource ๊ฒฝ๋กœ์— ๋ฆฌ์†Œ์Šค๋ฅผ ์š”์ฒญ

Host: api.example.com
# ์š”์ฒญ์„ ๋ฐ›๋Š” ์„œ๋ฒ„์˜ ํ˜ธ์ŠคํŠธ๋ช… (api.example.com)

Origin: https://aaa.com
# ์š”์ฒญ์„ ๋ณด๋‚ธ ์ถœ์ฒ˜์˜ ๋„๋ฉ”์ธ (https://aaa.com)
# ์„œ๋ฒ„๊ฐ€ ํ—ˆ์šฉ๋œ ์ถœ์ฒ˜์ธ์ง€ ํ™•์ธํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ

์ด ๊ฒฝ์šฐ, ๋ธŒ๋ผ์šฐ์ €๋Š” ์„œ๋ฒ„์— Access-Control-Allow-Origin์ด ์„ค์ •๋œ ์‘๋‹ต์„ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. ๋งŒ์•ฝ ์„œ๋ฒ„๊ฐ€ ํ•ด๋‹น ์ถœ์ฒ˜๋ฅผ ํ—ˆ์šฉํ–ˆ๋‹ค๋ฉด ๋ธŒ๋ผ์šฐ์ €๋Š” ์ด ์‘๋‹ต์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.


2๏ธโƒฃ Preflight Request (์‚ฌ์ „ ์š”์ฒญ)

  • Preflight Request๋Š” ๋ฏผ๊ฐํ•˜๊ฑฐ๋‚˜ ์„œ๋ฒ„์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋Š” ์š”์ฒญ์ž…๋‹ˆ๋‹ค.
  • ๋ธŒ๋ผ์šฐ์ €๋Š” ๋ณธ ์š”์ฒญ์„ ๋ณด๋‚ด๊ธฐ ์ „์— ๋จผ์ € OPTIONS ๋ฉ”์„œ๋“œ๋กœ ์„œ๋ฒ„์— ์‚ฌ์ „ ์š”์ฒญ์„ ๋ณด๋‚ด์„œ ํ•ด๋‹น ์š”์ฒญ์ด ํ—ˆ์šฉ๋˜๋Š”์ง€ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค.

โœ… Preflight๊ฐ€ ํ•„์š”ํ•œ ๊ฒฝ์šฐ

  • ์š”์ฒญ ๋ฉ”์„œ๋“œ๊ฐ€ PUT, DELETE, PATCH ๋“ฑ ์„œ๋ฒ„์˜ ์ƒํƒœ๋ฅผ ๋ณ€๊ฒฝํ•  ๊ฐ€๋Šฅ์„ฑ์ด ์žˆ๋Š” ๋ฉ”์„œ๋“œ์ธ ๊ฒฝ์šฐ.
  • Authorization์ด๋‚˜ X-Custom-Header ๊ฐ™์€ ์ปค์Šคํ…€ ํ—ค๋”๊ฐ€ ํฌํ•จ๋œ ๊ฒฝ์šฐ.
  • Content-Type์ด application/json๊ณผ ๊ฐ™์ด ์ผ๋ฐ˜์ ์ด์ง€ ์•Š์€ ํŠน์ • ํƒ€์ž…์ผ ๊ฒฝ์šฐ.

โœ… Preflight Request ๊ณผ์ •

  1. OPTIONS ๋ฉ”์„œ๋“œ๋กœ ์‚ฌ์ „ ์š”์ฒญ ์ „์†ก: ๋ธŒ๋ผ์šฐ์ €๋Š” ์„œ๋ฒ„๋กœ ํ”„๋ฆฌํ”Œ๋ผ์ดํŠธ ์š”์ฒญ์„ ๋จผ์ € ์ „์†กํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ์š”์ฒญ์˜ Origin๊ณผ ์š”์ฒญ์— ์‚ฌ์šฉํ•  ๋ฉ”์„œ๋“œ์™€ ํ—ค๋”์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ์„œ๋ฒ„์— ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค.
  2. ์„œ๋ฒ„์˜ ์‘๋‹ต: ์„œ๋ฒ„๋Š” Access-Control-Allow-Origin, Access-Control-Allow-Methods, Access-Control-Allow-Headers ๋“ฑ์„ ์‘๋‹ต ํ—ค๋”์— ํฌํ•จํ•˜์—ฌ, ํ•ด๋‹น ์š”์ฒญ์ด ํ—ˆ์šฉ๋˜๋Š”์ง€ ์•Œ๋ ค์ค๋‹ˆ๋‹ค.
  3. ๋ณธ ์š”์ฒญ ์ „์†ก: ํ”„๋ฆฌํ”Œ๋ผ์ดํŠธ ์š”์ฒญ์ด ์Šน์ธ๋˜๋ฉด, ๋ธŒ๋ผ์šฐ์ €๋Š” ์‹ค์ œ ์š”์ฒญ์„ ์„œ๋ฒ„๋กœ ๋ณด๋ƒ…๋‹ˆ๋‹ค.

๐ŸƒPreflight Request ์˜ˆ์‹œ

OPTIONS /api/resource HTTP/1.1
# OPTIONS ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด ์‚ฌ์ „ ์š”์ฒญ์„ ์ „์†กํ•˜์—ฌ, DELETE ์š”์ฒญ์„ ํ—ˆ์šฉํ•˜๋Š”์ง€ ํ™•์ธ

Host: api.example.com
# ์š”์ฒญ์„ ๋ฐ›๋Š” ์„œ๋ฒ„์˜ ํ˜ธ์ŠคํŠธ๋ช… (api.example.com)

Origin: https://aaa.com
# ์š”์ฒญ์„ ๋ณด๋‚ธ ์ถœ์ฒ˜ (https://aaa.com)
# ์„œ๋ฒ„๊ฐ€ ํ—ˆ์šฉ๋œ ์ถœ์ฒ˜์ธ์ง€ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด ํฌํ•จ

Access-Control-Request-Method: DELETE
# ๋ณธ ์š”์ฒญ์— ์‚ฌ์šฉํ•  HTTP ๋ฉ”์„œ๋“œ๊ฐ€ DELETE์ž„์„ ์„œ๋ฒ„์— ์•Œ๋ฆผ

Access-Control-Request-Headers: Content-Type
# ๋ณธ ์š”์ฒญ์— ํฌํ•จ๋  ํ—ค๋”(Content-Type)๊ฐ€ ์žˆ๋Š”์ง€ ์„œ๋ฒ„์— ์•Œ๋ฆผ

๐Ÿƒ์„œ๋ฒ„์˜ ํ”„๋ฆฌํ”Œ๋ผ์ดํŠธ ์‘๋‹ต ์˜ˆ์‹œ:

HTTP/1.1 200 OK
# ์‚ฌ์ „ ์š”์ฒญ์— ๋Œ€ํ•œ ์‘๋‹ต์ด ์„ฑ๊ณต์ ์œผ๋กœ ํ—ˆ์šฉ๋˜์—ˆ์Œ์„ ๋‚˜ํƒ€๋‚ด๋Š” ์ƒํƒœ ์ฝ”๋“œ 200

Access-Control-Allow-Origin: https://aaa.com
# https://aaa.com ์ถœ์ฒ˜์—์„œ์˜ ์š”์ฒญ์„ ํ—ˆ์šฉ

Access-Control-Allow-Methods: DELETE
# DELETE ๋ฉ”์„œ๋“œ๋ฅผ ํ—ˆ์šฉํ•จ์„ ๋ช…์‹œ

Access-Control-Allow-Headers: Content-Type
# Content-Type ํ—ค๋”๋ฅผ ํฌํ•จํ•œ ์š”์ฒญ์„ ํ—ˆ์šฉ

๐Ÿƒ๋ณธ ์š”์ฒญ ์ „์†ก ์˜ˆ์‹œ:

DELETE /api/resource HTTP/1.1
# ์„œ๋ฒ„์˜ /api/resource ๊ฒฝ๋กœ์— DELETE ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด ์‹ค์ œ ๋ฐ์ดํ„ฐ ์‚ญ์ œ ์š”์ฒญ์„ ์ „์†ก

Host: api.example.com
# ์š”์ฒญ์„ ๋ฐ›๋Š” ์„œ๋ฒ„์˜ ํ˜ธ์ŠคํŠธ๋ช… (api.example.com)

Origin: https://aaa.com
# ์š”์ฒญ์„ ๋ณด๋‚ธ ์ถœ์ฒ˜ (https://aaa.com)

Content-Type: application/json
# ์š”์ฒญ ๋ณธ๋ฌธ์ด JSON ํ˜•์‹์ž„์„ ๋‚˜ํƒ€๋‚ด๋Š” Content-Type ํ—ค๋”

3๏ธโƒฃ ์ธ์ฆ ์ •๋ณด ํฌํ•จ ์š”์ฒญ (Credentials)

  • ์ธ์ฆ ์ •๋ณด๊ฐ€ ํ•„์š”ํ•œ ์š”์ฒญ์˜ ๊ฒฝ์šฐ, ํด๋ผ์ด์–ธํŠธ์™€ ์„œ๋ฒ„ ๋ชจ๋‘ ๋ณ„๋„์˜ ์„ค์ •์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ์ฟ ํ‚ค๋‚˜ ๋กœ๊ทธ์ธ ํ† ํฐ ๊ฐ™์€ ์ •๋ณด๋ฅผ ์„œ๋ฒ„๋กœ ์ „์†กํ•  ๋•Œ๊ฐ€ ์ด์— ํ•ด๋‹นํ•ฉ๋‹ˆ๋‹ค.

โœ…์ธ์ฆ ์ •๋ณด ํฌํ•จ ์š”์ฒญ์˜ ์กฐ๊ฑด

  • ํด๋ผ์ด์–ธํŠธ๋Š” credentials: 'include' ์˜ต์…˜์„ ์„ค์ •ํ•˜์—ฌ ๋ธŒ๋ผ์šฐ์ €๊ฐ€ ์ฟ ํ‚ค๋‚˜ ์„ธ์…˜ ๊ฐ™์€ ์ธ์ฆ ์ •๋ณด๋ฅผ ํฌํ•จํ•˜๋„๋ก ๋ช…์‹œํ•ฉ๋‹ˆ๋‹ค.
  • ์„œ๋ฒ„๋Š” ์‘๋‹ต์— Access-Control-Allow-Credentials: true ํ—ค๋”๋ฅผ ์ถ”๊ฐ€ํ•ด, ์ธ์ฆ ์ •๋ณด ํฌํ•จ ์š”์ฒญ์„ ํ—ˆ์šฉํ•˜๊ฒ ๋‹ค๋Š” ์˜์‚ฌ๋ฅผ ํ‘œ์‹œํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

์ฃผ์˜: Access-Control-Allow-Origin์— ์™€์ผ๋“œ์นด๋“œ *๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๊ณ , ํŠน์ • ๋„๋ฉ”์ธ๋งŒ ์ง€์ •ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

๐Ÿƒ์ธ์ฆ ์ •๋ณด ํฌํ•จ ์š”์ฒญ ์˜ˆ์‹œ

GET /api/secure-resource HTTP/1.1
# HTTP GET ์š”์ฒญ์œผ๋กœ api.example.com ์„œ๋ฒ„์˜ /api/secure-resource ๊ฒฝ๋กœ์— ๋ฆฌ์†Œ์Šค๋ฅผ ์š”์ฒญ

Host: api.example.com
# ์š”์ฒญ์„ ๋ฐ›๋Š” ์„œ๋ฒ„์˜ ํ˜ธ์ŠคํŠธ๋ช… (api.example.com)

Origin: https://aaa.com
# ์š”์ฒญ์„ ๋ณด๋‚ธ ์ถœ์ฒ˜์˜ ๋„๋ฉ”์ธ (https://aaa.com)
# ์„œ๋ฒ„๊ฐ€ ์ด ์ถœ์ฒ˜์—์„œ์˜ ์š”์ฒญ์„ ํ—ˆ์šฉํ• ์ง€ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด ํฌํ•จ

Cookie: session_id=abc123
# ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์ธ์ฆ ์ •๋ณด๋ฅผ ํฌํ•จํ•œ ์š”์ฒญ์„ ๋ณด๋ƒ„ (์—ฌ๊ธฐ์„œ๋Š” ์ฟ ํ‚ค "session_id=abc123")
# ๋กœ๊ทธ์ธ ์„ธ์…˜์ด๋‚˜ ์‚ฌ์šฉ์ž ์‹๋ณ„์„ ์œ„ํ•ด ์„œ๋ฒ„์— ์ฟ ํ‚ค๋ฅผ ์ „์†ก

๐Ÿƒ์„œ๋ฒ„์˜ ์‘๋‹ต ํ—ค๋”:

HTTP/1.1 200 OK
# ์„œ๋ฒ„๊ฐ€ ์š”์ฒญ์„ ์„ฑ๊ณต์ ์œผ๋กœ ์ฒ˜๋ฆฌํ–ˆ์Œ์„ ๋‚˜ํƒ€๋‚ด๋Š” ์ƒํƒœ ์ฝ”๋“œ 200

Access-Control-Allow-Origin: https://aaa.com
# ์„œ๋ฒ„๊ฐ€ ์ด ์š”์ฒญ ์ถœ์ฒ˜ (https://aaa.com)๋ฅผ ํ—ˆ์šฉํ•œ๋‹ค๋Š” ๊ฒƒ์„ ๋ช…์‹œ
# ์ธ์ฆ ์ •๋ณด ์š”์ฒญ ์‹œ, ๋ฐ˜๋“œ์‹œ ํŠน์ • ์ถœ์ฒ˜๋งŒ ์ง€์ •ํ•ด์•ผ ํ•˜๋ฉฐ, * ์™€์ผ๋“œ์นด๋“œ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†์Œ

Access-Control-Allow-Credentials: true
# ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์ธ์ฆ ์ •๋ณด๋ฅผ ํฌํ•จํ•œ ์š”์ฒญ์„ ํ—ˆ์šฉํ•˜๋„๋ก ์„ค์ •
# ๋ธŒ๋ผ์šฐ์ €๋Š” ์ด ํ—ค๋”๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ์–ด์•ผ ์ฟ ํ‚ค๋‚˜ ์„ธ์…˜ ์ •๋ณด๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Œ

์ด๋Ÿฌํ•œ CORS ์š”์ฒญ ์œ ํ˜•์„ ํ†ตํ•ด, ์›น ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์€ ๋ณด์•ˆ์ ์œผ๋กœ ์•ˆ์ „ํ•˜๊ฒŒ ๋‹ค๋ฅธ ์ถœ์ฒ˜์˜ ๋ฆฌ์†Œ์Šค์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๋” ์ž์„ธํ•œ ๋‚ด์šฉ์ด ์•Œ๊ณ  ์‹ถ๋‹ค๋ฉด !!
+++ ์™€์ธ์ž”์กฐ 5์ฃ ์›๋“ค๊ณผ์˜ ์ฝ”๋“œํ† ํฌ ๋‚ด์šฉ์„ ์ฐธ์กฐํ•˜์—ฌ ์ž‘์„ฑํ–ˆ์Šต๋‹ˆ๋‹ค ใ…Žใ…Ž
https://developer.mozilla.org/ko/docs/Web/HTTP/CORS

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant