我想把从服务器上得到的cookies保存在客户端上。在firefox上一切正常,但chrome仍然显示“此设置cookie被阻止,因为其域属性对于当前主机url无效”
我已经读了很多教程,但我还是搞不懂。
它在localhost上运行良好,但是当我尝试在heroku上运行它时,我在chrome上收到这个警告,并且无法保存cookie。
我在heroku上创建了测试项目,客户端位于“https://cookietestclient.herokuapp.com,服务器位于https://cookieservertest.herokuapp.com/"
这是我的js获取代码:
function addCookie (){
fetch("https://cookieservertest.herokuapp.com/api/v1/test",{
method:"POST",
credentials:"include",
mode:"cors"
})
.then(res=>res.json())
.then(data=>{
console.log(data)
})
}
和Spring启动代码:
@RestController
@RequestMapping(path = "api/v1/test")
public class BasicReturnCookie {
@PostMapping
@CrossOrigin(origins = "https://cookietestclient.herokuapp.com", allowCredentials = "true",
allowedHeaders = "https://cookietestclient.herokuapp.com", maxAge = 3600)
public void getMapping(HttpServletResponse response){
ResponseCookie cookie = ResponseCookie.from("userName", "Jarek")
.domain("/")
.sameSite("None")
.build();
response.addHeader(HttpHeaders.SET_COOKIE, cookie.toString());
}
}
1条答案
按热度按时间tyg4sfes1#
samesite=none的cookies现在还必须指定secure属性(它们需要安全上下文/https)。