HttpOnly cookie不是通过请求发送的

时间:2017-02-03 06:41:23

标签: java angular cookies jwt cookie-httponly

我想使用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 cookie必须由浏览器在每次请求(JavaScript无权访问这些cookie)时发送到定义的路径。

我有以下请求标题:

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

以及以下响应标头:

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: trueX-Requested-With:XMLHttpRequest作为请求标头。

它是跨域。

1 个答案:

答案 0 :(得分:4)

是的,你正确拥有cookie,你的浏览器应该在没有过期时自动发送cookie,而httpOnly标志意味着它无法通过JavaScript访问或操作。

然而

您需要确保您发送的Cookie不是跨域,如果您需要跨域,则需要以不同方式处理它。