【问题标题】:Access a web-service with session-key/token from WSO2 ESB使用来自 WSO2 ESB 的会话密钥/令牌访问 Web 服务
【发布时间】:2016-06-06 17:18:31
【问题描述】:

我开始评估 WSO2 ESB 并尝试实现一些简单但真实的场景。

在这种特定情况下,我正在尝试做的是连接到使用有效负载中的会话密钥进行身份验证的 Web 服务。因此,有一个带有用户名和密码的 Web 服务调用来获取密钥,然后我需要将其放入第二个 Web 服务调用的有效负载中,以实际从服务中检索数据。

出于性能原因,我不想在每次服务调用时都登录,而是登录一次,将密钥存储一段时间并执行几个请求。

我要调用的服务是 SugarCRM 网络服务。

登录信息是这样的:

<soapenv:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:sug="http://www.sugarcrm.com/sugarcrm" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">
<soapenv:Header/>
<soapenv:Body>
   <sug:login soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
      <user_auth xsi:type="sug:user_auth">
         <user_name xsi:type="xsd:string">interface</user_name>
         <password xsi:type="xsd:string">MD5HASHOFPASSWORD</password>
      </user_auth>
      <application_name xsd:string">dummy</application_name>
      <name_value_list xsi:type="sug:name_value_list" soapenc:arrayType="sug:name_value[]"/>
   </sug:login>
</soapenv:Body>

返回如下内容:

<SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:tns="http://www.sugarcrm.com/sugarcrm">
   <SOAP-ENV:Body>
       <ns1:loginResponse xmlns:ns1="http://www.sugarcrm.com/sugarcrm">
          <return xsi:type="tns:entry_value">
              <id xsi:type="xsd:string">loggfi0i3j6eeugs7l0a0m2587</id>
          </return>
      </ns1:loginResponse>
   </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

然后我需要使用“id”字段作为新请求的令牌。

例如,请求所有潜在客户(“id”放入“会话”字段):

<soapenv:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:sug="http://www.sugarcrm.com/sugarcrm" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">
   <soapenv:Header/>
   <soapenv:Body>
      <sug:get_entry_list soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
         <session xsi:type="xsd:string">loggfi0i3j6eeugs7l0a0m2587</session>
         <module_name xsi:type="xsd:string">Leads</module_name>
      </sug:get_entry_list>
   </soapenv:Body>
</soapenv:Envelope>

此请求然后返回数据库中的所有潜在客户。

这对于访问使用 OAuth2 的 Web 服务(例如 MS Dynamics CRM 的 REST-API)同样是必需的,只有这样我才需要将令牌/承载放在标头中。

我将如何在 WSO2 ESB 中实现上述场景?我确信这很常见,但我还没有找到它的文档或示例。

以下是从 CRM 获取会话 ID 的顺序:

<?xml version="1.0" encoding="UTF-8"?>
<sequence name="sugar_login" trace="enable" xmlns="http://ws.apache.org/ns/synapse">
    <payloadFactory media-type="xml">
        <format>
            <sug:login xmlns:sug="http://www.sugarcrm.com/sugarcrm">
                <sug:user_auth>
                    <user_name>interface</user_name>
                    <password>MD5HASHOFPASSWORD</password>
                    <version>1</version>
                </sug:user_auth>
                <application_name>dummy</application_name>
            </sug:login>
        </format>
    </payloadFactory>
    <header name="To" value="http://my.sugarcrm.com:9090/service/v4_1/soap.php"/>
    <call>
        <endpoint name="templEPTimeout" template="org.wso2.carbon.connector.sugarcrm.timeout">
            <axis2ns653:parameter name="timoutDuration" value="6000" xmlns:axis2ns653="http://ws.apache.org/ns/synapse"/>
            <axis2ns654:parameter name="maximumDuration" value="3000" xmlns:axis2ns654="http://ws.apache.org/ns/synapse"/>
            <axis2ns655:parameter name="progressAFactor" value="2.0" xmlns:axis2ns655="http://ws.apache.org/ns/synapse"/>
            <axis2ns656:parameter name="initialDuration" value="2000" xmlns:axis2ns656="http://ws.apache.org/ns/synapse"/>
        </endpoint>
    </call>
    <!-- Remove response custom header information -->
    <header action="remove" name="X-SOAP-Server" scope="transport"/>
    <header action="remove" name="Cache-control" scope="transport"/>
    <header action="remove" name="Vary" scope="transport"/>
    <header action="remove" name="Expires" scope="transport"/>
    <header action="remove" name="Set-Cookie" scope="transport"/>
    <header action="remove" name="path" scope="transport"/>
    <property expression="//ns1:loginResponse/return/id"
        name="sugarsessionid" scope="default" type="STRING" xmlns:ns="http://org.apache.synapse/xsd"/>
</sequence>

【问题讨论】:

    标签: rest soap wso2esb synapse


    【解决方案1】:

    您可以使用 SugarCRM 连接器来连接 SugarCRM API。您可以在WSO2 Store 找到sugarcrm 连接器。在documentation 中查找更多详细信息。

    【讨论】:

    • 我当时试了一下,还是不行。不记得到底是什么问题,现在它已经过时了。
    猜你喜欢
    • 1970-01-01
    • 2016-06-10
    • 1970-01-01
    • 2011-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多