【问题标题】:Sails JS: How to store and access current user data?Sails JS:如何存储和访问当前用户数据?
【发布时间】:2017-03-17 18:19:26
【问题描述】:

我已经检查了很多参考资料,并找到了像这样一个很好的来源:Get current user from inside the model in Sails。因此,我在寻求最佳做法和您的经验。

由于我基于 JWT-Authentication 开发了一个相当复杂的平台,因此我必须修复一个主要错误,即在我的sails 实例上存储当前用户数据(当用户请求某些内容时)。 我知道这会导致重大安全漏洞(针对多个用户)。

问题是:如何在不通过我创建的几乎所有方法传递会话对象的情况下存储和访问当前用户数据?

通过所有帮助程序、实用程序等传递会话对象是解决此问题的唯一方法吗?而不是使用集中式服务,例如:UserService.getCurrentUser();

非常感谢任何帮助。谢谢!

【问题讨论】:

    标签: session sails.js jwt


    【解决方案1】:

    如果您要问是否有一种方法可以将用户数据全球化,以便它可以神奇地用于您的所有方法,简短的回答是在 Node 中没有安全的方法来执行此操作(更不用说在 Sails.js 中了)。 Node 的单线程特性使得以这种方式维护状态是不可能的。

    有些人在 Sails 中通过使用 globally-applied policy 查找用户并将其添加到请求中解决了这个问题:

    // api/policies/fetch-user.js
    module.exports = function fetchUserPolicy (req, res, next) {
    
      // Get the user ID  out of the session.
      var userId = req.session.userId;
    
      // If there's no user logged in, just continue.
      if (!userId) { return next(); }
    
      // Look up the user by ID.
      User.findOne({id: userId}).exec(function(err, user) {
        if (err) { return res.serverError(err); }
        if (!user) { return res.serverError(new Error('Could not find user in session!')); }
    
        // Add the user info to the request.
        req.user = user;
    
        // Continue the request.
        return next();  
    
      });
    
    };
    

    此代码没有任何问题,但我们不建议这样做,因为最佳做法是仅将策略用于访问控制。相反,您可以在自定义 hook 中执行几乎完全相同的操作:

    // api/hooks/fetch-user.js
    module.exports = function fetchUserHook(sails) {
    
      return {
    
        // Add some routes to the app.
        routes: {
    
          // Add these routes _before_ anything defined in `config/routes.js`.
          before: {
    
            // Add a route that will match everything (using skipAssets to...skip assets!)
            '/*': {
              fn: function(req, res, next) {
    
                // Get the user ID  out of the session.
                var userId = req.session.userId;
    
                // If there's no user logged in, just continue.
                if (!userId) { return next(); }
    
                // Look up the user by ID.
                User.findOne({id: userId}).exec(function(err, user) {
                  if (err) { return res.serverError(err); }
                  if (!user) { return res.serverError(new Error('Could not find user in session!')); }
    
                  // Add the user info to the request.
                  req.user = user;
    
                  // Continue the request.
                  return next();
    
                });
              },
              skipAssets: true
            }
    
          }
        }
      };
    };
    

    无论哪种方式,您仍然需要将req 传递给任何想要使用获取的用户信息的方法。

    【讨论】:

    • 感谢您的回复。因此,您建议在检查策略之前使用挂钩将用户信息添加到当前请求。使用所有一项服务来检查角色等的不同策略并在登录时将当前用户数据添加到请求会话有什么问题?什么是通过的最佳方式,例如?当前 userId 在某些时候至少有 4 个或更多方法/服务?只是通过所有这些方法作为参数?
    • 您对在模型中处理当前用户数据有什么建议?例如。根据 ACL 限制返回的 json 输出 [on toJSON()] 或在 afterUpdate() 等生命周期中记录用户操作?
    猜你喜欢
    • 1970-01-01
    • 2013-10-03
    • 2021-08-20
    • 1970-01-01
    • 1970-01-01
    • 2022-10-15
    • 2019-01-15
    • 2015-12-21
    • 1970-01-01
    相关资源
    最近更新 更多