2017-05-22 72 views
1

我有一個後端API,我想通過使用Azure API Management進行代理。 這個後端API需要我提供一個Bearer Oauth2標記。 我想使用Azure APIM爲我處理Oauth2流程,並且我想公開一個將被客戶端應用程序使用的非常簡單的API。我想避免我的客戶端應用程序使用Oauth2。 我該如何處理APIM?我發現很多示例演示瞭如何使用Oauth2保護後端API,但這不是我嘗試實現的用例。 謝謝。Azure API管理:Oauth2與後端API

回答

0

您需要做的是添加一個標頭來請求 - 使用set-header策略將授權標頭設置爲所需的值。如果您可以在策略中硬編碼令牌,那將很好。

如果你不能 - 你必須使用發送請求來組織OAuth流策略。簡而言之,您要做的是將您的應用ID和密碼發送給OAuth端點,並解析其響應以獲取令牌並將其附加到請求。

1

這裏是一個政策片段,使這項工作:

<send-request ignore-error="true" timeout="20" response-variable-name="bearerToken" mode="new"> 
     <set-url>{{authorizationServer}}</set-url> 
     <set-method>POST</set-method> 
     <set-header name="Content-Type" exists-action="override"> 
      <value>application/x-www-form-urlencoded</value> 
     </set-header> 
     <set-body> 
      @{ 
       return "client_id={{clientId}}&resource={{scope}}&client_secret={{clientSecret}}&grant_type=client_credentials"; 
      } 
     </set-body> 
    </send-request> 

    <set-header name="Authorization" exists-action="override"> 
     <value> 
      @("Bearer " + (String)((IResponse)context.Variables["bearerToken"]).Body.As<JObject>()["access_token"]) 
     </value> 
    </set-header> 

    <!-- We do not want to expose our APIM subscription key to the backend API --> 
     <set-header exists-action="delete" name="Ocp-Apim-Subscription-Key"/> 

來源:https://github.com/orangetoken/api-management-policy-snippets/blob/master/Snippets/Add%20Azure%20AD%20OAuth2%20bearer%20token%20to%20request%20to%20AD%20protected%20API.xml

很快從APIM團隊APIM政策片斷分公司