0

在 Azure APIM 处设置的响应标头,转为小写,而不是保留确切的标头名称。以下是验证 JWT 令牌的 APIM 策略。由于令牌无效或令牌过期,JWT 验证不成功时,设置 header WWW-Authenticate

<policies>
    <inbound>
        <base />
        <validate-jwt header-name="Authorization" failed-validation-httpcode="401" failed-validation-error-message="Unauthorized. Access token is missing or invalid." require-scheme="Bearer" output-token-variable-name="jwt">
            <openid-config url="https://login.microsoftonline.com/my_tenant/v2.0/.well-known/openid-configuration" />
            <audiences>
                <audience>my_audience_string</audience>
            </audiences>
            <issuers>
                <issuer>https://sts.windows.net/my_tenant/</issuer>
            </issuers>
            <required-claims>
                <claim name="roles" match="any">
                    <value>clients.manage</value>
                    <value>clients.delete</value>
                    <value>clients.read</value>
                </claim>
            </required-claims>
        </validate-jwt>
    </inbound>
    <backend>
        <base />
    </backend>
    <outbound>
        <base />
        <set-header name="content-type" exists-action="override">
            <value>application/json</value>
        </set-header>
    </outbound>
    <on-error>
        <base />
        <choose>
            <when condition="@(context.Response.StatusCode == 401)">
                <set-header name="WWW-Authenticate" exists-action="override">
                    <value>@("Bearer realm="+context.Request.OriginalUrl.Host)</value>
                </set-header>
            </when>
        </choose>
    </on-error>
</policies>

期待响应标头WWW-Authenticate,但实际上得到www-authenticate(全部小写)。

在此处输入图像描述

这是预期的吗?

4

1 回答 1

0

谢谢钱德拉莫汉。发布您的建议作为帮助其他社区成员的答案。

如果添加context.Request.Body.As<JObject>,则可以忽略大小写。

<choose>
<when condition="@((context.Request.Body != null) && context.Request.Body.As<JObject>(preserveContent: true).GetValue("channelId", StringComparison.OrdinalIgnoreCase)?.Value<string>() != null)">
    <set-header name="channelId" exists-action="override">
        <value>@(context.Request.Body.As<JObject>(preserveContent: true).GetValue("channelId", StringComparison.OrdinalIgnoreCase)?.Value<string>())</value>
    </set-header>
</when>

您也可以check-header政策声明中

<check-header name="header name" failed-check-httpcode="code" failed-check-error-message="message" ignore-case="true">
    <value>Value1</value>
    <value>Value2</value>
</check-header>

您可以参考Azure APIM 入站策略忽略大小写的属性名称检查 HTTP 标头

于 2021-09-27T09:52:31.360 回答