Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring boot 我能';在我的客户端站点上找不到我的Spring boot httpOnly Cookie_Spring Boot - Fatal编程技术网

Spring boot 我能';在我的客户端站点上找不到我的Spring boot httpOnly Cookie

Spring boot 我能';在我的客户端站点上找不到我的Spring boot httpOnly Cookie,spring-boot,Spring Boot,我正在使用SpringBoot2.3.1.RELEASE并试图向客户端发送一个httpOnly Cookie。 问题是Chrome没有显示任何cookie。 邮递员也是这样。 一件事是,在邮递员响应日志中,我可以看到标题Set Cookie及其值,而在Chrome上,console.log(res.headers)给出未定义的 这是我的服务器代码 public @ResponseBody ResponseEntity<?> sendCookie(HttpServletRequest

我正在使用SpringBoot2.3.1.RELEASE并试图向客户端发送一个httpOnly Cookie。 问题是Chrome没有显示任何cookie。 邮递员也是这样。 一件事是,在邮递员响应日志中,我可以看到标题
Set Cookie
及其值,而在Chrome上,
console.log(res.headers)给出
未定义的

这是我的服务器代码

public @ResponseBody ResponseEntity<?> sendCookie(HttpServletRequest request,
    HttpServletResponse response) {

    Cookie cookie = new Cookie("cookie", "one_cookie_for_you");

    cookie.setMaxAge(7 * 24 * 60 * 60); // expires in 7 days
    cookie.setSecure(true);
    cookie.setHttpOnly(true);

    response.addCookie(cookie);

    return ResponseEntity.ok("OK");
}
public@ResponseBody ResponseEntity sendCookie(HttpServletRequest请求,
HttpServletResponse(响应){
Cookie Cookie=新的Cookie(“Cookie”,“一块给你的Cookie”);
cookie.setMaxAge(7*24*60*60);//7天后过期
cookie.setSecure(true);
cookie.setHttpOnly(true);
addCookie(cookie);
返回响应。ok(“ok”);
}
我做错什么了吗?谢谢你的帮助。

多亏了@M.Deinum,我才解决了这个问题,原来是因为我缺乏知识。再次感谢你,这是解决办法

由于我的客户端位于非https的localhost上,因此cookie的安全标志必须设置为
false
like
cookie.setSecure(false)
这样即使协议不安全,浏览器也会存储cookie


其次,我的代码缺少
cookie.setPath(“”)
cookie.setDomain(“”
,使Spring自动将cookie的路径设置为api的映射。将其域名和路径设置为
“/”
会使cookie在任何请求中发送。

cookie没有附加到任何主机/url(至少看起来是这样的)。通过javascript访问cookie也不可能,因为它是HttpOnly(这是HttpOnly标志的全部要点)。谢谢。这帮我解决了问题。我会根据你的帮助给出答案。