【发布时间】:2020-06-17 18:29:05
【问题描述】:
我目前正在使用 Express 和 Passport 创建一个 Node 应用程序,并在我的主 server.js 文件中使用此中间件:
// Function to protect urls
function isProtected(req, res, next) {
if (req.isAuthenticated()) {
// User is logged in
res.locals.user = req.user;
return next();
}
// User is not logged in. Redirect to login.
res.redirect('login');
}
app.use('/protected_path', isProtected, protectedRouter);
我的问题针对这一行:
res.locals.user = req.user;
用户对象被传递给 res.locals,因此它可用于填充名称、用户角色、注册日期等字段...
用户/网站/api 程序(如 Postman)是否可以访问“res.locals”变量? req.user 对象中可能有一些敏感数据,我只想使用“res.locals”来获取将在个人资料页面等视图中呈现的数据。
我可以将必要的变量传递给 res.locals,但这会变得非常混乱。
我的最后一种方法是将此处描述的不必要变量“列入黑名单”:SO: How to omit specific properties from an object in JavaScript
用户可以访问 res.locals 变量,因此它是否与安全相关?如果不是,我会更喜欢我目前的解决方案。
【问题讨论】: