升级到Apache HttpClient 4.4后,它不会发送带有请求的cookie

Col*_*edy 4 java cookies apache-httpclient-4.x

我正在使用Apache HttpClient向我们的内部API服务器发送请求.服务器需要身份验证,并且需要使用身份验证令牌设置cookie.

直到HttpClient 4.3.6这已经工作正常,但在4.4及以上它已停止发送请求cookie.我的cookie域设置为.subdomain.mycompany.com,适用于4.3.6,但不适用于4.4及更高版本.如果我更具体,并将完整主机作为cookie域,即host.subdomain.mycompany.com,它可以工作,但这不是一个解决方案.

这是一个类似于我正在做的代码片段:

public CloseableHttpResponse execute(CloseableHttpClient httpClient) throws IOException {
    BasicClientCookie cookie = new BasicClientCookie("cookieName", "myAuthtoken");
    cookie.setPath("/");
    cookie.setDomain(".subdomain.mycompany.com");
    cookie.setSecure(false);
    HttpContext localContext = new BasicHttpContext(parentContext);
    CookieStore cookieStore = new BasicCookieStore();
    cookieStore.addCookie(cookie);
    localContext.setAttribute(HttpClientContext.COOKIE_STORE, cookieStore);
    return httpClient.execute(target, request, localContext);
}
Run Code Online (Sandbox Code Playgroud)

httpClient已经构造并传递到此代码中,该代码设置了auth cookie.

我看到了这个,类似的Cookie在Apache httpclient 4.4中被忽略了,但在我的情况下,cookie没有被发送到服务器.

打开HttpClient中的线路登录后,我可以在4.3.6中看到以下内容,但不会在4.4及以上版本中看到:

DEBUG [org.apache.http.client.protocol.RequestAddCookies] Cookie [version: 0][name: cookieName][value: authToken][domain: .subdomain.mycompany.com][path: /][expiry: Wed Jul 15 16:07:05 IST 2015] match [host.subdomain.mycompany.com:80/myApi]
Run Code Online (Sandbox Code Playgroud)

这让我觉得这与cookie域匹配有关.有人有主意吗?谢谢.

Ant*_*nev 6

我已经调试了示例代码.问题在于,BasicDomainHandler.match(Cookie, CookieOrigin) line: 129它需要设置org.apache.http.cookie.ClientCookie.DOMAIN_ATTR,以便将完整的主机名从URL匹配到cookie域.因此,在设置域后,需要在代码中添加以下行:

cookie.setAttribute(ClientCookie.DOMAIN_ATTR, "true");
Run Code Online (Sandbox Code Playgroud)

该更改在12/19/14,10:59 PM添加了修订版1646864:

符合RFC 6265的cookie规范