開發跨原始資源共用(CORS)

運用能力的簡短範例 CORS 以透過使用者端JavaScript從外部Web應用程式存取AEM內容。 此範例使用CORS OSGi設定在AEM上啟用CORS存取。 OSGi設定方法適用於以下情況:

  • 單一來源正在存取AEM發佈內容
  • AEM Author需要CORS存取權

如果需要多來源的AEM Publish存取權,請參閱 本檔案.

在本影片中:

  • www.example.com 透過以下方式對應至本地主機: /etc/hosts
  • aem-publish.local 透過以下方式對應至本地主機: /etc/hosts
  • SimpleHTTPServer (的包裝函式) Python的SimpleHTTPServer)透過連線埠8000提供HTML頁面。
    • Mac App Store不再提供此功能。 使用類似的,例如 吉維斯.
  • AEM Dispatcher 執行時間 Apache HTTP Web Server 2.4和向的反向代理要求 aem-publish.locallocalhost:4503.

如需詳細資訊,請檢閱 瞭解AEM中的跨原始資源共用(CORS).

www.example.comHTML和JavaScript

此網頁的邏輯如下

  1. 按一下按鈕時
  2. 建立 AJAX GET 要求給 http://aem-publish.local/content/we-retail/.../experience/_jcr_content.1.json
  3. 擷取 jcr:title 形成JSON回應
  4. 插入 jcr:title 進入DOM
<html>
<head>
<script
  src="https://code.jquery.com/jquery-3.2.1.min.js"
  integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4="
  crossorigin="anonymous"></script>
</head>
<body style="width: 960px; margin: 2rem auto; font-size: 2rem;">
    <button style="font-size: 2rem;"
            data="fn-getTitle">Get Title as JSON from AEM</button>
    <pre id="title">The page title AJAX'd in from AEM will injected here</pre>

    <script>
    $(function() {

        /** Get Title as JSON **/
        $('body').on('click', '[data="fn-getTitle"]', function(e) {
            $.get('http://aem-publish.local/content/we-retail/us/en/experience/_jcr_content.1.json', function(data) {
                $('#title').text(data['jcr:title']);
            },'json');

            e.preventDefault();
            return false;
        });
    });
    </script>
</body>
</html>

OSGi工廠設定

的OSGi設定處理站 Cross-Origin Resource Sharing 可透過以下方式取得:

  • http://<host>:<port>/system/console/configMgr > Adobe Granite Cross-Origin Resource Sharing Policy
<?xml version="1.0" encoding="UTF-8"?>
<jcr:root xmlns:sling="http://sling.apache.org/jcr/sling/1.0" xmlns:jcr="http://www.jcp.org/jcr/1.0"
    jcr:primaryType="sling:OsgiConfig"
    alloworigin="[https://www.example.com:8000]"
    alloworiginregexp="[]"
    allowedpaths="[/content/we-retail/.*]"
    exposedheaders="[]"
    maxage="{Long}1800"
    supportedheaders="[Origin,Accept,X-Requested-With,Content-Type,
Access-Control-Request-Method,Access-Control-Request-Headers]"
    supportedmethods="[GET]"
    supportscredentials="{Boolean}false"
/>

Dispatcher設定 dispatcher-configuration

允許CORS要求標頭

若要允許必要的 要傳遞至AEM以進行處理的HTTP請求標頭,在Dispatcher的 /clientheaders 設定。

/clientheaders {
   ...
   "Origin"
   "Access-Control-Request-Method"
   "Access-Control-Request-Headers"
}

快取CORS回應標頭

若要允許在快取內容上快取及提供CORS標頭,請新增下列內容 /cache /headers設定 至AEM發佈 dispatcher.any 檔案。

/publishfarm {
    ...
    /cache {
        ...
        # CORS HTTP response headers
        # https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS#the_http_response_headers
        /headers {
            ...
            "Access-Control-Allow-Origin"
            "Access-Control-Expose-Headers"
            "Access-Control-Max-Age"
            "Access-Control-Allow-Credentials"
            "Access-Control-Allow-Methods"
            "Access-Control-Allow-Headers"
        }
    ...
    }
...
}

重新啟動網頁伺服器應用程式 變更後 dispatcher.any 檔案。

可能需要完全清除快取,以確保標題在之後的下一個請求中被適當地快取 /cache /headers 設定更新。

支援材料 supporting-materials

recommendation-more-help
c92bdb17-1e49-4e76-bcdd-89e4f85f45e6