passport js如何在会话中存储用户对象?
How does passport js stores user object in session?
我在开发中使用 node/express 和 passport。我看到一篇文章说:
Express loads the session data and attaches it to the req. As passport stores the serialised user in the session, the serialised user object can be found at req.session.passport.user.
但令我惊讶的是,浏览器 cookie 中存储的 sessionID 值在登录前后保持不变。那么序列化后的用户对象存放在哪里呢?
我认为它最初存储在用户 sessionid
cookie 中,但似乎并非如此,因为我仍然可以使用 req.session.passport.user
访问我的用户对象
您可以将 cookie 中的 sessionID
视为存储会话数据的数据库的键。根据您使用 express 的会话处理程序,以及您使用的存储策略,数据将以不同的方式存储。这意味着 sessionID 在登录前、成功登录后甚至用户注销后都可以是相同的值。
如果使用express-session with MemoryStore
the data will be saved in the memory of the node process, indexed on the sessionID. Look here for initialization of the store and here for storing的数据。
您可以创建一个将数据序列化到 cookie 的存储区,但是 none compatible session stores 中列出了这些存储区。
So where does the serialised user object is stored?
简而言之
序列化用户 object 由 PassportJS
在 req.session.passport.user
的帮助下存储在 req.user
中(由 Express
填充) Passport's
deserializeUser
方法。
Express
将 session object 的 id 添加到用户浏览器上的 cookie 中,该 cookie 在每次请求时以 header 发送回 express。 Express
然后从 header 中获取 id 并搜索 session 商店(即 Mongo 或其他)并找到条目并将其加载到 req.session
.
PassportJS
使用 req.session
的内容在 serializeUser
和 deserializeUser
方法的帮助下跟踪经过身份验证的用户(有关 serializeUser
和 deserializeUser
在这个 SO question 中看到我的回答。
Express
负责创建session。 何时创建 sessions? 即 Express
未检测到 session cookie。因此,在 app
或 server.js
文件中组织 session
和 passport
配置的顺序非常重要。如果你在 static directory configs
之上声明你的 session
和 passport
配置,那么所有对 static content
的请求也会得到一个 session,这是不好的。
请参阅我对此 SO question 的回答,其中我提到了静态内容访问以及如何选择性地将 passport
应用于某些路由,而不是默认(您可能不需要验证所有路由 - 因此您可以通过仅将 session 附加到映射到安全 URL 的请求来避免不必要的 session store lookup
和 de-serialization
见下文)。
//selectively applying passport to only secure urls
app.use(function(req, res, next){
if(req.url.match('/xxxx/secure'))
passport.session()(req, res, next)
else
next(); // do not invoke passport
});
如果你想了解 PassportJS 的工作流程,我强烈建议你阅读一本书 tutorial。
我在开发中使用 node/express 和 passport。我看到一篇文章说:
Express loads the session data and attaches it to the req. As passport stores the serialised user in the session, the serialised user object can be found at req.session.passport.user.
但令我惊讶的是,浏览器 cookie 中存储的 sessionID 值在登录前后保持不变。那么序列化后的用户对象存放在哪里呢?
我认为它最初存储在用户 sessionid
cookie 中,但似乎并非如此,因为我仍然可以使用 req.session.passport.user
您可以将 cookie 中的 sessionID
视为存储会话数据的数据库的键。根据您使用 express 的会话处理程序,以及您使用的存储策略,数据将以不同的方式存储。这意味着 sessionID 在登录前、成功登录后甚至用户注销后都可以是相同的值。
如果使用express-session with MemoryStore
the data will be saved in the memory of the node process, indexed on the sessionID. Look here for initialization of the store and here for storing的数据。
您可以创建一个将数据序列化到 cookie 的存储区,但是 none compatible session stores 中列出了这些存储区。
So where does the serialised user object is stored?
简而言之
序列化用户 object 由 PassportJS
在 req.session.passport.user
的帮助下存储在 req.user
中(由 Express
填充) Passport's
deserializeUser
方法。
Express
将 session object 的 id 添加到用户浏览器上的 cookie 中,该 cookie 在每次请求时以 header 发送回 express。 Express
然后从 header 中获取 id 并搜索 session 商店(即 Mongo 或其他)并找到条目并将其加载到 req.session
.
PassportJS
使用 req.session
的内容在 serializeUser
和 deserializeUser
方法的帮助下跟踪经过身份验证的用户(有关 serializeUser
和 deserializeUser
在这个 SO question 中看到我的回答。
Express
负责创建session。 何时创建 sessions? 即 Express
未检测到 session cookie。因此,在 app
或 server.js
文件中组织 session
和 passport
配置的顺序非常重要。如果你在 static directory configs
之上声明你的 session
和 passport
配置,那么所有对 static content
的请求也会得到一个 session,这是不好的。
请参阅我对此 SO question 的回答,其中我提到了静态内容访问以及如何选择性地将 passport
应用于某些路由,而不是默认(您可能不需要验证所有路由 - 因此您可以通过仅将 session 附加到映射到安全 URL 的请求来避免不必要的 session store lookup
和 de-serialization
见下文)。
//selectively applying passport to only secure urls
app.use(function(req, res, next){
if(req.url.match('/xxxx/secure'))
passport.session()(req, res, next)
else
next(); // do not invoke passport
});
如果你想了解 PassportJS 的工作流程,我强烈建议你阅读一本书 tutorial。