【问题标题】:http-proxy-middleware, how to copy all/cookie headershttp-proxy-middleware,如何复制所有/cookie 标头
【发布时间】:2016-12-21 09:01:37
【问题描述】:

我正在使用 John Papa 的 lite 服务器和 chimurai 的 HTTP 代理中间件 作为开发服务器。 问题出在我的会话 cookie 上,我无法保留来自真实服务器的会话 cookie。 我看到了这个解决方案: https://github.com/chimurai/http-proxy-middleware/issues/78

但我认为与我的 bs-config.js 没有任何相似之处:

var proxy = require('http-proxy-middleware');

module.exports = {
    port: 3003,
    server: {
        middleware: {
            1: proxy('demo/webservice/jaxrs', {
                target: 'https://localhost:8443',
                secure: false, // disable SSL verification
                changeOrigin: true   // for vhosted sites, changes host header to match to target's host
            }),
            2: require('connect-history-api-fallback')({index: '/index.html', verbose: true})
        }
    }
};

有人知道如何合并这两者吗?

更新:这是响应标头的一部分:

set-cookie:JSESSIONID=620083CD7AEB7A6CC5772AC800E673E3; Path=/appServer/webservice/jaxrs; Secure
strict-transport-security:max-age=31622400; includeSubDomains
Transfer-Encoding:chunked

更新2: 我认为我的配置应该是这样的:

var proxy = require('http-proxy-middleware');

function relayRequestHeaders(proxyReq, req) {
    Object.keys(req.headers).forEach(function (key) {
        proxyReq.setHeader(key, req.headers[key]);
    });
};

function relayResponseHeaders(proxyRes, req, res) {
    Object.keys(proxyRes.headers).forEach(function (key) {
            res.append(key, proxyRes.headers[key]);
        });
};

module.exports = {
    port: 3003,
    server: {
        middleware: {
            1: proxy('/skybox', {
                target: 'https://localhost:8443',
                secure: false, // disable SSL verification
                changeOrigin: true,   // for vhosted sites, changes host header to match to target's host
                onProxyReq: relayRequestHeaders,
                onProxyRes: relayResponseHeaders
            }),
            2: require('connect-history-api-fallback')({index: '/index.html', verbose: true})
        }
    }
};

但是现在 res.append 是未定义的 :(

【问题讨论】:

  • 你能提供来自目标的 RAW 响应吗?该 RAW 响应中的 set-cookie 值是什么?
  • 我更新了我原来的帖子。这是会话 cookie:set-cookie:JSESSIONID=620083CD7AEB7A6CC5772AC800E673E3;路径=/appServer/webservice/jaxrs;安全
  • 您能否提供正在向服务器 3003 端口触发的 RAW 请求?
  • 我正在使用 create react 应用程序,当我使用代理中间件时,我只获得了部分身份验证令牌(大小减小),这有什么原因吗?

标签: javascript angular http-proxy browser-sync lite-server


【解决方案1】:
// Set up the proxy.
if (dev) {
  const { createProxyMiddleware } = require('http-proxy-middleware')
  server.use(
    '/api',
    createProxyMiddleware({
      target: 'https://api.example.com/',
      changeOrigin: true,
      cookieDomainRewrite: 'localhost',
      // logLevel: 'debug',
    })
  )
}

这是我的配置。我认为重点在于

cookieDomainRewrite: 'localhost',

【讨论】:

    【解决方案2】:

    在我的情况下,设置 "cookieDomainRewrite": "localhost", 有效。这允许浏览器正确设置 cookie,因为域会匹配

    下面是 React 中 setupProxy.js 的完整配置:

    const {createProxyMiddleware} = require('http-proxy-middleware');
    
    module.exports = function (app) {
        app.use(
            '/api',
            createProxyMiddleware({
                target: 'http://localhost:8000',
                changeOrigin: true,
                cookieDomainRewrite: "localhost",
            })
        );
    };
    

    【讨论】:

      【解决方案3】:

      B.Ma 的回答给了我一个提示来解决我的 webpack-dev-server 问题,它可能在后台使用 http-proxy-middleware 来代理请求。问题来自 httpOnly cookie,这种方法解决了它。 这是我在 webpack.conf.js 中使用的配置:

      let myappSessionValidationCookie = '';
      
      module.exports = {
          ...
          devServer: {
              publicPath: 'http://localhost:9000/',
              ...
              proxy: {
                  '/api': {
                      target: 'http://localhost/myapp',
                      changeOrigin: true,
                      onProxyReq: function (proxyReq) {
                          if (myappSessionValidationCookie) {
                              proxyReq.setHeader('cookie', myappSessionValidationCookie);
                          }
                      },
                      onProxyRes: function (proxyRes) {
                          const proxyCookie = proxyRes.headers['set-cookie'];
                          if (proxyCookie) {
                              myappSessionValidationCookie = proxyCookie;
                          }
                      },
                  },
              },
          },
      });
      

      对配置的一些解释。我有一个后端在 localhost/myapp/api/* 下为应用程序的 api 提供服务,并设置了一个用于身份验证的 httpOnly cookie。该标头(set-cookie)未由代理传输到新位置(localhost:9000/myapp/api/*),因此浏览器没有保留它,并且所有后续请求都没有此cookie并且失败。 所有的功劳都归于 B.Ma。非常感谢您的帖子!!!

      【讨论】:

        【解决方案4】:

        试试看:

        var cookie;
        function relayRequestHeaders(proxyReq, req) {
          if (cookie) {
            proxyReq.setHeader('cookie', cookie);
          }
        };
        
        function relayResponseHeaders(proxyRes, req, res) {
          var proxyCookie = proxyRes.headers["set-cookie"];
          if (proxyCookie) {
            cookie = proxyCookie;
          }
        };
        

        它与 lite-server 一起工作

        【讨论】:

        • 感谢您的提示,它解决了我在使用 nodejs 和 http-proxy-middleware 时遇到的类似问题
        【解决方案5】:

        不确定您的 localhost:3003 是如何配置的;有无https:...

        假设你使用的是 http://localhost:3000(不是 https:); 目标中的 Secure cookie 属性可能是您的浏览器忽略 cookie 的原因。

        4.1.2.5。安全属性

        Secure 属性将 cookie 的范围限制为“安全”
        通道(其中“安全”由用户代理定义)。当一个
        cookie 具有 Secure 属性,用户代理将包含
        仅当请求通过 a
        传输时,HTTP 请求中的 cookie 安全通道(通常是 HTTP over Transport Layer Security (TLS))

        来源:https://www.rfc-editor.org/rfc/rfc6265#section-4.1.2.5

        浏览器可能会根据以下算法忽略 cookie:https://www.rfc-editor.org/rfc/rfc6265#section-5.4

        尝试删除Secure Attribute 看看是否有帮助

        【讨论】:

        • 在没有安全标志的情况下移动到 http 时,我确实得到了会话。这是否意味着我不能在浏览器同步中使用 https 或安全 cookie?
        • 你仍然可以。只需在浏览器同步中启用https 选项:browserSync({ https: true }); 这样安全 cookie 可能不会被拒绝。
        猜你喜欢
        • 1970-01-01
        • 2020-03-26
        • 2017-03-05
        • 1970-01-01
        • 2019-06-14
        • 2019-05-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多