2012-10-18 62 views
12

我读过这个主题Node.js + express.js + passport.js : stay authenticated between server restart我需要完全一样的东西,但是对于Redis。我用这样的代码:Nodejs + Passport.js + Redis:如何在Redis中存储会话

var RedisStore = require('connect-redis')(express); 
app.use(express.session({ 
    secret: "my secret", 
    store: new RedisStore, 
     cookie: { secure: true, maxAge:86400000 } 
})); 

而且它不起作用。要连接Redis,我使用connect-redis模块。我做错了什么?谢谢!

UPD:我没有得到任何错误。为了确保auth进程成功,我添加了日志行,并执行。

function(email, password, done) { 
    // asynchronous verification, for effect... 
    process.nextTick(function() { 
     findByEmail(email, function(err, user) { 
      if (!user) { 
       return done(null, false, { 
        message: 'Unknown user ' + email 
       }); 
      } 
      if (user.password != password) { 
       return done(null, false, { 
        message: 'Invalid password' 
       }); 
      } 
      //just logging that eveything seems fine 
      console.log("STATUS: User " + email + " authentificated succesfully"); 
      return done(null, user); 
     }) 
    }); 
})); 

日志与express.logger()功能是:

127.0.0.1 - - [Fri, 19 Oct 2012 05:49:09 GMT] "GET /ico/favicon.ico HTTP/1.1" 404 - "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_2) AppleWebKit/537.4 (KHTML, like Gecko) Chrome/22.0.1229.94 Safari/537.4" 
STATUS: User admin authentificated succesfully 

假设有什么不对的身份验证/用户/凭证/串行/解串本身。问题是护照不能将Cookie设置为Redis并将其读取。

+0

什么不行?你遇到了什么错误? – Max

+0

我更新了我的帖子。 – f1nn

+0

“护照无法将Cookie设置为Redis并将其读取。”这没有意义。 – Ben

回答

5

我应该使用

cookie: { secure: false, maxAge:86400000 } 
+2

设置'secure:false'意味着您的会话cookie可以在没有SSL的情况下发送,使您容易受到会话劫持。 http://en.wikipedia.org/wiki/Session_hijacking – Alan

+2

不是一个好主意,而是使用cookie而不是session。 –

+0

您可以为开发做到这一点,但要确保在生产中将安全设置为true。你可以很容易地使用环境变量来做到这一点。 – Porlune

0

当你设定储存明确,会发生什么?即在您的应用程序沿着这些线:

var redis = require('redis'); 
// This is host and port-dependent, obviously 
var redisClient= redis.createClient(6379, 'localhost'); 

app.use(express.session({ 
    secret: 'your secret', 
    /* set up your cookie how you want */ 
    cookie: { maxAge: ... }, 
    store: new (require('express-sessions'))({ 
     storage: 'redis', 
     instance: redisClient 
    }) 
})); 
相关问题