如何在Mule上添加多个cookie?

时间:2016-01-13 13:44:24

标签: cookies mule

我能够通过Mule ESB在请求的响应中添加1个cookie。但是,当我尝试使用属性“Set-Cookie”添加多个cookie时,Mule会覆盖第一个cookie并返回第二个cookie。

例如,在下面的代码段中,我在邮件属性(这是一张地图)上添加了2个Cookie。

`String msg =  "SSID=123;domain=localhost;Path=/";
 String msg2 =  "SSAPID=456;domain=localhost;Path=/";
 message.setProperty(HttpHeaders.Names.SET_COOKIE,msg, PropertyScope.OUTBOUND);
 message.setProperty(HttpHeaders.Names.SET_COOKIE,msg2, PropertyScope.OUTBOUND);
`

当我收到回复时,只有一个cookie:

  Message properties:
  OUTBOUND scoped properties:
    Set-Cookie=SAPID=456
    access-control-allow-credentials=true
    access-control-allow-headers=Origin, X-Requested-With, Content-Type, Accept, Cache, X-Auth-Token
    access-control-allow-methods=PATCH, PUT, POST, GET, OPTIONS, DELETE
    access-control-allow-origin=*
    access-control-max-age=10
    cache-control=no-store
    content-encoding=gzip
    content-type=application/json;charset=UTF-8
    date=Wed, 13 Jan 2016 12:26:58 GMT
    http.reason=OK
    http.status=200
    pragma=no-cache
    server=Apache-Coyote/1.1
    transfer-encoding=chunked
    vary=[Accept-Encoding, Accept-Encoding]
    x-content-type-options=nosniff
    x-xss-protections=1; mode=block
  SESSION scoped properties:

设置-曲奇 = SAPID = 456

如您所见,请求中仅返回了第二个cookie。

如何实现我想要做的事情,因为此处描述的cookie规范https://tools.ietf.org/html/rfc6265#section-3.1表示我可以在响应标头上返回多个Set-Cookie。

  

服务器可以存储会话      标识符以及用户首选语言返回两个      Set-Cookie标头字段

1 个答案:

答案 0 :(得分:5)

如果您设置两次相同的属性,它将覆盖它。而是添加属性一次,但将值设置为列表:

List<String> cookies = new ArrayList<String>();
cookies.add(msg1);
cookies.add(msg2);

 message.setProperty(HttpHeaders.Names.SET_COOKIE,cookies, PropertyScope.OUTBOUND);

返回:

curl http://localhost:8087/test -i
HTTP/1.1 200
Content-Length: 3
Set-Cookie: SSAPID=456;domain=localhost;Path=/
Set-Cookie: SSID=123;domain=localhost;Path=/
Date: Wed, 13 Jan 2016 15:09:54 GMT