简体   繁体   English

通过 node-http-proxy 持久化基于 cookie 的会话

[英]Persisting a cookie based session over node-http-proxy

I have a simple Express based Node.js web server that I'm using for development of a JavaScript application.我有一个简单的基于 Express 的 Node.js Web 服务器,用于开发 JavaScript 应用程序。 I set up the server to use node-http-proxy to proxy API requests the application makes to a Jetty server that is running on a different domain and port.我将服务器设置为使用 node-http-proxy 来代理应用程序向在不同域和端口上运行的 Jetty 服务器发出的 API 请求。 This setup has been working flawlessly until I started to run into problems with session management.这个设置一直完美无缺,直到我开始遇到会话管理问题。

Upon authentication the application server returns a cookie with an auth token representing the server session.在身份验证后,应用程序服务器会返回一个带有代表服务器会话的身份验证令牌的 cookie。 When I run the JS application off of my filesystem (file://) I can see that once client receives the cookie, it is sent in all the subsequent API requests.当我从我的文件系统 (file://) 运行 JS 应用程序时,我可以看到,一旦客户端收到 cookie,它就会在所有后续 API 请求中发送。 When I run the JS app on the node server and API calls are proxied through node-http-proxy (RoutingProxy) the request headers never include the cookie.当我在节点服务器上运行 JS 应用程序并通过 node-http-proxy (RoutingProxy) 代理 API 调用时,请求标头从不包含 cookie。

Is there something I need to handle manually to support this type of session persistence through the proxy?是否需要手动处理以通过代理支持这种类型的会话持久性? I've been digging through the node-http-proxy code but it is a little over my head because I am new to Node.我一直在研究 node-http-proxy 代码,但我有点不知所措,因为我是 Node.js 的新手。

https://gist.github.com/2475547 or: https://gist.github.com/2475547或:

var express = require('express'),
    routingProxy = require('http-proxy').RoutingProxy(),
    app = express.createServer();

var apiVersion = 1.0,
    apiHost = my.host.com,
    apiPort = 8080;

function apiProxy(pattern, host, port) {
    return function(req, res, next) {
        if (req.url.match(pattern)) {
            routingProxy.proxyRequest(req, res, {host: host, port: port});
        } else {
            next();
        }
    }
}

app.configure(function () {
    // API proxy middleware
    app.use(apiProxy(new RegExp('\/' + apiVersion + '\/.*'), apiHost, apiPort));

    // Static content middleware
    app.use(express.methodOverride());
    app.use(express.bodyParser());
    app.use(express.static(__dirname));
    app.use(express.errorHandler({
        dumpExceptions: true, 
        showStack: true
    }));
    app.use(app.router);
});

app.listen(3000);

I did what you are asking by manually looking at the response, seeing if it is a set-cookie, snipping off the JSESSSIONID, storing it in a variable, and passing it on all subsequent requests as a header.我通过手动查看响应,查看它是否是 set-cookie,剪掉 JSESSSIONID,将其存储在变量中,并将其作为标头传递给所有后续请求,从而完成了您的要求。 This way the reverse proxy acts as a cookie.通过这种方式,反向代理充当 cookie。

on('proxyReq', function(proxyReq){ proxyReq.setHeader('cookie', 'sessionid=' + cookieSnippedValue) 

Hi @tomswift does your local server run on http protocol, but the session cookie receive from the remote server carry with Secure;嗨@tomswift 您的本地服务器是否运行在http协议上,但是从远程服务器接收的会话cookie带有Secure; like:像:

'set-cookie':
[ 'JSESSIONID=COOKIEWITHSECURE98123; Path=/;HttpOnly;Secure;']

If so, before your local server response to the client, extract set-cookie from the original response(response from remote server to local server) header, remove the Secure;如果是这样,在您本地服务器响应客户端之前,从原始响应(从远程服务器到本地服务器的响应)标头中提取set-cookie ,删除Secure; and put the rest of it into the proxy response (response from local server to client ) header like:并将其其余部分放入代理响应(从本地服务器到客户端的响应)标头中,例如:

'set-cookie':
[ 'JSESSIONID=COOKIEWITHSECURE98123; Path=/;HttpOnly;']

then the client will take the session cookie automatically.然后客户端将自动获取会话 cookie。

Hope it may help.希望它可以帮助。

Ideally the job of a proxy is to just forward a request to the destination, and should not strip off critical headers like cookies, But if it is, i think you should file a issue against them here https://github.com/nodejitsu/node-http-proxy/issues .理想情况下,代理的工作是将请求转发到目的地,而不应该剥离像 cookie 这样的关键标头,但如果是这样,我认为您应该在此处针对它们提出问题https://github.com/nodejitsu /node-http-proxy/issues

Also you said the request headers never include the cookie, Is it possible the client never received it?您还说请求标头从不包含 cookie,客户端是否可能从未收到过它?

I found a way to implement this by forking and modifying node-http-proxy.我找到了一种通过派生和修改 node-http-proxy 来实现这一点的方法。 It serves my current purpose which is a development environment.它符合我目前的目的,即开发环境。 For any sort of serious consideration it needs to be fleshed out into a more legitimate solution.对于任何形式的认真考虑,都需要将其充实为更合理的解决方案。

The details can be found in the issue I filed in GitHub: https://github.com/nodejitsu/node-http-proxy/issues/236#issuecomment-5334457详细信息可以在我在 GitHub 提交的问题中找到: https : //github.com/nodejitsu/node-http-proxy/issues/236#issuecomment-5334457

I would love some input on this solution, especially if I'm going completely in the wrong direction.我希望对这个解决方案有一些意见,特别是如果我完全走错了方向。

I had similar problems that cookies were not passed forward to the client.我遇到了类似的问题,即 cookie 没有传递给客户端。 Solution:解决方案:

  1. get cookie from proxy response从代理响应中获取 cookie
  2. set it in proxy request, with slightly modified value在代理请求中设置它,稍微修改值
let cookie;
const webpackConfig = {
  proxy: {
    '/api': {
        ...
        onProxyReq: (proxyReq) => {
          if(cookie) {
            proxyReq.setHeader('Cookie', cookie);
          }
        },
        onProxyRes: (proxyRes) => {
          const sc = proxyRes.headers['set-cookie'];

          const raw = sc.filter(s => s.startsWith('JSESSIONID'));

          if(raw.length) {
            cookie = raw[0].split(';')[0];
          }
        }

    }
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 节点HTTP代理错误 - Node-HTTP-Proxy error 使用node-http-proxy转发每个请求 - Forward every request with node-http-proxy node-http-proxy proxy.on('proxyReq'...)从不触发 - node-http-proxy proxy.on('proxyReq'…) never fires 正在运行的节点服务器将接管整个服务器,并且即使使用node-http-proxy也无法访问其他项目目录 - Running Node server takes over entire server and makes other project directory inaccessible, even with node-http-proxy node-http-proxy如何解析目标网址? - How does node-http-proxy parse the target url? 使用node-http-proxy代理同一子域下的节点服务器和websocket服务器 - Proxy a node server and websocket server under same subdomain with node-http-proxy 在一个端口上使用单个应用程序的节点应用程序使用代理服务器(如node-http-proxy)有什么意义? - What is the point of using a proxy server such as node-http-proxy for a node app with a single app on one port? 如何获取node-http-proxy完成的每个代理请求的响应时间? - How to get response time of each proxy-request done by node-http-proxy? 在向Node-http-proxy Node.js发送请求时挂起套接字 - Socket hangup while posting request to Node-http-proxy Node.js Node-http-proxy:对ressources的请求被重定向到index.html - Node-http-proxy: Requests on ressources get redirected to index.html
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM