gpt4 book ai didi

java - "Refused to set unsafe header ' Cookie ' "同时以 Angular 6 发送带有 GET 请求的 cookie

转载 作者:太空狗 更新时间:2023-10-29 18:22:15 26 4
gpt4 key购买 nike

我正在使用 spring security 和 angular 6。当我使用基本 Auth 服务器登录时,服务器会发送一个带有响应的 cookie JessionID。我想将此 cookie 与其他请求一起发送以进行身份​​验证,但它给了我错误 Refused to set unsafe header 'Cookie' 。当我在 header 中使用相同的 cookie 从 postman 访问相同的端点时,它会起作用。

下面是angular的方法:

注意:目前我是手动添加标题。

private heroesUrl = 'http://localhost:8080/hi';
header = new HttpHeaders().set("Cookie", "JSESSIONID=A2A75EC9A3E1172D60060C6E708549B5");
getMessage() :Observable<Message>{
return this.http.get<Message>(this.heroesUrl,{headers:this.header});
}

使用基本身份验证登录时得到的响应

Response which i get when i login using basic Auth

最佳答案

你不能这样做,因为浏览器不允许你这样做。让我在这里描述问题:

你有没有注意到 Set-Cookie: JSESSIONID=......; Path=/; HttpOnly在您的响应标题中?嗯,问题是 HttpOnly 标志。实际上 :) 这不是问题,它是一种防止旨在窃取浏览器 cookie 的攻击的功能:

HttpOnly is a flag added to cookies that tell the browser not to display the cookie through client-side scripts (document.cookie and others). ... When you set a cookie with the HttpOnly flag, it informs the browser that this special cookie should only be accessed by the server

所以浏览器不允许任何javascript代码访问这个变量。如果您可以更改该值,那么它就不是 HttpOnly标记的 cookie 了:)

如果您想通过 javascript 发送此 cookie,您应该通过 Authorization 发送它例如 header 并在 Java 服务器中编写中间件,以便它从 Authorization 中捕获这些值 header 并将它们视为 JSESSIONID cookie。您没有更多选择:)

关于java - "Refused to set unsafe header ' Cookie ' "同时以 Angular 6 发送带有 GET 请求的 cookie,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54058890/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com