问题描述
我正在调用具有凭据的身份验证服务,并且该服务会使用身份验证信息响应Cookie.我需要该信息才能访问Web服务.
I'm calling an authentication service with credentials and it responses back cookies with authentication info. I need that info for accessing a Web Service.
问题是身份验证服务返回的响应包含多个cookie,但是我只能从响应中访问第一个cookie,这是访问Web服务所需的第二个cookie(WSL-external = VhTee1 ...).
The problem is that the authentication service returns a response with multiple cookies but I can only access to the first cookie from response it's the second cookie (WSL-external=VhTee1...) that I need for accessing the Web Service.
我从身份验证服务器获得的响应:
The response I'm getting from the authentication server:
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}&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调解人:
I have also tried to make my own Class mediator:
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;
}
}
并从这样的序列中调用它:
And calling it from the sequence like this:
<class name="org.wso2.mediator.CookieMediator"/>
但是它也只返回第一个cookie:
But it also returns only the first cookie:
Cookie: JSESSIONID=000036_xxxxxxx_xxxxxx_xxxxxx:xxxxxxxxx; Path=/
我已经阅读了这些帖子,但是对于我遇到的第二个cookie问题没有帮助:
I have already read these posts but it doesn't help with the 2nd cookie problem I'm having:
在WSO2 ESB中,如何存储cookie并在以后用于身份验证?
谢谢.
更新:
我的解决方案如下:
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地图中获取.
You may need to get this from the EXCESS_TRANSPORT_HEADERS map.
Map excessHeaders = (Map) synCtx.getProperty(NhttpConstants.EXCESS_TRANSPORT_HEADERS);
更具体地说,这是一个包含多值标头的MultiValueMap
.
more specifically this is a MultiValueMap
that contains multi-valued headers.
这篇关于WSO2 ESB从具有多个cookie的传输头中获取一个cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!