本文介绍了http-proxy-middleware,如何复制全部/cookie标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using lite server by John Papa with HTTP proxy middleware by chimuraias a dev server.the problem is with my session cookie, I cannot persist the session cookie that comes from the real server.I saw this solution:https://github.com/chimurai/http-proxy-middleware/issues/78

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

but I see no resemblance to my 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})
        }
    }
};

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

Does someone knows how to merge this two?

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

UPDATE: this is part of the response headers:

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

UPDATE2:我认为我的配置应如下所示:

UPDATE2:I think my config should look like this:

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是未定义的:(

but now res.append is undefined :(

推荐答案

尝试一下:

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一起使用

It's working with lite-server

这篇关于http-proxy-middleware,如何复制全部/cookie标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 17:19