Azure API 管理 CORS:为什么我会收到“以‘Access-Control-’开头的标头已被删除...”

2024-02-10

通过以下简单的策略:

<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 响应标头,但我没有收到 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."
}

在我看来,这是一种无意义的行为,并且可能是一个错误。在提交之前我想问一下您是否有任何解释?为什么我会得到这个?

以“Access-Control-”开头的标头已从 回复。


在 Azure API 管理中执行 CORS 有两种方法。自动 - 只需在所需范围内删除和配置 CORS 策略,APIM 将负责响应与现有操作匹配的 OPTIONS 请求。

或者您可以选择手动方式 - 创建一个单独的操作来响应 OPTIONS 方法并在策略中手动形成响应,可能使用返回响应策略。

你遇到的问题是因为你两者都有。他们基本上是冲突的。 CORS 策略将请求识别为跨源,并安排在请求完成后进行处理,但 OPTIONS 操作级别上的返回响应策略会破坏此处理管道,并在 CORS 策略采取操作之前立即返回响应。

由于您使用的是 CORS 策略,因此您应该从 API 中删除 OPTIONS 操作才能正常工作。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Azure API 管理 CORS:为什么我会收到“以‘Access-Control-’开头的标头已被删除...” 的相关文章

随机推荐