使用iframe时,Set-Cookie似乎已损坏

时间:2017-04-07 00:31:12

标签: javascript rest http iframe cookies

我们正面临着一个奇怪的问题。服务器从https://product.example.com/rest/v1/stuff

等端点返回以下内容
Set-Cookie: id=c50b72c0-0b3a-11e7-b356-002590812948;Version=1;Comment="ID";Domain=.example.com;Path=/;HttpOnly

完成后,我们会创建一个iframe,iframe中的所有请求都会发送cookie。当浏览器发出下一个请求(iframe ref)时,会发生一些事情

  1. 没有发送cookie
  2. cookie以id = 9b639ea0-e800-11e6-b3ee-002590812948
  3. 的形式发送
  4. cookie以id = 9b639ea0-e800-11e6-b3ee-002590812948发送; ID = c50b72c0-0b3a-11E7-b356-002590812948
  5. cookie以id = c50b72c0-0b3a-11e7-b356-002590812948
  6. 的形式发送

    如果第一个请求确实使用了正确的cookie,则下一个集可能会有所不同并执行不同的操作(加载页面后大约50个HTTP调用,每个都执行4个列出的状态之一)

    我们发现它发生在Chrome,Safari和Firefox中。

    调试:

    这可能是一个应用程序问题,但我们还没有找到一种方法来调试设置cookie的方法。是否有任何浏览器我们可以通知任何cookie状态的更改(来自http或js)?

    使用Javascript:

    我们使用react 14,这是有问题的代码

    提出网络请求

    getUri(id)
      .then((uri) => {
        self.dispatch(uri.uri);
      })
      .catch((errorMessage) => {
         self.actions.idFailures(errorMessage)
      })
    

    这是getUri

    getUri(id) {
    return new Promise(function(resolve, reject) {
      let url = 'rest/stuff/' + id;
      request
        .get(url)
        .set('Accept', 'application/json')
        .end(function(err, result){
          if(err || !result){
            reject(Error(err.response.text));
          }
          else {
            resolve(JSON.parse(result.text));
          }
        });
    });
    

    },

    设置iframe

    if(this.state.uri) {
      instance = <iframe src={this.state.uri} style={iFrameStyle}> </iframe>;
    }
    

1 个答案:

答案 0 :(得分:0)

是否有任何浏览器可以获得有关Cookie状态更改的通知?

您可以编写计时器以定期检查cookie状态,并在cookie状态更改时在控制台中打印有用信息。检查can i be notified of cookie changes in client side javascript以获取示例代码。以下代码可以使用:

var lastCookie = document.cookie;
var checkCookie = function() {
  var currentCookie = document.cookie;
  if (currentCookie != lastCookie) {
    console.log('cookie changed!');
    console.log('old cookie:');
    console.log(lastCookie);
    console.log('new cookie:');
    console.log(currentCookie);
    lastCookie = currentCookie;
  }
};

window.setInterval(checkCookie, 1000);

但是,只有在代码执行后才能检测到cookie更改,这意味着可能会错过HTML文件响应的Set-Cookie

在您的代码中,使用一个Set-Cookie响应标头设置多个Cookie值,不建议这样做。查看Is it possible to set more than one cookie with a single Set-Cookie?以了解之前的讨论。