我正在将 Passport 与 NodeJS Express、Connect 一起使用。会话存储是 RedisStore。我正在使用 Forcedotcom 策略。
当我对我的 Express 服务器进行 REST 调用时,我总是在 req.session 的 Passport 部分看到一个空白。此外, req.user 未定义:
{ cookie: ,
_expires: Tue Oct 29 2013 07:38:47 GMT+0000 (UTC),
httpOnly: true,
domain: '.DOMAIN' },
passport: {} }
但是,用户正在我的套接字对象 socket.handshake.user 上设置。
io.set("authorization", passportSocketIo.authorize({
cookieParser: express.cookieParser, //or connect.cookieParser
key: 'expressSid', //the cookie where express (or connect) stores its session id.
secret: expressSecret, //the session secret to parse the cookie
store: sessionStore, //the session store that express uses
fail: function(data, accept) { // *optional* callbacks on success or fail
accept(null, false);
},
success: function(data, accept) {
accept(null, true);
}
}));
我在身份验证期间存储在 Redis 中的用户对象的 Passport 序列化和反序列化:
passport.serializeUser(function( user, done ) {
done( null, user.id);
});
passport.deserializeUser(function( id, done ) {
redis.hget("usersf:"+id, "data", function(err, data) {
done( null, JSON.parse(data) );
});
});
快速连接中间件:
var sessionStore = new RedisStore({
client :redis,
host: rtg.hostname,
port: rtg.port,
db: redisAuth[0],
pass: redisAuth[1],
prefix: 'appsess:'
});
appSecure.use(allowCrossDomain);
appSecure.use(express.cookieParser());
appSecure.use(express.bodyParser());
appSecure.use(express.methodOverride());
appSecure.set('port', port);
appSecure.use(express.session({ secret: expressSecret, store: sessionStore, key:'expressSid', cookie: { maxAge : 604800000, domain:'.DOMAIN'}}));
appSecure.use(passport.initialize());
appSecure.use(passport.session());
appSecure.use(appSecure.router);
appSecure.use(express.static(__dirname + '/public'));
appSecure.use(express.errorHandler({ dumpExceptions: true, showStack: true }));