node.js - 通过护照 jwt 进行身份验证并将 req 对象附加到对象名称“用户”
问题描述
通过passport-jwt进行身份验证时,它是否总是将req对象附加一个名为“user”的对象?“用户”这个名字是从哪里来的?
app.post('/profile',
passport.authenticate('jwt', { session: false }),
function(req, res) {
res.send(req.user.profile);
}
);
此代码是否总是在对象后面附加req
一个名为 的对象"user"
?这个名字"user"
是从哪里来的?
解决方案
这是passport.authenticate
来自护照源代码第 12 行的定义。所以基本上,passport.authenticate
它是一个中间件,它将进行数据提取,然后将数据绑定到req.user
.
* Applies the `name`ed strategy (or strategies) to the incoming request, in
* order to authenticate the request. If authentication is successful, the user
* will be logged in and populated at `req.user` and a session will be
* established by default. If authentication fails, an unauthorized response
推荐阅读
- twitter-bootstrap - $(...).tooltip 不是在 npm webpack 中使用 bootstrap 4 的函数
- javascript - 我如何在 Angular 6+ 中替换
和 用图标标记值? - emacs - Spacemacs 测试运行器
- c++ - 更改地图中特定键的值
- kotlin - 使用等待时是否异步阻塞
- continuous-integration - 由于对业务组的访问权限,使用 Mule Maven 插件将 Cloudhub 部署到业务组失败
- java - 使用jackson从webfront解析json字符串时如何忽略无效值并设置空值?
- c - “数据定义没有类型或存储类”在 C 中有效吗?
- angularjs - 如何在获取 api 请求中发送日期为 YYYY/mm/dd 而不是 angularjs 中的长格式 2019-12-09T18:15:00.000?
- c++ - 为 glm 翻译添加统一向量在 OpenGL 中不起作用