【问题标题】:Sending the User model's password to the client side in Sailsjs App在 Sails Js App 中将用户模型密码发送到客户端
【发布时间】:2014-11-14 01:02:11
【问题描述】:

在学习 Sailsjs 时,我正在经历 example Chat application code。但似乎在成功登录或注册后 MainController 中执行这些操作的功能是通过 res.send(user) 行将在 db 中找到或在 db 中创建的整个 User 对象发送到客户端。

我说的对吗?发送密码不是错误和不安全吗?

或者它只是没有发送?如果有,怎么做?

login action from the api/controllers/MainController.js:

    login: function (req, res) {
        var username = req.param('username');
        var password = req.param('password');

        // Users.findByUsername(username)...
        // In v0.9.0 the find method returns an empty array when no results are found
        // when only one result is needed use findOne.
        Users.findOneByUsername(username)
        .done(function loginfindUser(err, usr){
          if (err) {
            // We set an error header here,
            // which we access in the views an display in the alert call.
            res.set('error', 'DB Error');
            // The error object sent below is converted to JSON
            res.send(500, { error: "DB Error" });
          } else {
            if (usr) {
              var hasher = require("password-hash");
              if (hasher.verify(password, usr.password)) {
                req.session.user = usr;
                res.send(usr);
              } else {
                // Set the error header
                res.set('error', 'Wrong Password');
                res.send(400, { error: "Wrong Password" });
              }
            } else {
              res.set('error', 'User not Found');
              res.send(404, { error: "User not Found"});
            }
          }
        });
      },

【问题讨论】:

    标签: node.js sails.js


    【解决方案1】:

    在用户模型的属性中添加一个 toJSON 函数来删除密码

    module.exports = {
    
        attributes: {
    
            ...
    
            toJSON: function() {
                user = this.toObject()
                delete user.password
                return user
            }
        }
    }
    

    如果您正确地加密了用户的密码,将它们发送到客户端并不是可怕的,但它仍然被认为是一种不好的做法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-24
      • 1970-01-01
      • 1970-01-01
      • 2016-05-17
      • 1970-01-01
      • 2018-09-16
      相关资源
      最近更新 更多