未在浏览器中设置http-proxy-middleware“ set-cookie”响应

时间:2019-09-10 22:06:08

标签: node.js express cookies proxy http-proxy-middleware

我尝试查看过去对此类问题的答复,例如以下内容,但它们都会引发各种错误:

我的代理设置返回了正确的Response Headers,其中包含要设置的Cookie:set-cookie: JSESSIONID=yElsHUaPE8Ip_AAD_oIfTQ; Path=/; Secure; HttpOnly;。这些不是会话Cookie。

但是,由于JSESSIONID未存储为cookie,因此我的登录正式失败。

这是我的代理设置:

const proxyTable = {
  "/url": "http://localhost:4040/url",
  "proxy.url.com/": "http://localhost:4040/",
};

const signin_proxy_options = {
  target: host,
  autoRewrite: true,
  secure: true,
  reqBodyEncoding: null,
  changeOrigin: true,
  logLevel: "debug",
  router: proxyTable,
  protocolRewrite: "http",
  cookieDomainRewrite: { "*": "" },
  onProxyRes: function(proxyRes, req, res) {
    if (proxyRes.headers["set-cookie"] !== undefined) {
      console.log("** SET-COOKIE: ", proxyRes.headers["set-cookie"]);

      const cookieJar = proxyRes.headers["set-cookie"];
      // cookieJar = 'JSESSIONID=yElsHUaPE8Ip_AAD_oIfTQ; Path=/; Secure; HttpOnly;'
      var temp = cookieJar.split(";")[0].split("=");
      // temp = [ 'JSESSIONID', 'yElsHUaPE8Ip_AAD_oIfTQ' ]
      res.cookie(temp[0], temp[1]);
    }
  },
};

// Proxy configuration
const signin_proxy = proxy(signin_filter, signin_proxy_options);
app.use("/signin", signin_proxy);

成功后,服务器将返回302进行重定向。这会产生影响吗?这就是为什么我有proxyTable的原因...

此外,由于看起来响应正常,因此我删除了onProxyRes字段希望自动设置该字段的希望,但也没有运气。

我感谢任何想法/解决方案。

1 个答案:

答案 0 :(得分:0)

我不确定这是否是最佳做法,但是JSESSIONID cookie似乎不喜欢与Secure标志一起存储。这是我更新的代理选项:

const signin_proxy_options = {
  target: host,
  autoRewrite: true,
  secure: true,
  changeOrigin: true,
  logLevel: "debug",
  protocolRewrite: "http",
  onProxyRes: function(proxyRes, req, res) {
    if (proxyRes.headers["set-cookie"] !== undefined) {
      proxyRes.headers["set-cookie"] = proxyRes.headers[
        "set-cookie"
      ][0].replace("Secure; ", ""); // JSESSIONID cookie cannot be set thru proxy with Secure
      return proxyRes;
    }
  },
};