You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Azure API Management与Web应用证书认证场景下如何支持大请求?

客户端证书认证大请求问题的APIM解决方案

Azure API Management(APIM)可通过自定义策略实现微软官方提出的两种解决客户端证书认证处理大请求失败的方案,具体实现方式如下:

方案1:通过HEAD请求预建立连接

可利用APIM的<send-request>策略,在转发大请求前先向后端发起HEAD请求,完成客户端证书的握手流程,避免后续大请求因证书认证环节超时或失败。

示例策略代码:

<policies>
    <inbound>
        <!-- 仅针对大请求触发预握手逻辑,可根据实际请求大小调整判断条件 -->
        <choose>
            <when condition="@(context.Request.Headers.GetValueOrDefault("Content-Length", "0") | int > 10485760)"> <!-- 10MB以上请求 -->
                <!-- 发起HEAD请求完成证书握手 -->
                <send-request mode="new" response-variable-name="headResponse" timeout="20" ignore-error="true">
                    <set-url>@(context.Request.Url.ToString())</set-url>
                    <set-method>HEAD</set-method>
                    <!-- 传递客户端证书到后端 -->
                    <authentication-certificate thumbprint="YOUR_CLIENT_CERT_THUMBPRINT" />
                </send-request>
            </when>
        </choose>
        <!-- 继续转发原大请求 -->
        <base />
    </inbound>
    <backend>
        <base />
    </backend>
    <outbound>
        <base />
    </outbound>
    <on-error>
        <base />
    </on-error>
</policies>

注意替换YOUR_CLIENT_CERT_THUMBPRINT为实际的客户端证书指纹,同时可根据业务需求调整触发预握手的请求大小阈值。

方案2:添加Expect: 100-continue请求头

通过APIM的<set-header>策略,在转发请求到后端前添加该请求头,让后端先返回100 Continue响应,确认证书认证通过后再发送大请求体。

示例策略代码:

<policies>
    <inbound>
        <!-- 针对大请求添加Expect请求头 -->
        <choose>
            <when condition="@(context.Request.Headers.GetValueOrDefault("Content-Length", "0") | int > 10485760)">
                <set-header name="Expect" exists-action="override">
                    <value>100-continue</value>
                </set-header>
            </when>
        </choose>
        <base />
    </inbound>
    <backend>
        <base />
    </backend>
    <outbound>
        <base />
    </outbound>
    <on-error>
        <base />
    </on-error>
</policies>

此方案无需额外发起请求,仅通过修改请求头即可实现,逻辑更简洁。

内容的提问来源于stack exchange,提问作者JustAMartin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.13 17:43:31