WSO2 ESB 使用多个 cookie 从传输 header 获取 cookie

WSO2 ESB Getting a cookie from transport header with multiple cookies

我正在使用凭据调用身份验证服务,它使用身份验证信息响应 cookie。我需要该信息来访问 Web 服务。

问题是身份验证服务 returns 具有多个 cookie 的响应,但我只能从响应访问第一个 cookie,这是第二个 cookie (WSL-external=VhTee1...)我需要访问 Web 服务。

我从身份验证服务器获得的响应:

HTTP/1.1 200 OK
Content-Language: en-US
Expires: Thu, 01 Jan 1970 00:00:00 GMT
Set-Cookie: JSESSIONID=000036_xxxxxxx_xxxxxx_xxxxxx:xxxxxxxxx; Path=/
Set-Cookie: WSL-external=VhTee1YVaxsBANcABHVzZXJpZD11d2lzaABpcGFkZHI9Ny43LjcuNwBhY2lncm91cD1SSVAAZGVwdD02NzIwAG9yZ2NvZGU9PwBlbXBjb2RlPUEAbXJyb2xlPU4Ab3JnPVBBRwBjb21wYW55PT8AZGl2YWJicj1HRwBzaXRlY29kZT03MDAzAGNpdHk9Y2l0eQBzdGF0ZT0/AGNvdW50cnk9R0cAc3ViamVjdGlkPXV3aXNoQGdnLmNvbQAAAENOPXdzbC1leHRlcm5hbABqdXN0aW5jYXNlaWZ0aGlzY29pbnRhaW5zc29tZXNlbnRpdml0ZWRhdGFpbW5vdGdpdmluZ2l0dG95b3U=; Path=/; Domain=.xxx.xxx
Content-Type: text/html; charset=UTF-8; charset=UTF-8
Pragma: no-cache
Cache-Control: no-cache
Date: Wed, 07 Oct 2015 08:58:36 GMT
Transfer-Encoding: chunked
Connection: Keep-Alive

ESB 序列:

<?xml version="1.0" encoding="UTF-8"?>
<proxy xmlns="http://ws.apache.org/ns/synapse"
       name="TestLogProxy"
       transports="https,http"
       statistics="disable"
       trace="disable"
       startOnLoad="true">
   <target>
      <inSequence>
         <property name="uri.var.userid" value="userid"/>
         <property name="uri.var.password" value="password"/>
         <send>
            <endpoint>
               <http method="GET"
                     uri-template="https://www.company.biz/auth.cgi?userid={uri.var.userid}&amp;password={uri.var.password}"/>
            </endpoint>
         </send>
      </inSequence>
      <outSequence>
         <property name="setCookieHeader"
                   expression="$trp:Set-Cookie"
                   scope="default"
                   type="STRING"/>
         <log level="custom">
            <property name="setCookieHeader value" expression="$ctx:setCookieHeader"/>
         </log>
         <send/>
      </outSequence>
   </target>
   <description/>
</proxy>

我收到的日志消息:

setCookieHeader value = JSESSIONID=000036_xxxxxxx_xxxxxx_xxxxxx:xxxxxxxxx; Path=/

我也试过自己做Class调解员:

package org.wso2.mediator;

import java.util.Map;

import org.apache.synapse.MessageContext; 
import org.apache.synapse.core.axis2.Axis2MessageContext;
import org.apache.synapse.mediators.AbstractMediator;

public class CookieMediator extends AbstractMediator { 

    public boolean mediate(MessageContext synCtx) { 
        try {
            System.out.println("CookieMediator doing stuff...");

            // Extracting transport headers
            org.apache.axis2.context.MessageContext msgContext = ((Axis2MessageContext) synCtx).getAxis2MessageContext();

            Map headersMap = (Map) msgContext.getProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS);

            // Printing the cookie
            System.out.println("Cookie: " +  headersMap.get("Set-Cookie"));

          } catch (Exception e) {
            System.out.println("Exception: " + e);
            handleException("Exception", e, synCtx);
          }

        return true;
    }
}

并按如下顺序调用它:

<class name="org.wso2.mediator.CookieMediator"/>

但它也 returns 只有第一个 cookie:

Cookie: JSESSIONID=000036_xxxxxxx_xxxxxx_xxxxxx:xxxxxxxxx; Path=/

我已经阅读了这些帖子,但这对我遇到的第二个 cookie 问题没有帮助:

WSO2 ESB - How to get and set cookies in WSDL calls in

谢谢。

更新:

我的解决方案如下:

package org.wso2.mediator;

import java.util.Map;
import org.apache.synapse.MessageContext; 
import org.apache.synapse.core.axis2.Axis2MessageContext;
import org.apache.synapse.mediators.AbstractMediator;

public class CookieMediator extends AbstractMediator { 

    public boolean mediate(MessageContext synCtx) { 
        try {
            System.out.println("CookieMediator extracting cookie...");

            // Extracting cookie from excess headers
            org.apache.axis2.context.MessageContext msgContext = ((Axis2MessageContext) synCtx).getAxis2MessageContext();
            Map excessHeaders = (Map) msgContext.getProperty("EXCESS_TRANSPORT_HEADERS");

            if (excessHeaders != null) {
                String cookie = excessHeaders.get("Set-Cookie").toString().split(";")[0];

                if (cookie.startsWith("[WSL-external")) {
                    System.out.println("Cookie: " + cookie.substring(1));
                }
            }
          } catch (Exception e) {
            System.out.println("Exception: " + e);
            handleException("Exception", e, synCtx);
          }

        return true;
    }
}

您可能需要从 EXCESS_TRANSPORT_HEADERS 地图中获取。

Map excessHeaders = (Map) synCtx.getProperty(NhttpConstants.EXCESS_TRANSPORT_HEADERS);

更具体地说,这是一个包含 multi-valued headers.

MultiValueMap