请求未发送 HttpOnly cookie

HttpOnly cookies not sent by request

我想使用 HttpOnly cookie,我在 Java 中设置如下:

...

Cookie accessTokenCookie = new Cookie("token", userToken);
accessTokenCookie.setHttpOnly(true);
accessTokenCookie.setSecure(true);
accessTokenCookie.setPath("/");
response.addCookie(accessTokenCookie);
Cookie refreshTokenCookie = new Cookie("refreshToken", refreshToken);
refreshTokenCookie.setHttpOnly(true);
refreshTokenCookie.setSecure(true);
refreshTokenCookie.setPath("/");
response.addCookie(refreshTokenCookie);

...

我在客户端收到了带有 cookie 的响应,但是当我发送下一个请求时,请求中没有 cookie。也许我错过了一些东西,但据我所知,这些 HttpOnly cookies 必须由浏览器发送回每个请求(Java脚本无法访问这些 cookies)到达定义的路径。

我有以下请求 Headers:

Accept:application/json, text/plain, */*
Accept-Encoding:gzip, deflate, br
Accept-Language:en-US,en;q=0.8,hu;q=0.6,ro;q=0.4,fr;q=0.2,de;q=0.2
Authorization:Basic dXNlcm5hbWU6cGFzc3dvcmQ=
Connection:keep-alive
Content-Length:35
content-type:text/plain
Host:localhost:8080
Origin:http://localhost:4200
Referer:http://localhost:4200/
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.95 Safari/537.36
X-Requested-With:XMLHttpRequest

和以下响应 headers:

Access-Control-Allow-Credentials:true
Access-Control-Allow-Origin:http://localhost:4200
Access-Control-Expose-Headers:Access-Control-Allow-Origin, Content-Type, Date, Link, Server, X-Application-Context, X-Total-Count
Cache-Control:no-cache, no-store, max-age=0, must-revalidate
Content-Length:482
Content-Type:application/json;charset=ISO-8859-1
Date:Fri, 03 Feb 2017 13:11:29 GMT
Expires:0
Pragma:no-cache
Set-Cookie:token=eyJhbGciO;Max-Age=10000;path=/;Secure;HttpOnly
Set-Cookie:refreshToken=eyJhb8w;Max-Age=10000;path=/;Secure;HttpOnly
Vary:Origin

同样在客户端,我在 Angular2 中使用 withCredentials: true 并在请求中使用 X-Requested-With:XMLHttpRequest header。

而且是跨域

是的,您的 cookie 是正确的,您的浏览器应该在 cookie 未过期时自动发送 cookie,并且 httpOnly 标志意味着无法通过 JavaScript.

访问或操作它

不过

您需要确保您发送的 cookie 不是跨域的,如果您需要跨域,则需要进行不同的处理。