【问题标题】:Use azure Apim to Call an Api that uses OAuth2 token使用 azure Apim 调用使用 OAuth2 令牌的 Api
【发布时间】:2021-12-26 16:11:07
【问题描述】:

使用 Apim,我正在尝试调用需要 OAuth2 验证的后端 Api。这个问题或多或少类似于:Azure API Management: Oauth2 with backend API 但是这里没有好的答案……

我一直在阅读有关策略和缓存的大量内容。 但似乎无法正确设置。我希望能够调用 apim,然后 apim 调用后端 api 来获取令牌,并使用该令牌调用 Api 来获取一些输出数据。 我还发现了一个我必须在后端设置一些策略的地方.. 谁能帮我制定政策?

我的政策是这样的:

<policies>
    <inbound>
        <base />
        <set-variable name="originBearer" value="@(context.Request.Headers.GetValueOrDefault("Authorization", "empty_token").Split(' ')[0].ToString())" />
        <send-request ignore-error="true" timeout="20" response-variable-name="bearerToken" mode="new">
            <set-url>{{lookupAccessTokenUrl}}</set-url>
            <set-method>GET</set-method>
            <set-header name="Content-Type" exists-action="override">
                <value>application/x-www-form-urlencoded</value>
            </set-header>
            <set-body>@{
                    return "client_id={{HLR-app-client-id}}&scope={{HLR-scope}}&client_secret={{HLR-secret}}&assertion="+(string)context.Variables["originBearer"]+"&grant_type=urn:ietf:params:oauth:grant-type:client_credentials&requested_token_use=on_behalf_of";
            }</set-body>
        </send-request>
        <set-variable name="requestResponseToken" value="@((String)((IResponse)context.Variables["bearerToken"]).Body.As<JObject>()["access_token"])" />
        <set-header name="Authorization" exists-action="override">
            <value>@("Bearer " + (string)context.Variables["requestResponseToken"])</value>
        </set-header>
    </inbound>
    <backend>
        <base />
    </backend>
    <outbound>
        <base />
    </outbound>
    <on-error>
        <base />
    </on-error>
</policies>

【问题讨论】:

    标签: azure oauth-2.0 policies apim


    【解决方案1】:

    我找到了我自己问题的答案 :-) 我尝试对每一行进行评论,但如果您将所有代码放在一起,您将获得在后端 api 中处理 Oauth2 的策略。

    在入站部分,缓存查找值 将缓存中的值分配给名为“bearerToken”的上下文变量。 在第一次进入时,缓存值将为空,变量不会 已创建。

    <inbound>
    <cache-lookup-value key="cacheAccessToken" variable-name="bearerToken" />
    

    创建一个包含 clientid 和 secret 的变量 - 调用 api 所需的

        <set-variable name="user-password" value="{{HLR-Clientid}}:{{HLR-Secret}}" 
    />
        <choose>
    

    检查上下文变量集合是否包含一个名为 “bearerToken”,如果没有找到,则执行打开和关闭之间的代码 “” XML 元素。

    <when condition="@(!context.Variables.ContainsKey("bearerToken"))">
    

    通过响应向 OAuth 端点发起请求 超时 20 秒。这会将响应消息放入变量中 称为“oauthResponse”

    <send-request mode="new" response-variable-name="oauthResponse" timeout="20" ignore-error="false">
    <set-url>{{lookupAccessTokenUrl}}</set-url>
    <set-method>POST</set-method>
    <set-header name="Content-Type" exists-action="override">
    <value>application/x-www-form-urlencoded</value>
    </set-header>
    

    在这里你定义你的标题授权并使用包含 clientid 和密码的变量

    <set-header name="Authorization" exists-action="override">
    <value>@("Basic " +  system.Convert.ToBase64String(Encoding.UTF8.GetBytes((string)context.Variables["user-password"])))</value>
    </set-header>
    <set-body>@("grant_type=client_credentials&scope={{HLR-Scope}}")</set-body>
    </send-request>
    

    将响应转换为 JSON 对象,以允许使用索引器检索“access_token”值并将其分配给上下文变量“accessToken”。

    <set-variable name="AccessToken" value="@((string)((IResponse)context.Variables["oauthResponse"]).Body.As<JObject>()["access_token"])" />
    

    将结果存储在缓存中,我们将变量“accessToken”的内容添加到缓存中,时间为 3600 秒。

    <cache-store-value key="cacheAccessToken" value="@((string)context.Variables["AccessToken"])" duration="3600" />
    

    在上下文变量中设置变量,然后它可以立即使用

    <set-variable name="bearerToken" value="@((string)context.Variables["AccessToken"])" />
    </when>
    </choose>
    <base />
    </inbound>
    
    <backend>
        <!--Creates the request to the backend web service. Here we are placing the response from the web service into the variable called “transferWSResponse”.-->
        <send-request mode="copy" response-variable-name="transferWSResponse" timeout="60" ignore-error="false">
            <set-method>GET</set-method>
            <!--Is the creating the “Authorization” header to be sent with the request.-->
            <set-header name="Authorization" exists-action="override">
                <value>@("Bearer " + (string)context.Variables["bearerToken"])</value>
            </set-header>
            <!--Removes the APIM subscription from being forwarded to the backend web service.-->
            <set-header name="Ocp-Apim-Subscription-Key" exists-action="delete" />
            <set-header name="Content-Type" exists-action="override">
                <value>application/json</value>
            </set-header>
        </send-request>
    </backend>
    
    
    <outbound>
        <!--Now we need to return the response message from the backend web service to the caller. This is done in the “<outbound>” policy section. Here we just simply return the value of the variable “transferWSResponse” back to the caller-->
        <return-response response-variable-name="transferWSResponse" />
        <base />
    </outbound>
    <on-error>
        <base />
    </on-error>
    </policies>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-01-08
      • 1970-01-01
      • 1970-01-01
      • 2020-07-09
      • 2016-02-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多