2017-02-27 24 views
1

下面一個簡單的策略:Azure的API管理CORS:爲什麼我收到 「開始 '訪問 - 控制 - ' 被拆除頭......」

<policies> 
    <inbound> 
     <cors> 
      <allowed-origins> 
       <origin>http://microfost.com/</origin> 
      </allowed-origins> 
      <allowed-methods preflight-result-max-age="300"> 
       <method>GET</method> 
       <method>POST</method> 
       <method>PATCH</method> 
       <method>DELETE</method> 
      </allowed-methods> 
      <allowed-headers> 
       <header>content-type</header> 
       <header>accept</header> 
       <header>Authorization</header> 
      </allowed-headers> 
     </cors> 
    </inbound> 
</policies> 

HTTP請求

OPTIONS https://XXXX.azure-api.net/demo/XXX/XXX/* HTTP/1.1 
Host: XXXX.azure-api.net 
Ocp-Apim-Trace: true 
Ocp-Apim-Subscription-Key: <secret> 
Origin: http://microfost.com 
Access-Control-Request-Headers: Authorization 
Access-Control-Request-Method: GET 

回覆內容

Access-Control-Allow-Origin: http://microfost.com 
Ocp-Apim-Trace-Location: <trace> 
Date: Mon, 27 Feb 2017 20:09:14 GMT 
Content-Length: 0 

我收到這封郵件並期待Origin response header我沒有收到3封API中的2封的任何內容(1 API正在工作與預期相同的政策)。

**Inbound** 
[...] 
cors (0 ms) 
"Cross domain request was well formed and was allowed to proceed. CORS related headers were added to the response." 

**Backend** 

No records. 
Outbound 

cors (0 ms) 
{ 
    "message": "Headers starting with 'Access-Control-' were removed from the response. ", 
    "headers": [] 
} 
transfer-response (0 ms) 
{ 
    "message": "Response headers have been sent to the caller." 
} 

這在我看來是無意義的行爲,可能是一個錯誤。在提交之前,我想問問你是否有任何解釋?我爲什麼得到這個?

從「訪問控制 - 」開始的標題從 響應中刪除。

+0

這是你得到的整個迴應?您應該獲得Access-Control-Allow-Headers,Access-Control-Allow-Origin,Access-Control-Max-Age和Access-Control-Allow-Methods標題。但不只是「起源」。 CORS規範(https://www.w3.org/TR/cors/)將Origin描述爲僅請求頭。 –

+0

是的。這是我得到的整個迴應。我缺少的是你提到的標題。 –

+0

嘗試將\t 添加到您的入境政策,因此更高層面的政策的將被稱爲 –

回答

1

有兩個方法可以做到CORS在Azure的API管理。自動 - 只需在所需的範圍內刪除和配置CORS策略,並且APIM將處理匹配現有操作的OPTIONS請求。

或者你也可以選擇手動方式 - 創建一個單獨的操作,響應OPTIONS方法和形式響應手動權限的策略中,可能使用返回響應策略。

您遇到的問題是,因爲你有兩個。他們基本上有衝突。 CORS策略將請求標識爲跨源,並且在請求完成後調度處理,但OPTIONS操作級別上的返回 - 響應策略會中斷此處理管道,並在CORS策略可以採取行動之前立即返回響應。

由於您使用CORS政策,你應該從你的API移除選項操作纔可以正常工作。

+0

你是絕對正確的,我們已經測試過它的工作。雖然仍然令人不安的是,即使使用額外的OPTIONS策略,創建的第一個API也起作用。我會說文檔不清楚如何配置它(https://docs.microsoft.com/en-us/azure/api-management/api-management-cross-domain-policies#CORS)。感謝你的回答! –