【问题标题】:Why the cookie details returned by @CookieValue is null?为什么@CookieValue 返回的 cookie 详细信息为空?
【发布时间】:2019-03-17 13:56:09
【问题描述】:

我有一个控制器,我试图在其中使用

@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)是正确的,其他细节不正确。为什么会有这样的行为?

【问题讨论】:

    标签: spring spring-mvc httpcookie


    【解决方案1】:

    因为浏览器使用 Set-Cookie 标头从 response 中设置的 cookie 接收所有这些信息,但它只将 cookie value 发送到Cookie 标头。

    请参阅 https://en.wikipedia.org/wiki/HTTP_cookiehttps://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cookie

    注意,顺便说一句,注释被命名为CookieValue。这个名字是有原因的。

    【讨论】:

    • “注释被命名为 CookieValue。这个名字是有原因的”。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-12
    • 2013-07-24
    • 2020-09-18
    相关资源
    最近更新 更多