http-proxy-middleware,如何复制所有/cookie 标头

Avi*_*Avi 5 javascript http-proxy browser-sync lite-server angular

我正在使用 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})
        }
    }
};
Run Code Online (Sandbox Code Playgroud)

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

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

set-cookie:JSESSIONID=620083CD7AEB7A6CC5772AC800E673E3; Path=/appServer/webservice/jaxrs; Secure
strict-transport-security:max-age=31622400; includeSubDomains
Transfer-Encoding:chunked
Run Code Online (Sandbox Code Playgroud)

UPDATE2:我认为我的配置应该是这样的:

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})
        }
    }
};
Run Code Online (Sandbox Code Playgroud)

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

B.M*_*.Ma 5

尝试一下:

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;
  }
};
Run Code Online (Sandbox Code Playgroud)

它与精简服务器一起工作


chi*_*rai 2

不确定你的 localhost:3003 是如何配置的;有还是没有https:...

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

4.1.2.5. 安全属性

Secure 属性将 cookie 的范围限制为“安全”
通道(其中“安全”由用户代理定义)。当
cookie 具有 Secure 属性时,
仅当请求通过
安全通道(通常是基于传输层安全 (TLS) 的 HTTP)传输时,用户代理才会在 HTTP 请求中包含 cookie

来源: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并看看是否有帮助