HP Alm์ REST API์์ ์ผ๋ถ ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ธ ์ค๋ ค๊ณ ํฉ๋๋ค. ์์ curl ์คํฌ๋ฆฝํธ์ ์ ์๋ํฉ๋๋ค. ๋ฐ์ดํฐ๋ฅผ ์ป์ต๋๋ค.
์ด์ JavaScript๋ฅผ ์ฌ์ฉํ์ฌ ๊ฐ์ ธ ์ค๋ ๊ฒ๊ณผ ๊ฐ์ ธ ์ค๊ธฐ ๋ฐ ES6 (๋ ๋ง๊ฑฐ๋ ์ ์)์ด ๋ ํฐ ๋ฌธ์ ์ธ ๊ฒ ๊ฐ์ต๋๋ค. ์ด ์ค๋ฅ ๋ฉ์์ง๊ฐ ๊ณ์ ๋ํ๋ฉ๋๋ค.
Fetch API๊ฐ๋ก๋ ํ ์ ์์ต๋๋ค. ํ๋ฆฌ ํ๋ผ์ดํธ ์์ฒญ์ ๋ํ ์๋ต์ด ์ก์ธ์ค ์ ์ด ํ์ธ์ ํต๊ณผํ์ง ๋ชปํจ : ์์ฒญ ๋ ๋ฆฌ์์ค์ โAccess-Control-Allow-Originโํค๋๊ฐ ์์ต๋๋ค. ๋ฐ๋ผ์ ์๋ณธ โ http://127.0.0.1:3000 โ์ ์ก์ธ์ค ํ ์ ์์ต๋๋ค. ์๋ต์ HTTP ์ํ ์ฝ๋๋ 501์ ๋๋ค. ๋ถํฌ๋ช ํ ์๋ต์ด ์ฌ์ฉ์์ ์๊ตฌ์ ๋ถ์ํ๋ ๊ฒฝ์ฐ CORS๊ฐ ๋นํ์ฑํ ๋ ์ํ์์ ๋ฆฌ์์ค๋ฅผ ๊ฐ์ ธ ์ค๋ ค๋ฉด ์์ฒญ ๋ชจ๋๋ฅผ โno-corsโ๋ก ์ค์ ํ์ญ์์ค.
๋ก์ปฌ ํธ์คํธ ๋ด์์ ํด๋น ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ธ ์ค๋ ค๊ณ ํ๊ธฐ ๋๋ฌธ์ ์๋ฃจ์ ์ด CORS๋ฅผ ์ฌ์ฉํด์ผํ๊ธฐ ๋๋ฌธ์ ์ด๊ฒ์ด ์ดํด๋ฉ๋๋ค. ์ด์ ์ค์ ๋ก ๊ทธ๋ ๊ฒํ๋ค๊ณ ์๊ฐํ์ง๋ง ์ด๋ป๊ฒ ๋ ํค๋์ ์ฐ๋ ๊ฒ์ ๋ฌด์ํ๊ฑฐ๋ ๋ฌธ์ ๊ฐ ๋ค๋ฅธ ๊ฒ์ ๋๊น?
๊ทธ๋ ๋ค๋ฉด ๊ตฌํ ๋ฌธ์ ๊ฐ ์์ต๋๊น? ๋ด๊ฐ ์๋ชปํ๊ณ ์์ต๋๊น? ๋ถํํ๋ ์๋ฒ ๋ก๊ทธ๋ฅผ ํ์ธํ ์ ์์ต๋๋ค. ๋๋ ์ ๋ง๋ก ์ฌ๊ธฐ ๋ถ์ด ์์ต๋๋ค.
function performSignIn() {
let headers = new Headers();
headers.append('Content-Type', 'application/json');
headers.append('Accept', 'application/json');
headers.append('Access-Control-Allow-Origin', 'http://localhost:3000');
headers.append('Access-Control-Allow-Credentials', 'true');
headers.append('GET', 'POST', 'OPTIONS');
headers.append('Authorization', 'Basic ' + base64.encode(username + ":" + password));
fetch(sign_in, {
//mode: 'no-cors',
credentials: 'include',
method: 'POST',
headers: headers
})
.then(response => response.json())
.then(json => console.log(json))
.catch(error => console.log('Authorization failed : ' + error.message));
}
Chrome์ ์ฌ์ฉํ๊ณ ์์ต๋๋ค. Chrome CORS ํ๋ฌ๊ทธ์ธ์ ์ฌ์ฉํด ๋ณด์์ง๋ง ๋ค๋ฅธ ์ค๋ฅ ๋ฉ์์ง๊ฐ ๋ํ๋ฉ๋๋ค.
์์ฒญ์ ์๊ฒฉ ์ฆ๋ช ๋ชจ๋๊ฐ โincludeโ์ธ ๊ฒฝ์ฐ ์๋ต์ โAccess-Control-Allow-Originโํค๋ ๊ฐ์ด ์์ผ๋ ์นด๋ โ*โ๊ฐ ์๋์ด์ผํฉ๋๋ค. ๋ฐ๋ผ์ ์๋ณธ โ http://127.0.0.1:3000 โ์ ์ก์ธ์ค ํ ์ ์์ต๋๋ค. XMLHttpRequest์ ์ํด ์์๋ ์์ฒญ์ ์๊ฒฉ ์ฆ๋ช ๋ชจ๋๋ withCredentials ์์ฑ์ ์ํด ์ ์ด๋ฉ๋๋ค.
๋ต๋ณ
์ด ๋ต๋ณ์ ๋ง์ ๊ทผ๊ฑฐ๋ฅผ ๋ค๋ฃจ๋ฏ๋ก ์ธ ๋ถ๋ถ์ผ๋ก ๋๋ฉ๋๋ค.
- CORS ํ๋ก์๋ฅผ ์ฌ์ฉํ์ฌ โNo Access-Control-Allow-Origin headerโ ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ๋ ๋ฐฉ๋ฒ
- CORS ํ๋ฆฌ ํ๋ผ์ดํธ๋ฅผ ํผํ๋ ๋ฐฉ๋ฒ
- ์ด๋ป๊ฒ ํด๊ฒฐํ๋ โ์ก์ธ์ค ์ ์ด โ ํ์ฉ โ ์์ฐ์ง ํค๋๋ ์์ผ๋ ์นด๋๊ฐ ์๋์ด์ผํฉ๋๋คโ ๋ฌธ์
CORS ํ๋ก์๋ฅผ ์ฌ์ฉํ์ฌ โNo Access-Control-Allow-Origin headerโ ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ๋ ๋ฐฉ๋ฒ
ํ๋ก ํธ ์๋ JavaScript ์ฝ๋๊ฐ ์์ฒญ์ ๋ณด๋ด๋ ์๋ฒ๋ฅผ ์ ์ดํ์ง ์๊ณ ํด๋น ์๋ฒ์ ์๋ต ๋ฌธ์ ๊ฐ ํ์ํ Access-Control-Allow-Origin
ํค๋ ๊ฐ ๋ถ์กฑํ ๊ฒฝ์ฐ ์๋ ์์ฒญ์ ํตํด ์์
์ ์ํ ํ ์ ์์ต๋๋ค. CORS ํ๋ก์. ์๋ ๋ฐฉ์์ ๋ณด์ฌ์ฃผ๊ธฐ ์ํด ๋จผ์ CORS ํ๋ก์๋ฅผ ์ฌ์ฉํ์ง ์๋ ์ฝ๋๊ฐ ์์ต๋๋ค.
const url = "https://example.com"; // site that doesnโt send Access-Control-*
fetch(url)
.then(response => response.text())
.then(contents => console.log(contents))
.catch(() => console.log("Canโt access " + url + " response. Blocked by browser?"))
catch
๋ธ๋ก์ ๋๋ฌ ํ๋ ์ด์ ๋ ๋ธ๋ผ์ฐ์ ๊ฐ ํด๋น ์ฝ๋๊ฐ์์ ์ค๋ ์๋ต์ ์ก์ธ์คํ์ง ๋ชปํ๊ฒํ๋ ๊ฒ์
๋๋ค https://example.com
. ๊ทธ๋ฆฌ๊ณ ๋ธ๋ผ์ฐ์ ๊ฐ ๊ทธ๋ ๊ฒํ๋ ์ด์ ๋ Access-Control-Allow-Origin
์๋ต ํค๋์ ์๋ต์ด ์๊ธฐ ๋๋ฌธ ์
๋๋ค.
์ด์ ์ ํํ ๋์ผํ ์์ด์ง๋ง CORS ํ๋ก์๊ฐ ์ถ๊ฐ ๋ ๊ฒ์ ๋๋ค.
const proxyurl = "https://cors-anywhere.herokuapp.com/";
const url = "https://example.com"; // site that doesnโt send Access-Control-*
fetch(proxyurl + url) // https://cors-anywhere.herokuapp.com/https://example.com
.then(response => response.text())
.then(contents => console.log(contents))
.catch(() => console.log("Canโt access " + url + " response. Blocked by browser?"))
์ฐธ๊ณ : https://cors-anywhere.herokuapp.com์ ๋ค์ด๋ก๋ ํ ๋ ๋ค์ด๋์๊ฑฐ๋ ์ฌ์ฉํ ์์๋ ๊ฒฝ์ฐ 2-3 ๋ถ๋ง์ Heroku์ ์์ฒด CORS Anywhere ์๋ฒ๋ฅผ ๋ฐฐํฌํ๋ ๋ฐฉ๋ฒ์ ์๋๋ฅผ ์ฐธ์กฐํ์ญ์์ค.
์์ ๋ ๋ฒ์งธ ์ฝ๋ ์ค ๋ํซ์ ์์ฒญ URL์ ๊ฐ์ ธ ์์ ํ๋ก์ URL์ ์ ๋์ฌ๋ก ์ฌ์ฉ ํ์ฌ https://cors-anywhere.herokuapp.com/https://example.com์ผ๋ก ๋ณ๊ฒฝํ๊ธฐ ๋๋ฌธ์ ์๋ต์ ์ฑ๊ณต์ ์ผ๋ก ์ก์ธ์ค ํ ์ ์์ต๋๋ค . ํด๋น ํ๋ก์๋ฅผ ํตํด ์์ฒญํ๋ฉด ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
- ์ ์์ฒญ์ ์ ๋ฌํฉ๋๋ค
https://example.com
. - ์์ ์๋ต์๋ฐ์ต๋๋ค
https://example.com
. Access-Control-Allow-Origin
์๋ต์ ํค๋๋ฅผ ์ถ๊ฐํฉ๋๋ค .- ์ถ๊ฐ ๋ ํค๋์ ํจ๊ป ํด๋น ์๋ต์ ์์ฒญ ํ๋ก ํธ ์๋ ์ฝ๋๋ก ๋ค์ ์ ๋ฌํฉ๋๋ค.
๊ทธ๋ฐ ๋ค์ ๋ธ๋ผ์ฐ์ ๋ ํ๋ก ํธ ์๋ ์ฝ๋๊ฐ ์๋ต์ ์ก์ธ์ค ํ ์์๊ฒํฉ๋๋ค. Access-Control-Allow-Origin
์๋ต ํค๋๊ฐ ์๋ ์๋ต์ด ๋ธ๋ผ์ฐ์ ์ ํ์ ๋๊ธฐ ๋๋ฌธ์
๋๋ค .
https://github.com/RobโW/cors-anywhere/์ ์ฝ๋๋ฅผ ์ฌ์ฉํ์ฌ ์์ ์ ํ๋ก์๋ฅผ ์ฝ๊ฒ ์คํํ ์ ์์ต๋๋ค .
5 ๊ฐ์ ๋ช
๋ น์ผ๋ก ๋ง ๊ทธ๋๋ก 2-3 ๋ถ๋ง์ Heroku์ ์์ ์ ํ๋ก์๋ฅผ ์ฝ๊ฒ ๋ฐฐํฌ ํ ์ ์์ต๋๋ค.
git clone https://github.com/Rob--W/cors-anywhere.git
cd cors-anywhere/
npm install
heroku create
git push heroku master
ํด๋น ๋ช
๋ น์ ์คํํ๋ฉด https://cryptic-headland-94862.herokuapp.com/ ๊ณผ ๊ฐ์ ์์ฒด CORS Anywhere ์๋ฒ๊ฐ ์คํ๋ฉ๋๋ค . ๋ฐ๋ผ์ ์์ฒญ URL ์์ ์ ๋์ฌ๋ฅผ ๋ถ์ด์ง https://cors-anywhere.herokuapp.com
๋ง๊ณ ๋์ ์์ ์ ์ธ์คํด์ค URL๋ก ์ ๋์ฌ๋ฅผ ๋ถ์ด์ญ์์ค . ์๋ฅผ ๋ค์ด, https://cryptic-headland-94862.herokuapp.com/https://example.com .
๋ฐ๋ผ์ https://cors-anywhere.herokuapp.com์ ์ฌ์ฉํ๋ ค๊ณ ํ ๋ ๋ค์ด๋์ด ์์์ ๋ฐ๊ฒฌ ํ ๊ฒฝ์ฐ (์์ง ์ฌ์ฉํ์ง ์๋ ๊ฒฝ์ฐ) Heroku ๊ณ์ ์ ๊ฐ์ ธ ์์ 2๋ฅผ ๊ฐ์ ธ ์ค๋ ๊ฒ์ ๊ณ ๋ คํ์ญ์์ค. ๋๋ 3 ๋ถ ๋์ ์ ๋จ๊ณ๋ฅผ ์ํํ์ฌ Heroku์ ์์ ์ CORS Anywhere ์๋ฒ๋ฅผ ๋ฐฐํฌํ์ญ์์ค.
์์ฒด์ ์ผ๋ก ์คํํ๋ https://cors-anywhere.herokuapp.com ๋๋ ๊ธฐํ ๊ฐ๋ฐฉํ ํ๋ก์๋ฅผ ์ฌ์ฉํ๋ ์๊ด์์ด์ด ์๋ฃจ์
์ ๋ธ๋ผ์ฐ์ ๊ฐ CORS ํ๋ฆฌ ํ๋ผ์ดํธ OPTIONS
์์ฒญ ์ ์ํํ๋๋ก ๋ธ๋ผ์ฐ์ ๋ฅผ ํธ๋ฆฌ๊ฑฐ ํ๋ ๊ฒฝ์ฐ์๋ ์๋ํฉ๋๋ค. ๋ํ ํ๋ก์๋ ํ๋ฆฌ ํ๋ผ์ดํธ๋ฅผ ์ฑ๊ณต์ ์ผ๋ก ์ํํ๋ ๋ฐ ํ์ํ Access-Control-Allow-Headers
๋ฐ Access-Control-Allow-Methods
ํค๋๋ฅผ ๋ค์ ๋ณด๋
๋๋ค .
CORS ํ๋ฆฌ ํ๋ผ์ดํธ๋ฅผ ํผํ๋ ๋ฐฉ๋ฒ
๋ฌธ์ ์ ์ฝ๋๋ Authorization
ํค๋๋ฅผ ์ ์กํ๊ธฐ ๋๋ฌธ์ CORS ํ๋ฆฌ ํ๋ผ์ดํธ๋ฅผ ํธ๋ฆฌ๊ฑฐํฉ๋๋ค .
https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS#Preflighted_requests
๊ทธ๊ฒ ์์ด๋ Content-Type: application/json
ํค๋๋ ํ๋ฆฌ ํ๋ผ์ดํธ๋ฅผ ํธ๋ฆฌ๊ฑฐํฉ๋๋ค.
๋ฌด์ โํ๋ฆฌ ํ๋ผ์ดํธโ์๋จ : ๋ธ๋ผ์ฐ์ ๊ฐ๋ฅผ ์๋ํ๊ธฐ ์ ์ POST
๋ฌธ์ ์ ์ฝ๋์์, ๊ทธ๊ฒ์ ๋จผ์ ๋ณด๋ด๋๋ฆฝ๋๋ค OPTIONS
์๋ฒ์ ์์ฒญ์ โ ํฌ๋ก์ค ๊ธฐ์ ์์ ์ ์๋ฒ๊ฐ ์ฌ์ฉ ์ค๋จ ๋ ๊ฒฝ์ฐ ๊ฒฐ์ POST
ํฌํจ Authorization
ํ๊ณ Content-Type: application/json
ํค๋๋ฅผ.
์์ curl ์คํฌ๋ฆฝํธ์ ์ ์๋ํฉ๋๋ค. ๋ฐ์ดํฐ๋ฅผ ์ป์ต๋๋ค.
๋ก ์ฌ๋ฐ๋ฅด๊ฒ ํ
์คํธํ๋ ค๋ฉด ๋ธ๋ผ์ฐ์ ๊ฐ ์ ์ก curl
ํ๋ ํ๋ฆฌ ํ๋ผ์ดํธ OPTIONS
์์ฒญ์ ์๋ฎฌ๋ ์ดํธํด์ผํฉ๋๋ค .
curl -i -X OPTIONS -H "Origin: http://127.0.0.1:3000" \
-H 'Access-Control-Request-Method: POST' \
-H 'Access-Control-Request-Headers: Content-Type, Authorization' \
"https://the.sign_in.url"
โฆ https://the.sign_in.url
์ค์ sign_in
URL์ด ๋ฌด์์ด๋ ๋์ฒด๋ฉ๋๋ค .
๋ธ๋ผ์ฐ์ ๊ฐ ํด๋น OPTIONS
์์ฒญ ์์ ํ์ธํด์ผํ๋ ์๋ต ์๋ ๋ค์๊ณผ ๊ฐ์ ํค๋๊ฐ ํฌํจ๋์ด์ผํฉ๋๋ค.
Access-Control-Allow-Origin: http://127.0.0.1:3000
Access-Control-Allow-Methods: POST
Access-Control-Allow-Headers: Content-Type, Authorization
๊ฒฝ์ฐ OPTIONS
์๋ต์ด ๊ทธ ํค๋๋ฅผ ํฌํจํ์ง ์๋, ๋ธ๋ผ์ฐ์ ๋ ๋ฐ๋ก ๊ฑฐ๊ธฐ ์ค์ง, ์ฌ์ง์ด ๋ณด๋ดํ์ง ๋ง์ญ์์ค POST
์์ฒญ์. ๋ํ ์๋ต์ HTTP ์ํ ์ฝ๋๋ 2xx (์ผ๋ฐ์ ์ผ๋ก 200 ๋๋ 204) ์ฌ์ผํฉ๋๋ค. ๋ค๋ฅธ ์ํ ์ฝ๋ ์ธ ๊ฒฝ์ฐ ๋ธ๋ผ์ฐ์ ๊ฐ ๋ฐ๋ก ์ค์ง๋ฉ๋๋ค.
๋ฌธ์ ์ ์๋ฒ OPTIONS
๊ฐ 501 ์ํ ์ฝ๋๋ก ์์ฒญ์ ์๋ตํ๊ณ ์์ต๋๋ค. ์ด๋ ์์ฒญ์ ๋ํ ์ง์์ ๊ตฌํํ์ง ์์์ ๋ํ๋ด๋ ๊ฒ์ ์๋ฏธ OPTIONS
ํฉ๋๋ค. ์ด ๊ฒฝ์ฐ ๋ค๋ฅธ ์๋ฒ๋ ์ผ๋ฐ์ ์ผ๋ก 405 โMethod not allowedโ์ํ ์ฝ๋๋ก ์๋ตํฉ๋๋ค.
๋ฐ๋ผ์ POST
์๋ฒ OPTIONS
๊ฐ 405 ๋๋ 501 ๋๋ 200 ๋๋ 204 ์ด์ธ์ ๋ค๋ฅธ ์์ฒญ์ผ๋ก ํด๋น ์์ฒญ์ ์๋ต ํ๊ฑฐ๋ ํ์ํ ์๋ต์ผ๋ก ์๋ตํ์ง ์์ผ๋ฉด ํ๋ฐํธ ์๋ JavaScript ์ฝ๋์์ ํด๋น ์๋ฒ๋ก ์ง์ ์์ฒญ ์ ํ ์ ์์ต๋๋ค. ์๋ต ํค๋.
๋ฌธ์ ์ ์ฌ๋ก์ ๋ํ ํ๋ฆฌ ํ๋ผ์ดํธ๋ฅผ ์ ๋ฐํ์ง ์๋ ๋ฐฉ๋ฒ์ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
- ์๋ฒ๊ฐ
Authorization
์์ฒญ ํค๋๋ฅผ ์๊ตฌํ์ง ์๊ณ ๋์POST
์์ฒญ ๋ณธ๋ฌธ์ ๋ด์ฅ ๋ ์ธ์ฆ ๋ฐ์ดํฐ ๋๋ ์ฟผ๋ฆฌ ๋งค๊ฐ ๋ณ์์ ์์กด ํ๋ ๊ฒฝ์ฐ - ์๋ฒ์
POST
๋ณธ๋ฌธ์Content-Type: application/json
๋ฏธ๋์ด ์ ํ ์ด ํ์ํ์ง ์์ง๋ง ๋์ JSON ๋ฐ์ดํฐ ๊ฐ ์ ๊ฐ๋ ๋งค๊ฐ ๋ณ์ (๋๋ ๊ธฐํ)๋ฅผ ์ฌ์ฉ ํ์ฌPOST
๋ณธ๋ฌธ์ ์๋ฝ ํ ๊ฒฝ์ฐapplication/x-www-form-urlencoded
json
์ด๋ป๊ฒ ํด๊ฒฐํ๋ โ์ก์ธ์ค ์ ์ด โ ํ์ฉ โ ์์ฐ์ง ํค๋๋ ์์ผ๋ ์นด๋๊ฐ ์๋์ด์ผํฉ๋๋คโ ๋ฌธ์
๋ค๋ฅธ ์ค๋ฅ ๋ฉ์์ง๊ฐ ๋ํ๋ฉ๋๋ค.
์์ฒญ์ ์๊ฒฉ ์ฆ๋ช ๋ชจ๋๊ฐ โincludeโ์ธ ๊ฒฝ์ฐ ์๋ต์ โAccess-Control-Allow-Originโํค๋ ๊ฐ์ด ์์ผ๋ ์นด๋ โ*โ๊ฐ ์๋์ด์ผํฉ๋๋ค. ๋ฐ๋ผ์ ์๋ณธ โ http://127.0.0.1:3000 โ์ ์ก์ธ์ค ํ ์ ์์ต๋๋ค. XMLHttpRequest์ ์ํด ์์๋ ์์ฒญ์ ์๊ฒฉ ์ฆ๋ช ๋ชจ๋๋ withCredentials ์์ฑ์ ์ํด ์ ์ด๋ฉ๋๋ค.
์๊ฒฉ ์ฆ๋ช
์ด ํฌํจ ๋ ์์ฒญ์ ๊ฒฝ์ฐ Access-Control-Allow-Origin
์๋ต ํค๋ ๊ฐ์ด ์ธ ๊ฒฝ์ฐ ๋ธ๋ผ์ฐ์ ์์ ํ๋ฐํธ ์๋ JavaScript ์ฝ๋๊ฐ ์๋ต์ ์ก์ธ์ค ํ ์ ์์ต๋๋ค *
. ๋์ ์ด ๊ฒฝ์ฐ์ ๊ฐ์ ํ๋ก ํธ ์๋ ์ฝ๋์ ์์ ๊ณผ ์ ํํ ์ผ์นํด์ผํฉ๋๋ค http://127.0.0.1:3000
.
์ฐธ์กฐ ์๊ฒฉ์๋ ์์ฒญ๊ณผ ์์ผ๋ ์นด๋ MDN์ด HTTP ์ก์ธ์ค ์ ์ด (CORS) ๊ธฐ์ฌ๋ฅผ.
์์ฒญ์ ๋ณด๋ด๋ ์๋ฒ๋ฅผ ์ ์ดํ๋ โโ๊ฒฝ์ฐ์ด ๊ฒฝ์ฐ๋ฅผ ์ฒ๋ฆฌํ๋ ์ผ๋ฐ์ ์ธ ๋ฐฉ๋ฒ์ Origin
์์ฒญ ํค๋ ์ ๊ฐ์ ๊ฐ์ ธ ์์ Access-Control-Allow-Origin
์๋ต ํค๋ ์ ๊ฐ์ผ๋ก ์์ฝ / ๋ฐ์ํ๋ ์๋ฒ๋ฅผ ๊ตฌ์ฑํ๋ ๊ฒ ์
๋๋ค. ์๋ฅผ ๋ค์ด, nginx์ ๊ฒฝ์ฐ :
add_header Access-Control-Allow-Origin $http_origin
๊ทธ๋ฌ๋ ์ด๊ฒ์ ํ๋์ ์์ผ๋ฟ์ ๋๋ค. ๋ค๋ฅธ (์น) ์๋ฒ ์์คํ ์ ์์ ๊ฐ์ ๋ฐํฅํ๋ ์ ์ฌํ ๋ฐฉ๋ฒ์ ์ ๊ณตํฉ๋๋ค.
Chrome์ ์ฌ์ฉํ๊ณ ์์ต๋๋ค. ๋ํ ๊ทธ Chrome CORS ํ๋ฌ๊ทธ์ธ์ ์ฌ์ฉํด ๋ณด์์ต๋๋ค.
Chrome CORS ํ๋ฌ๊ทธ์ธ Access-Control-Allow-Origin: *
์ ๋ธ๋ผ์ฐ์ ๊ฐ ๋ณด๋ ์๋ต์ ํค๋๋ฅผ ๊ฐ๋จํ๊ฒ ์ฝ์
ํฉ๋๋ค . ํ๋ฌ๊ทธ์ธ์ด ๋ ๋๋ํ๋ค๋ฉด, ๊ฐ์ง Access-Control-Allow-Origin
์๋ต ํค๋ ์ ๊ฐ์ ํ๋ก ํธ ์๋ JavaScript ์ฝ๋์ ์ค์ ์์ ์ผ๋ก ์ค์ ํ๋ฉด http://127.0.0.1:3000
๋ฉ๋๋ค.
๋ฐ๋ผ์ ํ
์คํธ์๋ ํ๋ฌ๊ทธ์ธ์ ์ฌ์ฉํ์ง ๋ง์ญ์์ค. ์ฐ๋ง์
๋๋ค. ๋ธ๋ผ์ฐ์ ์์ ์๋ต์ ํํฐ๋งํ์ง ์๊ณ ์๋ฒ์์ ์ด๋ค ์๋ต์ ์ป๋ ์ง ํ
์คํธํ๋ ค๋ฉด curl -H
์์ ๊ฐ์ด ์ฌ์ฉํ๋ ๊ฒ์ด ์ข์ต๋๋ค .
fetch(โฆ)
์ง๋ฌธ ์ ์์ฒญ์ ๋ํ ํ๋ก ํธ ์๋ JavaScript ์ฝ๋๊น์ง :
headers.append('Access-Control-Allow-Origin', 'http://localhost:3000');
headers.append('Access-Control-Allow-Credentials', 'true');
๊ทธ ์ค์ ์ ๊ฑฐํ์ญ์์ค. Access-Control-Allow-*
ํค๋๋ ์๋ต ํค๋. ๋น์ ์ ์์ฒญ์ ๊ทธ๋ค์ ๋ณด๋ด๊ณ ์ถ์ง ์์ต๋๋ค. ํ์ํ ์ ์ผํ ํจ๊ณผ๋ ํ๋ฆฌ ํ๋ผ์ดํธ๋ฅผ ์ํํ๋๋ก ๋ธ๋ผ์ฐ์ ๋ฅผ ํธ๋ฆฌ๊ฑฐํ๋ ๊ฒ์
๋๋ค.
๋ต๋ณ
์ด ์ค๋ฅ๋ ํฌํธ ๋ฒํธ๋ฅผ ํฌํจํ์ฌ ํด๋ผ์ด์ธํธ URL๊ณผ ์๋ฒ URL์ด ์ผ์นํ์ง ์์ ๋ ๋ฐ์ํฉ๋๋ค. ์ด ๊ฒฝ์ฐ ๊ต์ฐจ ์ถ์ฒ ์์ ๊ณต์ ์ธ CORS์ ๋ํด ์๋น์ค๋ฅผ ํ์ฑํํด์ผํฉ๋๋ค.
Spring REST ์๋น์ค๋ฅผ ํธ์คํ ํ๋ ๊ฒฝ์ฐ Spring Framework ์ ๋ธ๋ก๊ทธ ๊ฒ์๋ฌผ CORS ์ง์์์ ์ฐพ์ ์ ์์ต๋๋ค .
Node.js ์๋ฒ๋ฅผ ์ฌ์ฉํ์ฌ ์๋น์ค๋ฅผ ํธ์คํ ํ๋ ๊ฒฝ์ฐ
- Node.js ์๋ฒ๋ฅผ ์ค์งํ์ญ์์ค.
npm install cors --save
-
server.js์ ๋ค์ ์ค์ ์ถ๊ฐํ์ญ์์ค.
var cors = require('cors') app.use(cors()) // Use this after the variable declaration
๋ต๋ณ
ํ๋ก ํธ ์๋์์ ์์ฒญ ํค๋ ๋ก ๋ค์ ์ฝ๋๋ฅผ ์ถ๊ฐํ๊ธฐ ๋๋ฌธ์ ๋ฌธ์ ๊ฐ ๋ฐ์ํ์ต๋๋ค .
headers.append('Access-Control-Allow-Origin', 'http://localhost:3000');
headers.append('Access-Control-Allow-Credentials', 'true');
์ด๋ฌํ ํค๋๋ ์์ฒญ์ด ์๋ response ์ ์ํฉ๋๋ค . ๋ฐ๋ผ์ ๋ผ์ธ์ ํฌํจํ์ฌ ์ ๊ฑฐํ์ญ์์ค .
headers.append('GET', 'POST', 'OPTIONS');
๊ทํ์ ์์ฒญ์ โContent-Type : application / jsonโ์ด ์์์ผ๋ฏ๋ก CORS ํ๋ฆฌ ํ๋ผ์ดํธ๊ฐ ํธ๋ฆฌ๊ฑฐ๋์์ต๋๋ค. ์ด๋ก ์ธํด ๋ธ๋ผ์ฐ์ ๊ฐ OPTIONS ๋ฉ์๋๋ก ์์ฒญ์ ๋ณด๋์ต๋๋ค. ์์ธํ ๋ด์ฉ์ CORS ํ๋ฆฌ ํ๋ผ์ดํธ ๋ฅผ ์ฐธ์กฐํ์ญ์์ค.
๋ฐ๋ผ์ ๋ฐฑ์๋ ์์๋ ๋ค์์ ํฌํจํ๋ ์๋ต ํค๋๋ฅผ ๋ฐํํ์ฌ์ด ํ๋ฆฌ ํ๋ผ์ดํธ ์์ฒญ์ ์ฒ๋ฆฌํด์ผํฉ๋๋ค.
Access-Control-Allow-Origin : http://localhost:3000
Access-Control-Allow-Credentials : true
Access-Control-Allow-Methods : GET, POST, OPTIONS
Access-Control-Allow-Headers : Origin, Content-Type, Accept
๋ฌผ๋ก ์ค์ ๊ตฌ๋ฌธ์ ๋ฐฑ์๋์ ์ฌ์ฉํ๋ ํ๋ก๊ทธ๋๋ฐ ์ธ์ด์ ๋ฐ๋ผ ๋ค๋ฆ ๋๋ค.
ํ๋ฐํธ ์๋์์๋ ๋ค์๊ณผ ๊ฐ์์ผํฉ๋๋ค.
function performSignIn() {
let headers = new Headers();
headers.append('Content-Type', 'application/json');
headers.append('Accept', 'application/json');
headers.append('Authorization', 'Basic ' + base64.encode(username + ":" + password));
headers.append('Origin','http://localhost:3000');
fetch(sign_in, {
mode: 'cors',
credentials: 'include',
method: 'POST',
headers: headers
})
.then(response => response.json())
.then(json => console.log(json))
.catch(error => console.log('Authorization failed : ' + error.message));
}
๋ต๋ณ
๋ด ๊ฒฝ์ฐ์๋ ์๋ ์๋ฃจ์ ์ ์ฌ์ฉํฉ๋๋ค.
ํ๋ฐํธ ์๋ ๋๋ ์ต๊ทค๋ฌ
post(
this.serverUrl, dataObjToPost,
{
headers: new HttpHeaders({
'Content-Type': 'application/json',
})
}
)
๋ฐฑ์๋ (PHP๋ฅผ ์ฌ์ฉํฉ๋๋ค)
header("Access-Control-Allow-Origin: http://localhost:4200");
header('Access-Control-Allow-Methods: GET, POST, OPTIONS');
header("Access-Control-Allow-Headers: Content-Type, Authorization");
$postdata = file_get_contents("php://input");
$request = json_decode($postdata);
print_r($request);
๋ต๋ณ
CORS ํ๋ก์๋ฅผ ์ฌ์ฉํ์ฌโ ํค๋ ์์ โ๋ฌธ์ ๋ฅผ ํด๊ฒฐ ํ๋ ๋ฐฉ๋ฒ์ ๊ดํ 2 ์ผํธAccess-Control-Allow-Origin
๋ฐฑ์๋์์ PHP๋ก ์์ ํ๋ ์ฌ๋๋ค์ ์ํด โCORS ํ๋ก์โ๋ฅผ ๋ฐฐํฌํ๋ ๊ฒ์ ๋ค์๊ณผ ๊ฐ์ด ๊ฐ๋จํฉ๋๋ค.
-
๋ค์ ๋ด์ฉ์ผ๋ก โno-cors.phpโ๋ผ๋ ํ์ผ์ ๋ง๋ญ๋๋ค.
$URL = $_GET['url'];
echo json_encode(file_get_contents($URL));
die(); -
ํ๋ก ํธ ์๋์์ ๋ค์๊ณผ ๊ฐ์ดํ์ญ์์ค.
fetch('https://example.com/no-cors.php' + '?url=' + url)
.then(response=>{*/Handle Response/*})
๋ต๋ณ
์ด๊ฒ์ ์ ๊ฑฐํ์ญ์์ค :
credentials: 'include',
๋ต๋ณ
์ฌ์ฉํ๋ dataType: 'jsonp'
๋ ์ํด ์ผํ์ต๋๋ค.
async function get_ajax_data(){
var _reprojected_lat_lng = await $.ajax({
type: 'GET',
dataType: 'jsonp',
data: {},
url: _reprojection_url,
error: function (jqXHR, textStatus, errorThrown) {
console.log(jqXHR)
},
success: function (data) {
console.log(data);
// note: data is already json type, you
// just specify dataType: jsonp
return data;
}
});
} // function