NodeJS redis connect会话ID在保持持久性时会重新生成

时间:2012-02-26 10:26:19

标签: node.js session express redis express-session

我尝试在NodeJS中使用Redis的标准会话持久性:

var express = require('express');
var RedisStore = require('connect-redis')(express);
var app = module.exports = express.createServer();

// Configuration

app.configure(function(){
  app.use(express.bodyParser());
  app.use(express.cookieParser());
  app.use(express.session({ 
    secret: "keyboard cat", 
    store: new RedisStore, 
    key: 'sid' 
  }));
});


app.get('/', function (req, res) {
  if (req.session.isValid) {
    console.log("There is an existing session.");
  }
  else {
    req.session.isValid = true;
    console.log("New session.");
    console.log('Old session ID: ' + req.header('Cookie'));
    console.log('New session ID: ' + req.session.id);
  }

  res.render('index', {'title': s});
});
app.listen(4000);

理论上我应该看到“新会话”这一行。网站的一次和所有后续调用应导致“存在现有会话”。 不幸的是,在每次调用时都会重新生成新的会话ID。浏览器中的cookie工作正常,内容正确传输,我可以在

中看到它
req.header('Cookie')

这是控制台日志的样子:

[app.js] New session.
[app.js] Old session ID: undefined
[app.js] New session ID: nuoHKZj2j0AoRkvqT4xE5h6W.zF+DNv2rzr3kpeO2IyD7sa4xdamFQMugjfQvY6OYymE
[app.js] New session.
[app.js] Old session ID: sid=neLUc5PXxPoj1yFqukerv49x.BHzYKiuAfFSNHKd4fCAkv8wNwZO%2FxykJPN5R5tjAlQc
[app.js] New session ID: FvuzjnXvchCkmVqsq5mrodL2.5YlT3InfTbvOwEUc0dNpPLT77tcdJpNuhbFGVYkLneQ
[app.js] New session.
[app.js] Old session ID: sid=pFbyVdlNZXtF5vZ35CW9sfmq.nJ1RBjJu59iUJJjmZv9TCYiYLcvycme%2BJh8sQC6%2FzEE
[app.js] New session ID: KdPhwqwwgmOnPZEuVapy7EJe.I0TGT9HSSQQSporwCNsxl11rXDxR/ysjTeZb0lD5uwI

同时在redis-cli中运行“MONITOR”命令时,我得到以下输出:

sess:nuoHKZj2j0AoRkvqT4xE5h6W.zF+DNv2rzr3kpeO2IyD7sa4xdamFQMugjfQvY6OYymE
*2
$3
get
$73
sess:Q6Z06GL4hdRytKA2MToCIgVw.JWxImSB/m20Urn+IYMQqnNqfQp4ygAESiyBLORn3Iuo
*2
$3
get
$73
sess:neLUc5PXxPoj1yFqukerv49x.BHzYKiuAfFSNHKd4fCAkv8wNwZO/xykJPN5R5tjAlQc
*2
$3
get
$73
sess:FvuzjnXvchCkmVqsq5mrodL2.5YlT3InfTbvOwEUc0dNpPLT77tcdJpNuhbFGVYkLneQ 
*2 
$3
get
$73
sess:zvCWdwzowgAfl6jH8m0D31vL.b5tK5VZUJtPHrdvH09A/hjhjoOg6bT0CmAcWWRf99SI
*2
$3
get
$73
sess:pFbyVdlNZXtF5vZ35CW9sfmq.nJ1RBjJu59iUJJjmZv9TCYiYLcvycme+Jh8sQC6/zEE
*2
$3
get
$73
sess:KdPhwqwwgmOnPZEuVapy7EJe.I0TGT9HSSQQSporwCNsxl11rXDxR/ysjTeZb0lD5uwI
*2
$3
get
$73
sess:r793eaJyOnaq2RNyw1Hmpuwv.xnonbOlaWEAlpz+LDg0SHcUeAa0sbjyw0oIcwFmlX0w

当我使用RedStore的MemoryStore insteand时,一切都像预期的那样工作。

有什么想法吗?

3 个答案:

答案 0 :(得分:0)

我有类似的问题。我的会话在每次请求时都会重新生成。我通过在虚拟机上设置适当的日期来解决它。

答案 1 :(得分:0)

试试这个..

let redisStore,redisClient;
const redis = require('redis');
redisStore = require('connect-redis')(session); 

redisClient = redis.createClient();

redisClient.on('error', (err) => {
    console.log('Error: ', err);
});

app.use(session({
    store   : new redisStore({ host: 'localhost', port: 'port', client: redisClient }),
    name    : 'session_name', 
    resave  : false,
    saveUninitialized: false,
    rolling : true,
}));

注意:redis 不支持 window/localhost。

答案 2 :(得分:-1)

在每个请求上重新生成会话时,我遇到了同样的问题。原因是'静态'中间件被放置在'session'之下。因此,请确保按正确顺序放置中间件,即:

...
app.use(express.static(path.join(__dirname, 'public')));
app.use(express.session({ store: new RedisStore({ prefix: 'sid_' }) }));
app.use(app.router)
app.get ...

'express.session'中间件应该在'静态'下。