如何在HTTP协议中传递cookie ?

我知道基于cookie的身份验证。SSL和HttpOnly标志可以应用于保护基于cookie的身份验证不受MITM和XSS的影响。然而,为了保护它不受CSRF的影响,还需要采取更多的特殊措施。它们只是有点复杂。(参考)

最近,我发现JSON Web Token (JWT)作为一种身份验证解决方案非常热门。我了解编码、解码和验证JWT的知识。然而,我不明白为什么有些网站/教程告诉我们,如果使用JWT,就不需要CSRF保护。我已经阅读了很多,并试图总结以下问题。我只是希望有人能提供一个关于JWT的更大的图景,并澄清我对JWT的误解。

If the JWT is stored in a cookie, I think it is the same as cookie-based authentication except that the server does not need to have sessions to verify the cookie/token. There is still a risk of CSRF if no special measure is implemented. Isn't JWT stored in a cookie? If the JWT is stored in localStorage/sessionStorage, then there is no cookie involved so don't need to protect against CSRF. The question is how to send the JWT to the server. I found here that it is suggested to use jQuery to send the JWT by HTTP header of ajax requests. So, only the ajax requests can do the authentication? Also, I found one more blog that points to use "Authorization header" and "Bearer" to send the JWT. I don't understand the method the blog talks about. Could someone please explain more about "Authorization header" and "Bearer"? Does this make the JWT transmitted by HTTP header of ALL requests? If yes, what about CSRF?

我正在尝试新的Fetch API,但cookie有问题。具体来说,在成功登录后,在未来的请求中有一个Cookie报头,但Fetch似乎忽略了这个报头,并且我所有的请求都是未经授权的。

是因为Fetch还没有准备好,还是Fetch不能与cookie一起工作?

我用Webpack构建我的应用程序。我也在React Native中使用Fetch,它没有同样的问题。

当我想要删除Cookie时,我会尝试

unset($_COOKIE['hello']);

我在firefox的cookie浏览器中看到cookie仍然存在。我怎么才能真正去掉饼干呢?

如何在JavaScript中创建和读取cookie的值?

网络浏览器的cookie密钥的最大大小是多少?

我知道一个cookie的最大大小是4KB,但是密钥也有限制吗?

我有两个WebApp1和WebApp2在两个不同的域。

我在WebApp1的HttpResponse中设置了一个cookie。 如何从WebApp2中的HttpRequest读取相同的cookie ?

我知道这听起来很奇怪,因为cookie是特定于给定域的,我们不能从不同的域访问它们;不过我听说过跨域cookie,它可以在多个web应用程序之间共享。如何使用跨域cookie实现这一需求?

注意:我正在尝试使用J2EE web应用程序

cookie名称和值中允许的字符是什么?它们与URL或某个公共子集相同吗?

我问的原因是,我最近遇到了一些奇怪的行为与cookie有-在他们的名字,我只是想知道这是特定于浏览器或我的代码是错误的。

在通过$.ajax()登录到一个网站后,我试图向该网站发送第二个$.ajax()请求-但当我检查使用FireBug发送的报头时,请求中没有会话cookie。

我做错了什么?

我有两个网站,比如说example.com和anotherexample.net。 在anotherexample.net/page.html上,我有一个IFRAME SRC=“http://example.com/someform.asp”。IFRAME显示一个表单供用户填写并提交到http://example.com/process.asp。当我在它自己的浏览器窗口中打开表单(“someform.asp”)时,一切正常。 然而,当我在ie6或ie7中加载someform.asp作为IFRAME时,example.com的cookie没有保存。在Firefox中,这个问题不会出现。

出于测试目的,我在http://newmoon.wz.cz/test/page.php上创建了一个类似的设置。

example.com使用基于cookie的会话(对此我无能为力),因此如果没有cookie, process.asp将无法执行。我如何迫使IE保存这些cookie ?

嗅探HTTP流量的结果:在GET /someform.asp响应中,有一个有效的每会话Set-Cookie报头(例如Set-Cookie: ASPKSJIUIUGF=JKHJUHVGFYTTYFY),但在POST /process.asp请求中,根本没有Cookie报头。

Edit3:一些AJAX+服务器端脚本显然能够避开这个问题,但这看起来非常像一个bug,而且它还打开了一组全新的安全漏洞。我不希望我的应用程序使用漏洞+安全漏洞的组合只是因为它很容易。

编辑:P3P政策是根本原因,详细解释如下。