WSO2 ESB Mediation 在序列中检索用户属性(声明)

WSO2 ESB Mediation Retrieve user attributes (claims) in inSequence

我已经在 API Publisher 中发布了 API。 API 有 POST 方法 confirm,它使用以下参数检索 JSON 数据:userUUID、appName、version。在 API Publisher 中,这个 API 只需要两个参数:appName 和 version。

我不想从客户端发送 userUUID,但我想从 inSequence 中的 accessToken(它在用户声明中)检索 userUUID,并将其作为新参数添加到 sended JSON,然后发送它全部到后端。

可能吗?也许我至少可以从 accessToken 检索用户电子邮件?

我看到两种将用户信息传递到后端的方法。

  • 一个是智威汤逊令牌。在 api-manager.xml 中,您可以使用声明检索器启用 JWT 令牌生成。 JWT 令牌将作为 HTTP header

  • 发送到后端服务
  • 在序列中,您可以调用其中一项管理服务(请参阅 https://docs.wso2.com/display/AM210/WSO2+Admin+Services)以获取分配的用户和应用程序

参见 https://localhost:9443/services/OAuth2TokenValidationService?wsdlvalidatebuildIntrospectionResponse 操作

希望对您有所帮助

我找到了从 https://localhost:9443/oauth2/userinfo?schema=openid

获取用户信息的解决方法

首先,在文件 [WSO2_AM]/repository/conf/api-manager.xml

中更改 OAuthConfigurations 中的值 RemoveOAuthHeadersFromOutMessage

其次,来自 https://localhost:9443/oauth2/userinfo?schema=openid 的用户声明应该在 WSO2 API Service Providers 中的 Manager Carbon Server 中配置。

算法:

  1. 将请求正文复制到 属性 body_of_zero_call
  2. 将请求目标 REST API 方法复制到 属性 urlPostfixZero
  3. 将值 ?schema=openid 设置为请求目标 REST API 方法
  4. 调用https://localhost:9443/oauth2/userinfo?schema=openid获取用户信息
  5. 检查响应代码:如果是 200,则通过,否则 return 代码 500 和消息 { "status": "Can't get user info"}
  6. 将有趣的信息(在我的例子中是 user_uuid)从响应正文复制到 属性 user_uuid_first_call
  7. 将源请求正文从 属性 body_of_zero_call 复制到正文
  8. 将源请求目标 REST API 方法从 属性 urlPostfixZero 复制到请求目标 REST API 方法
  9. 将元素 userUUID 添加到请求正文
  10. 用 属性 user_uuid_first_call
  11. 中的值填充正文中的元素 userUUID
  12. 使用更改的主体和目标 REST API 方法调用目标 URL
  13. 回复

调解员:

<?xml version="1.0" encoding="UTF-8"?>
<sequence xmlns="http://ws.apache.org/ns/synapse" name="token_to_user_uuid" trace="disable">
   <!-- 1 -->
   <enrich>
      <source clone="true" type="body" />
      <target action="child" property="body_of_zero_call" type="property" />
   </enrich>
   <!-- 2 -->
   <property expression="$axis2:REST_URL_POSTFIX" name="urlPostfixZero" scope="default" type="STRING" />
   <!-- 3 -->
   <property name="REST_URL_POSTFIX" scope="axis2" type="STRING" value="?schema=openid" />
   <!-- 4 -->
   <call blocking="true">
      <endpoint>
         <http method="get" trace="disable" uri-template="https://localhost:9443/oauth2/userinfo" />
      </endpoint>
   </call>
   <!-- 5 -->
   <filter regex="200" source="get-property('axis2', 'HTTP_SC')">
      <then>
         <!-- 6 -->
         <property expression="$body//jsonObject//user_uuid" name="user_uuid_first_call" scope="default" type="STRING" />
         <!-- 7 -->
         <enrich>
            <source clone="true" property="body_of_zero_call" type="property" />
            <target type="body" />
         </enrich>
         <!-- 8 -->
         <property expression="get-property('urlPostfixZero')" name="REST_URL_POSTFIX" scope="axis2" type="STRING" />
         <!-- 9 -->
         <enrich>
            <source clone="true" type="inline">
               <userUUID xmlns="" />
            </source>
            <target action="child" xpath="$body//jsonObject" />
         </enrich>
         <!-- 10 -->
         <enrich>
            <source clone="true" property="user_uuid_first_call" type="property" />
            <target xpath="$body//jsonObject//userUUID" />
         </enrich>
         <!-- 11 -->
         <call blocking="true">
            <endpoint>
               <http method="post" trace="disable" uri-template="https://localhost:9444/customAuth/services/regulations" />
            </endpoint>
         </call>
         <!-- 12 -->
         <respond />
      </then>
      <else>
         <property name="HTTP_SC" scope="axis2" type="STRING" value="500" />
         <payloadFactory media-type="json">
            <format>{ "status": "Can't get user info"}</format>
            <args />
         </payloadFactory>
         <respond />
      </else>
   </filter>
</sequence>