我目前正在使用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变量,因此它与安全性相关吗?如果没有,我希望使用当前的解决方案。
[否,他们无法访问res.locals
,除非您以视图或其他方式自行将它们提供给他们。 Some details can be found in the documentation.