是否可以根据特定的JWT声明将请求转发到区域API?
我正在处理的平台每个区域有一个API,我们的客户需要知道它才能构建基本请求URL - 例如:https:// {region} .service.com
不幸的是,在我们的场景中,尝试找出从Azure APIM自动调用哪个区域api(例如:在https://api.service.com调用单个端点)的唯一可靠方法是分析一个始终伴随着不记名令牌的声明(我们已经做了at the APIM level。)
有没有人需要这样做?提前致谢!
APIM策略表达式和“选择”策略允许您创建任意处理逻辑:https://docs.microsoft.com/en-us/azure/api-management/api-management-policy-expressions。
可以访问JWT
context.Request.Headers.GetValueOrDefault("Authorization").AsJwt()
它返回Jwt对象(在上面的同一页面上查找它的属性)。
所有这些与“后端服务”政策相结合应该足以完成这项工作。
维塔利的答案是解决这个问题的关键。这是完整的答案,以防有人在寻找同样的事情。
<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>