【问题标题】:Cookie not getting sent by Chrome and withCredentials is trueCookie 未由 Chrome 和 withCredentials 发送是真的
【发布时间】:2020-03-14 08:53:38
【问题描述】:

Chrome 选择不在 GET 请求中发送 Cookie 标头。我重写了XMLHTTPRequest,这样当库调用open() 时,它调用open() 的原始实现,然后设置withCredentials = true

我尝试了一个 netlog 查看器会话,但似乎没有显示与 cookie 相关的项目。我尝试了不同的绑定技术,但我认为这个问题与this 上下文无关。 cookie 在浏览器工具的应用程序下的 cookie 列表中显示良好。该域被列为.mydomain.com

<script type="text/javascript">

   document.cookie = "ATOKEN=\"d=dflskdjflsdkfj=1,\"; Version=1; Domain=.mydomain.com; expires=Tues, 23 Jul 2019 20:54:04 GMT; Path=/";

   // library makes request to a-subdomain.mydomain.com

   var originalXMLHttpRequest = XMLHttpRequest;

   XMLHttpRequest = function ()
   {
     var xhr     = new originalXMLHttpRequest();
     var _httpOpen = xhr.open.bind(xhr);

     xhr.open = function (method, url, flag)
     {
       _httpOpen(method, url, flag ? flag : true);

       if (url.indexOf('a-subdomain') >= 0)
       {
         xhr.withCredentials = true;
       }
     };

     return xhr;
   };
</script>

使用 Charles 代理查看 403 请求并注意到 Cookie 标头不存在。 Cookie 标头应该与 Cookie: ...

【问题讨论】:

  • 当您发送带有withCredentials true 的请求时,浏览器首先发送一个OPTIONS 预检请求,并且服务器必须使用Access-Control-Allow-Credentials: true 响应标头响应该OPTIONS 请求,以及所有其他必需的CORS 标头。您是否看到正在发出 OPTIONS 请求,如果有,服务器如何响应?如果没有 OPTIONS 预检,那么听起来您的 cookie 可能只是一个一般性问题(例如,它没有标记为发送到您的目标域)
  • 我在 Chrome 网络调用中没有看到 OPTIONS 预检请求。服务器正在以Access-Control-Allow-Credentials: trueAccess-Control-Allow-Origin: https://a-subdomain.mydomain.com403 状态响应GET。那么可能是cookie有问题。
  • 您需要确保您发送请求的服务器允许未经身份验证的 OPTIONS 请求。启用 CORS 的服务器基本上总是以 200 OK 响应 OPTIONS 请求——即使请求从未包含凭据(因为浏览器从不发送任何凭据作为 CORS 预检 OPTIONS 请求的一部分)。但是在这种情况下,服务器正在使用 403 响应 OPTIONS 请求。如果服务器正确启用了 CORS,则基本上不会发生这种情况。所以这里的第一个修复是让服务器响应 200 OK 对 OPTIONS 请求。

标签: javascript google-chrome http cookies cross-domain


【解决方案1】:

我通过将withCredentials 设置为true“发送前”而不是“打开后”来使其工作。在调用_httpSend() 之前的xhr.send() 中,xhr 对象的withCredentials 属性设置回false,即使它在xhr.open() 中设置为true

我仔细检查了这两个方法中的 xhr 对象是同一个对象,并且属性实际上在每个方法中都设置为true。不知何故,withCredentials 在调用xhr.openxhr.send 之间被重置。所以我只是确保在发送请求之前将其设置为xhr.send 中的true

<script type="text/javascript">

    document.cookie = 'ATOKEN=\"d=dflskdjflsdkfj=1,\"; domain=.mydomain.com;';

    // library makes request to a-subdomain.mydomain.com

    var originalXMLHttpRequest = XMLHttpRequest;

    XMLHttpRequest = function ()
    {
      var xhr     = new originalXMLHttpRequest();
      var _httpOpen = xhr.open.bind(xhr);
      var _httpSend = xhr.send.bind(xhr);

      var _url;

      xhr.open = function (method, url, flag)
      {
        _url = url;

        _httpOpen(method, url, flag ? flag : true);
      };

      xhr.send = function (body)
      {

        if (_url && _url.indexOf('a-subdomain') >= 0)
        {
          xhr.withCredentials = true;
        }

        _httpSend(body);
      };

      return xhr;
    };
</script>

我不知道为什么要重置此属性,因为 XMLHTTPRequest Standard 中的方法不会触及该属性。也许它在我覆盖的第三方 Javascript 中被重置,我无权访问。

【讨论】:

    猜你喜欢
    • 2012-12-06
    • 2014-04-01
    • 2018-10-09
    • 2014-07-16
    • 2018-04-01
    • 1970-01-01
    • 2014-08-02
    • 1970-01-01
    • 2019-04-02
    相关资源
    最近更新 更多