根据特定的 JWT 声明将请求转发到区域 API
Forward requests to regional API based on a specific JWT claim
是否可以根据特定的 JWT 声明将请求转发到区域 API?
我正在使用的平台每个区域有一个 API,我们的客户需要了解它才能构建基本请求 URL - 例如:https://{地区}.service.com
不幸的是,这是尝试找出从 Azure APIM 自动调用哪个区域 api 的唯一可靠方法(例如:在 https://api.service.com), in our scenario, would be by analyzing a claim that always comes with the bearer token (which we already do at the APIM level 调用单个端点)
有没有人需要这样做?提前致谢!
APIM 策略表达式和 "choose" 策略允许您创建任意处理逻辑:https://docs.microsoft.com/en-us/azure/api-management/api-management-policy-expressions.
可以访问 JWT
context.Request.Headers.GetValueOrDefault("Authorization").AsJwt()
它 returns Jwt 对象(在上面的同一页面上查找它的属性)。
所有这些加上 "set-backend-service" 政策应该足以完成这项工作。
Vitaly 的回答是解决这个问题的关键。这是完整的答案,以防有人在寻找同样的东西。
<policies>
<inbound>
<!-- Extract Token from Authorization header parameter -->
<set-variable name="token" value="@(context.Request.Headers.GetValueOrDefault("Authorization",string.Empty).Split(' ').Last().AsJwt())" />
<choose>
<when condition="@(context.Variables["token"] != null)">
<set-variable name="api_uri" value="@(((Jwt)context.Variables["token"]).Claims.GetValueOrDefault("api_uri", string.Empty))" />
<choose>
<when condition="@(context.Variables["api_uri"] != string.Empty)">
<set-backend-service base-url="@((string)context.Variables["api_uri"])" />
</when>
<otherwise />
</choose>
</when>
<otherwise />
</choose>
<base />
</inbound>
</policies>
是否可以根据特定的 JWT 声明将请求转发到区域 API?
我正在使用的平台每个区域有一个 API,我们的客户需要了解它才能构建基本请求 URL - 例如:https://{地区}.service.com
不幸的是,这是尝试找出从 Azure APIM 自动调用哪个区域 api 的唯一可靠方法(例如:在 https://api.service.com), in our scenario, would be by analyzing a claim that always comes with the bearer token (which we already do at the APIM level 调用单个端点)
有没有人需要这样做?提前致谢!
APIM 策略表达式和 "choose" 策略允许您创建任意处理逻辑:https://docs.microsoft.com/en-us/azure/api-management/api-management-policy-expressions.
可以访问 JWT
context.Request.Headers.GetValueOrDefault("Authorization").AsJwt()
它 returns Jwt 对象(在上面的同一页面上查找它的属性)。
所有这些加上 "set-backend-service" 政策应该足以完成这项工作。
Vitaly 的回答是解决这个问题的关键。这是完整的答案,以防有人在寻找同样的东西。
<policies>
<inbound>
<!-- Extract Token from Authorization header parameter -->
<set-variable name="token" value="@(context.Request.Headers.GetValueOrDefault("Authorization",string.Empty).Split(' ').Last().AsJwt())" />
<choose>
<when condition="@(context.Variables["token"] != null)">
<set-variable name="api_uri" value="@(((Jwt)context.Variables["token"]).Claims.GetValueOrDefault("api_uri", string.Empty))" />
<choose>
<when condition="@(context.Variables["api_uri"] != string.Empty)">
<set-backend-service base-url="@((string)context.Variables["api_uri"])" />
</when>
<otherwise />
</choose>
</when>
<otherwise />
</choose>
<base />
</inbound>
</policies>