1.1 Simple request
1.2 preflighted request
1.3 credentialed request
!) https://domain-a.com의 프론트 엔드 JavaScript 코드가 XMLHttpRequest를 사용하여 https://domain-b.com/data.json을 요청하는 경우, 보안 상의 이유로 브라우저는 스크립트에서 시작한 교차 출처 HTTP 요청을 제한합니다.
!!) 브라우저가 자발적으로 브라우저의 어플리케이션을 이용하는 사용자를 보호하기 위한 정책입니다.
TIP)Content-Type은 다음의 값들만 허용합니다.
- application/x-www-form-urlencoded
- multipart/form-data
- text/plain
예시)
1)JS 화면
const xhr = new XMLHttpRequest(); //xhr로 인스턴스를 만들어준뒤
const url = 'https://bar.other/resources/public-data/'; //리소스를 요청할 URL
xhr.open('GET', url); //xhr을 open함
xhr.onreadystatechange = someHandler;
xhr.send();
2)Developer 콘솔 화면
GET /resources/public-data/ HTTP/1.1
Host: bar.other
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:71.0) Gecko/20100101 Firefox/71.0
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
Connection: keep-alive
Origin: https://foo.example //요청한 origin
HTTP/1.1 200 OK
Date: Mon, 01 Dec 2008 00:23:53 GMT
Server: Apache/2
Access-Control-Allow-Origin: * //모든 도메인에서 unlock 하겠다.
Keep-Alive: timeout=2, max=100
Connection: Keep-Alive
Transfer-Encoding: chunked
Content-Type: application/xml
[…XML Data…]
TIP) https://bar.other 의 리소스 소유자가 오직 https://foo.example 의 요청만 리소스에 대한 접근을 허용하려는 경우 다음을 전송합니다.
Access-Control-Allow-Origin: https://foo.example
위와 같이 전송할 경우 이제 https://foo.example 이외의 도메인은 corss-site 방식으로 리소스에 접근할 수 없습니다.
예시)
1)JS화면
const xhr = new XMLHttpRequest();
xhr.open('POST', 'https://bar.other/resources/post-here/');
xhr.setRequestHeader('Ping-Other', 'pingpong');//헤더로 비표준 HTTP Ping-Other 요청헤더가 설정됨
xhr.setRequestHeader('Content-Type', 'application/xml');//content-Type을 설정함
xhr.onreadystatechange = handler;
xhr.send('<person><name>Arun</name></person>');
!) Content-Type 이 application/xml이며 사용자 정의 헤더가 사용되었으므로 위 요청은 preflighted로 처리됩니다.
2-1)Developer 화면(preflight request)
OPTIONS /resources/post-here/ HTTP/1.1
Host: bar.other
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:71.0) Gecko/20100101 Firefox/71.0
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
Connection: keep-alive
Origin: http://foo.example
Access-Control-Request-Method: POST //POST를 해도 되는지 여부를 먼저 요청함
Access-Control-Request-Headers: X-PINGOTHER, Content-Type
HTTP/1.1 204 No Content //응답 요청은 성공함(204는 클라이언트가 현재 페이지를 벗어나지 않아도 된다는것을 의미)
Date: Mon, 01 Dec 2008 01:15:39 GMT
Server: Apache/2
Access-Control-Allow-Origin: https://foo.example //foo.example에서 오는 요청은 승인 하겠다.
Access-Control-Allow-Methods: POST, GET, OPTIONS//다음 3가지 메소드를 허가 하겠다.
Access-Control-Allow-Headers: X-PINGOTHER, Content-Type //실제 요청에 헤더를 사용 할 수 있음을 확인함
Access-Control-Max-Age: 86400
Vary: Accept-Encoding, Origin
Keep-Alive: timeout=2, max=100
Connection: Keep-Alive
2-2)Developer 화면(preflighted request요청 후)
POST /resources/post-here/ HTTP/1.1
Host: bar.other
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:71.0) Gecko/20100101 Firefox/71.0
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
Connection: keep-alive
X-PINGOTHER: pingpong
Content-Type: text/xml; charset=UTF-8
Referer: https://foo.example/examples/preflightInvocation.html
Content-Length: 55
Origin: https://foo.example
Pragma: no-cache
Cache-Control: no-cache
<person><name>Arun</name></person>
HTTP/1.1 200 OK
Date: Mon, 01 Dec 2008 01:15:40 GMT
Server: Apache/2
Access-Control-Allow-Origin: https://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]
1.preflight 리다이렉트를 방지하기 위해 서버측 동작을 변경
2.preflight를 발생시키지 않는 simple request 가 되도록 요청을 변경
3.Fetch API를 사용하여 simple request 를 작성
4.첫 번째 단계에서 Response.url 혹은 XMLHttpRequest.responseURL 로부터 얻은 URL을 사용
예제)
const invocation = new XMLHttpRequest();
const url = 'http://bar.other/resources/credentialed-content/';
function callOtherDomain() {
if (invocation) {
invocation.open('GET', url, true);
invocation.withCredentials = true; // XMLHttpReques의 플래그중 하나를 사용
invocation.onreadystatechange = handler;
invocation.send();
}
}
GET /resources/credentialed-content/ HTTP/1.1
Host: bar.other
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:71.0) Gecko/20100101 Firefox/71.0
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
Connection: keep-alive
Referer: http://foo.example/examples/credential.html
Origin: http://foo.example
Cookie: pageAccess=2
HTTP/1.1 200 OK
Date: Mon, 01 Dec 2008 01:34:52 GMT
Server: Apache/2
Access-Control-Allow-Origin: https://foo.example
Access-Control-Allow-Credentials: true
Cache-Control: no-cache
Pragma: no-cache
Set-Cookie: pageAccess=3; expires=Wed, 31-Dec-2008 01:34:53 GMT
Vary: Accept-Encoding, Origin
Content-Encoding: gzip
Content-Length: 106
Keep-Alive: timeout=2, max=100
Connection: Keep-Alive
Content-Type: text/plain
[text/plain payload]