为什么@CookieValue返回的cookie详情为null?
Why the cookie details returned by @CookieValue is null?
我有一个控制器,我试图在其中使用
获取 cookie 的值
@PostMapping(SIGNOUT)
@ResponseStatus(value=HttpStatus.OK)
public void signoutUser(@CookieValue(name="acctk") Cookie cookie ,final HttpServletRequest request, final HttpServletResponse response) {
System.out.println("value: " + cookie.getValue());
System.out.println("path: " + cookie.getPath());
System.out.println("domain: " + cookie.getDomain());
System.out.println("max-age: " + cookie.getMaxAge());
System.out.println("is secure: " + cookie.getSecure());
}
控制器正在返回:
value: 3C6E523D68F35294D3D6AC099CDA60EB
path: null
domain: null
max-age: -1
is secure: false
随请求发送的 cookie:
acctk=3C6E523D68F35294D3D6AC099CDA60EB; Max-Age=2592000; Expires=Tue, 16-Apr-2019 13:52:12 GMT; Path=/api/v1; HttpOnly
因此,根据结果,只有 cookie 值(和 secure
)是正确的,其他细节不正确。为什么会有这样的行为?
因为浏览器使用 Set-Cookie
header 从 响应 中设置的 cookie 接收所有这些信息,但它只发送 cookie 值到服务器中Cookie
header。
参见https://en.wikipedia.org/wiki/HTTP_cookie, and https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cookie。
注意,顺便说一句,注释的名称是 CookieValue
。这个名字是有原因的。
我有一个控制器,我试图在其中使用
获取 cookie 的值@PostMapping(SIGNOUT)
@ResponseStatus(value=HttpStatus.OK)
public void signoutUser(@CookieValue(name="acctk") Cookie cookie ,final HttpServletRequest request, final HttpServletResponse response) {
System.out.println("value: " + cookie.getValue());
System.out.println("path: " + cookie.getPath());
System.out.println("domain: " + cookie.getDomain());
System.out.println("max-age: " + cookie.getMaxAge());
System.out.println("is secure: " + cookie.getSecure());
}
控制器正在返回:
value: 3C6E523D68F35294D3D6AC099CDA60EB
path: null
domain: null
max-age: -1
is secure: false
随请求发送的 cookie:
acctk=3C6E523D68F35294D3D6AC099CDA60EB; Max-Age=2592000; Expires=Tue, 16-Apr-2019 13:52:12 GMT; Path=/api/v1; HttpOnly
因此,根据结果,只有 cookie 值(和 secure
)是正确的,其他细节不正确。为什么会有这样的行为?
因为浏览器使用 Set-Cookie
header 从 响应 中设置的 cookie 接收所有这些信息,但它只发送 cookie 值到服务器中Cookie
header。
参见https://en.wikipedia.org/wiki/HTTP_cookie, and https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cookie。
注意,顺便说一句,注释的名称是 CookieValue
。这个名字是有原因的。