2017-05-22 75 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政策片断分公司