【问题标题】:Azure APIM response headers turning to lower caseAzure APIM 响应标头变为小写
【发布时间】:2021-11-16 04:05:28
【问题描述】:

在 Azure APIM 处设置的响应标头,转为小写,而不是保留确切的标头名称。以下是验证 JWT 令牌的 APIM 策略。由于令牌无效或令牌过期而导致 JWT 验证不成功时,设置标头 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(全部小写)。

这是预期的吗?

【问题讨论】:

    标签: azure azure-api-management apim


    【解决方案1】:

    谢谢Chandra Mohan。发布您的建议作为帮助其他社区成员的答案。

    如果添加context.Request.Body.As&lt;JObject&gt;,则可以忽略大小写。

    <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 inbound policy ignore case for the property nameCheck HTTP header

    【讨论】:

    • 感谢您的回答。但我的问题看起来不同。 APIM 中设置的标头参数为WWW-Authenticate,但当以www-authenticate 到达调用方时,它变为小写。有什么方法可以保留在 APIM 设置的标头 AS-IS?
    • ignore-case - 可以设置为 True 或 False。如果设置为 True,则在将标头值与一组可接受值进行比较时忽略大小写。可以参考和API import restrictions and known issuesAuthorization in Azure API management through JWT token
    • 检查 HTTP 标头是否有预期值:context.Request.Headers.GetValueOrDefault("header-name", "").Equals("expected-header-value", StringComparison.OrdinalIgnoreCase) 参考:Common policy expressions
    • 抱歉,我没有在 APIM 查看传入的标头。从 APIM 发出的响应头变为小写。
    • 好的,当然。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-17
    • 2022-08-16
    • 1970-01-01
    • 2022-08-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多