크로스도메인 window.postMessage()

황제낙엽 2020.07.29 14:53 조회 수 : 2114

sitelink1 https://developer.mozilla.org/en-US/docs...ostMessage 
sitelink2 https://ooz.co.kr/232 
sitelink3 https://jjeong.tistory.com/476 
sitelink4 https://blog.naver.com/hs88610/221709466083 
extra_vars4 https://blog.naver.com/hs88610/221709466083 
extra_vars5  
extra_vars6  

The window.postMessage() method safely enables cross-origin communication between Window objects

 

 

Syntax

> targetWindow.postMessage(message, targetOrigin, [transfer]);

 

Example

 

/*

 * In window A's scripts, with A being on <http://example.com:8080>:

 */

var popup = window.open(/* popup details */);

 

// When the popup has fully loaded, if not blocked by a popup blocker:

 

// This does nothing, assuming the window hasn't changed its location.

popup.postMessage("The user is 'bob' and the password is 'secret'", "https://secure.example.net");

 

// This will successfully queue a message to be sent to the popup, assuming

// the window hasn't changed its location.

popup.postMessage("hello there!", "http://example.com");

 

function receiveMessage(event)

{

  // Do we trust the sender of this message?  (might be

  // different from what we originally opened, for example).

  if (event.origin !== "http://example.com")

    return;

 

  // event.source is popup

  // event.data is "hi there yourself!  the secret response is: rheeeeet!"

}

window.addEventListener("message", receiveMessage, false);

 

/*

 * In the popup's scripts, running on <http://example.com>:

 */

 

// Called sometime after postMessage is called

function receiveMessage(event)

{

  // Do we trust the sender of this message?

  if (event.origin !== "http://example.com:8080")

    return;

 

  // event.source is window.opener

  // event.data is "hello there!"

 

  // Assuming you've verified the origin of the received message (which

  // you must do in any case), a convenient idiom for replying to a

  // message is to call postMessage on event.source and provide

  // event.origin as the targetOrigin.

  event.source.postMessage("hi there yourself!  the secret response " + "is: rheeeeet!", event.origin);

}

 

window.addEventListener("message", receiveMessage, false);

 

 

그리고 popup 또는 iframe의 타겟페이지가 있는 서버의 web.xml에 다음과 같은 설정이 필요하다

 

<filter>

  <filter-name>CorsFilter</filter-name>

  <filter-class>org.apache.catalina.filters.CorsFilter</filter-class>

  <init-param>

    <param-name>cors.allowed.origins</param-name>

    <param-value>http://아이피:포트</param-value> <!-- 호출페이지가 위치한 서버의 도메인 주소

  </init-param>

  <init-param>

    <param-name>cors.allowed.methods</param-name>

    <param-value>GET,POST,HEAD,OPTIONS,PUT</param-value>

  </init-param>

  <init-param>

    <param-name>cors.allowed.headers</param-name>

    <param-value>Pragma,Expires,If-Modified-Since,Cache-Control,Content-Type,X-Requested-With,accept,Origin,Access-Control-Request-Method,Access-Control-Request-Headers</param-value>

  </init-param>

  <init-param>

    <param-name>cors.exposed.headers</param-name>

    <param-value>Access-Control-Allow-Origin,Access-Control-Allow-Credentials,</param-value>

  </init-param>

  <init-param>

    <param-name>cors.support.credentials</param-name>

    <param-value>true</param-value>

  </init-param>

  <init-param>

    <param-name>cors.preflight.maxage</param-name>

    <param-value>10</param-value>

  </init-param>

</filter>

<filter-mapping>

  <filter-name>CorsFilter</filter-name>

  <url-pattern>/*</url-pattern>

</filter-mapping>

 

또는, 타겟서버의 CGI페이지에서 다음과 같이 Response의 Header를 설정해도 된다

 

response.setHeader("Access-Control-Allow-Headers", "accept, cache-control, content-type, expires, if-modified-since, pragma, x-requested-with");

response.setHeader("Access-Control-Allow-Methods", "POST, GET, PUT, DELETE, OPTIONS, HEAD") ; // CORS에서 허용하는 메소드

response.setHeader("Access-Control-Max-Age", "3600"); // CORS가 유효한 시간

response.setHeader("Access-Control-Allow-Origin", "*"); // CORS를 허용하는 Origin

response.setHeader("Access-Control-Allow-Credentials", "true");

 

그리고 타겟페이지에서는 다음과 같이 스크립트를 작성한다

 

<script type="text/javascript">

    var retValue = "";

    

     window.onload = function() {

         if (window.addEventListener) {  // all browsers except IE before version 9

             window.addEventListener ("message", OnMessage, false);

         }

         else {

             if (window.attachEvent) {   // IE before version 9

                 window.attachEvent("onmessage", OnMessage);     // Internet Explorer from version 8

             }

         }

    }

 

    function OnMessage (event) {

        // Check the location of the caller

        // Opera earlier than version 10

        if ('domain' in event) {

            alert("event.domain-"+event.domain);

            if (event.domain != "http://172.10.12.11:9290") { //허용할 도메인 주소(호출페이지)

                return;

            }

        }

 

        // Firefox, Safari, Google Chrome, Internet Explorer from version 8 and Opera from version 10

        if ('origin' in event) {

            alert("event.origin-"+event.origin);

            if (event.origin != "http://172.10.12.11:9290") { //허용할 도메인 주소(호출페이지)

                return;

            }

        }

        

        message = '잘 받았습니다'; //회신할 메세지

        message = event.data + message;

        event.source.postMessage (message, event.origin);

    }

</script>

 

호출페이지에서는 타겟페이지의(iframe) window객체의 postMessage()를 호출하면서 데이터를 전달할 수 있다

 

 

번호 제목 글쓴이 날짜 조회 수
206 [GitHub] JavaScript Algorithms and Data Structures (알고리즘과 자료구조) file 황제낙엽 2021.03.01 595
205 콜백지옥과 비동기제어 file 황제낙엽 2021.02.18 1590
204 Json 특징 및 문법 황제낙엽 2021.01.26 522
203 비동기프로그래밍 - 콜백함수(Callback function) file 황제낙엽 2020.08.26 681
202 Strict 모드 황제낙엽 2020.08.23 627
» window.postMessage() 황제낙엽 2020.07.29 2114
200 withCredentials 관련 황제낙엽 2020.07.29 542
199 CORS 관련 레퍼런스 URL 황제낙엽 2020.07.10 590
198 ECMA 스크립트의 JSON 황제낙엽 2019.10.16 580
197 경과 시간 구하기 황제낙엽 2019.10.04 1673
196 입력받은 날짜와 현재 날짜와의 비교 함수 황제낙엽 2019.08.02 1049
195 사용자 모듈 만들기 황제낙엽 2019.07.09 42275
194 charcode 32와 160 차이 (javascript char 160 to 32) 황제낙엽 2019.05.11 580
193 UTF-8 한글 초성 추출 (자바스크립트) 황제낙엽 2019.05.07 750
192 IE브라우저에서 서버의 XML파일을 ajax로 가져와 DOM파싱하는 예제 (XMLHttpRequest, XML, ActiveXObject) 황제낙엽 2018.11.23 652
191 XMLHttpRequest.timeout 황제낙엽 2018.11.03 779
190 부동소수점 (floating-point) file 황제낙엽 2018.03.26 1626
189 User Agent Parser들 황제낙엽 2017.11.20 6204
188 window.postMessage 이해하기 file 황제낙엽 2017.10.16 1904
187 브라우저의 새로고침과 종료에 대한 이벤트 황제낙엽 2017.08.11 3327