【问题标题】:Bcrypt not hashing and salting passwords anymoreBcrypt 不再对密码进行散列和加盐处理
【发布时间】:2019-07-11 16:12:47
【问题描述】:

在我当前应用程序的先前版本中,我有一个使用 bcrypt 的工作后端应用程序,它对我的​​密码进行加盐和哈希处理。在这个版本中,我现在处理的是具有相同路由和控制器的 1 对 1 副本。 一切正常,来自发布请求的数据保存得很好,但没有散列密码。现在显示空白密码。

我在 Windows 10、64 位上工作,我的版本中的两个版本都是 bcrypt 是 3.0.4 本地安装的。我使用 mongoDB 和 mongoose。

我对@9​​87654321@ 使用最通用的代码版本。如前所述,这在我的旧版本中仍然有效。

有人知道发生了什么变化吗?

代码如下:

//relevant parts of app.js
const express = require('express');
const path = require('path');
//const favicon = require('serve-favicon');
const logger = require('morgan');
const cookieParser = require('cookie-parser');
const bodyParser = require('body-parser');
const helmet = require('helmet');
const cors = require('cors');

// connection to mongoose 
require('./app_api/models/db');

//route to routes
const users = require('./app_api/routes/users');

//routes (post request)
router
	.route('/user/signup')
	.post(AuthenticationControllerPolicy.signupPost, ctrlUsers.signupPost); 

//fragment of the post controller
const signupPost = function (req, res) {
	//Make sure this account already exists
	Base.
		findOne({
			userName: req.body.userName
		}, function (user, err) {
			//Make sure user doesn 't already exist
			if (err) {
				return res.status(400).send({ msg: 'The email address you have entered is already associated with another account.' });
			} else { //etc..


//Create and save the user
user = new Base({
password: req.body.password
});
user.save(function (err) {

// base model with hashing and salting code
const baseSchema = new mongoose.Schema({
	password: { type: String, required: true }
	}, options);

const Base = mongoose.model('Base', baseSchema);

// salting and hashing
						

// hashing and salting before saving
baseSchema.pre('save', function (next) {

	let base = this;
	// only hash the password if it has been modified (or is new)
	if (!base.isModified('password')) return next();

	//generate a salt
	bcrypt.genSalt(SALT_WORK_FACTOR, function (err, salt) {
	if (err) return next(err);

	// hash the password using our new salt
	bcrypt.hash(base.password, salt, function (err, hash) {
	if (err) return next(err);

	// override the cleartext password with the hashed one
	base.password = hash;
	next();
		});
	});
	});

【问题讨论】:

    标签: node.js express mongoose bcrypt


    【解决方案1】:

    试试这样的。确保 const Base = mongoose.model('Base', baseSchema); 位于代码的末尾,因为它负责创建模型,并且您已在 pre 钩子不会被创建,密码也不会被散列。

        // On Save Hook, encrypt password
        // Before saving a model, run this function
        baseSchema.pre('save', function (next) {
          //get access to the user model
          const base= this;
    
          // generate a salt then run callback
          bcrypt.genSalt(SALT_WORK_FACTOR, function (err, salt) {
            if (err) { return next(err); }
    
            // hash (encrypt) our password using the sale
            bcrypt.hash(base.password, salt, null, function (err, hash) {
              if (err) { return next(err); }
    
              //overwrite plain text password with encrypted password
              base.password = hash;
              next();
            });
          });
        });
    
    const Base = mongoose.model('Base', baseSchema);
    

    【讨论】:

    • 它有效。感谢您的帮助。仍然不明白为什么这在旧版本中有效。
    猜你喜欢
    • 2015-10-08
    • 2015-08-06
    • 2017-09-30
    • 1970-01-01
    • 2015-02-17
    • 1970-01-01
    • 2015-08-20
    • 2021-06-04
    • 2011-06-02
    相关资源
    最近更新 更多