通过node-http-proxy持续基于cookie的会话 [英] Persisting a cookie based session over node-http-proxy

查看:738
本文介绍了通过node-http-proxy持续基于cookie的会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的基于Express的Node.js Web服务器,用于开发JavaScript应用程序。我设置服务器以使用node-http-proxy来代理应用程序对在不同域和端口上运行的Jetty服务器的API请求。此设置一直工作正常,直到我开始遇到会话管理问题。

I have a simple Express based Node.js web server that I'm using for development of a JavaScript application. 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. This setup has been working flawlessly until I started to run into problems with session management.

在身份验证后,应用程序服务器将返回一个带有表示服务器会话的验证令牌的cookie。当我从我的文件系统(文件://)运行JS应用程序时,我可以看到,一旦客户端收到cookie,它将在所有后续API请求中发送。当我在节点服务器上运行JS应用程序时,通过node-http-proxy(RoutingProxy)代理API调用,请求头不会包含该cookie。

Upon authentication the application server returns a cookie with an auth token representing the server session. 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. 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.

是否有东西我需要手动处理以通过代理支持这种类型的会话持久性?我一直在挖掘node-http代理代码,但是这是一个有点超出我的头,因为我是Node的新手。

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.

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);


推荐答案

我通过手动查看响应,看看它是否是一个cookie,剪切JSESSSIONID,将其存储在一个变量中,并将其作为标题将所有后续请求传递给它。这样,反向代理作为一个cookie。

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. This way the reverse proxy acts as a cookie.

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

这篇关于通过node-http-proxy持续基于cookie的会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆