后台是Spring boots。我在那里设置了cookie,它在postman上工作得很好。
@CrossOrigin(origins = "http://localhost:3000", allowCredentials = "true")
//Spring boots (http://localhost:8080)
Cookie cookie = new Cookie("access-token",token);
cookie.setPath("/");
cookie.setSecure(false);
response.addCookie(cookie);
response.addHeader("Access-Control-Allow-Credentials", "true");
前端是ReactJS,我使用Axios的POST方法来获取登录信息。我得到了回应,但没有设置cookies
//React JS (http://localhost:3000)
axios({
url: common.url + '/login',
data: "userId=" + userid+ "&password=" + password,
method: 'post',
cache: false,
contentType: 'application/x-www-form-urlencoded',
headers: {
'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8', 'Cache-Control': 'no-cache','withCredentials':'true'
}
}).then(function (result) {
;
//Success Login
})
尝试了很多方法。没有用
StackOverflow:java - Why Cookies are not set in the browser but works in Postman? - Stack Overflow