欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

使用mongoose和bcrypt實現(xiàn)用戶密碼加密的示例

 更新時間:2018年02月11日 08:38:27   作者:小火柴的藍(lán)色理想  
下面小編就為大家分享一篇使用mongoose和bcrypt實現(xiàn)用戶密碼加密的示例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

前面的話

最近在做的個人項目中,需要對密碼進(jìn)行加密保存,對該操作的詳細(xì)步驟記錄如下

介紹

關(guān)于mongoose已經(jīng)寫過博客就不再贅述,下面主要介紹bcrypt

bcrypt是一個由兩個外國人根據(jù)Blowfish加密算法所設(shè)計的密碼散列函數(shù)。實現(xiàn)中bcrypt會使用一個加鹽的流程以防御彩虹表攻擊,同時bcrypt還是適應(yīng)性函數(shù),它可以借由增加迭代之次數(shù)來抵御暴力破解法

使用npm安裝即可

npm install --save bcrypt

用戶模型

下面來創(chuàng)建代碼用戶user的schema,用戶名不能重復(fù)

var mongoose = require('mongoose'),
 Schema = mongoose.Schema,
 bcrypt = require('bcrypt');var UserSchema = new Schema({
 username: { type: String, required: true, index: { unique: true } },
 password: { type: String, required: true }
});
module.exports = mongoose.model('User', UserSchema);

加密

下面加入用戶模型的是Mongoose的中間件,該中間件使用pre前置鉤子,在密碼保存之前,自動地把密碼變成hash。詳細(xì)代碼如下

let SALT_WORK_FACTOR = 5
UserSchema.pre('save', function(next) {
 var user = this;
 //產(chǎn)生密碼hash當(dāng)密碼有更改的時候(或者是新密碼)
 if (!user.isModified('password')) return next();
 // 產(chǎn)生一個salt
 bcrypt.genSalt(SALT_WORK_FACTOR, function(err, salt) {
  if (err) return next(err);
  // 結(jié)合salt產(chǎn)生新的hash
  bcrypt.hash(user.password, salt, function(err, hash) {
   if (err) return next(err);
   // 使用hash覆蓋明文密碼
   user.password = hash;
   next();
  });
 });
});

在node.bcrypt.js中SALT_WORK_FACTOR默認(rèn)使用的是10,這里設(shè)置為5

驗證

加密之后,密碼原文被替換為密文了。我們無法解密,只能通過bcrypt的compare方法,對再次傳入的密碼和數(shù)據(jù)庫中保存的加密后的密碼進(jìn)行比較,如果匹配,則登錄成功

UserSchema.methods.comparePassword = function(candidatePassword, cb) {
 bcrypt.compare(candidatePassword, this.password, function(err, isMatch) {
  if (err) return cb(err);
  cb(null, isMatch);
 });
};

把上面的幾個步驟串在一起,完整代碼如下

var mongoose = require('mongoose'),
 Schema = mongoose.Schema,
 bcrypt = require('bcrypt'),
 SALT_WORK_FACTOR = 5;
var UserSchema = new Schema({
 username: { type: String, required: true, index: { unique: true } },
 password: { type: String, required: true }
});
UserSchema.pre('save', function(next) {
 var user = this;
 // only hash the password if it has been modified (or is new)
 if (!user.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(user.password, salt, function(err, hash) {
   if (err) return next(err);
   // override the cleartext password with the hashed one
   user.password = hash;
   next();
  });
 });
});
UserSchema.methods.comparePassword = function(candidatePassword, cb) {
 bcrypt.compare(candidatePassword, this.password, function(err, isMatch) {
  if (err) return cb(err);
  cb(null, isMatch);
 });
};
module.exports = mongoose.model('User', UserSchema);

測試

把上面的代碼保存成user-model.js,然后運行下面代碼來實際測試

var mongoose = require('mongoose'),
 User = require('./user-model');
var connStr = 'mongodb://localhost:27017/mongoose-bcrypt-test';
mongoose.connect(connStr, function(err) {
 if (err) throw err;
 console.log('Successfully connected to MongoDB');
});
// create a user a new user
var testUser = new User({
 username: 'jmar777',
 password: 'Password123'
});
// save user to database
testUser.save(function(err) {
 if (err) throw err;
 // fetch user and test password verification
 User.findOne({ username: 'jmar777' }, function(err, user) {
  if (err) throw err;
  // test a matching password
  user.comparePassword('Password123', function(err, isMatch) {
   if (err) throw err;
   console.log('Password123:', isMatch); // -> Password123: true
  });
  // test a failing password
  user.comparePassword('123Password', function(err, isMatch) {
   if (err) throw err;
   console.log('123Password:', isMatch); // -> 123Password: false
  });
 });
});

控制臺中輸入如下數(shù)據(jù):

數(shù)據(jù)庫數(shù)據(jù)如下:

以上這篇使用mongoose和bcrypt實現(xiàn)用戶密碼加密的示例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Mongodb數(shù)據(jù)庫兩種啟動方法小結(jié)

    Mongodb數(shù)據(jù)庫兩種啟動方法小結(jié)

    MongoDB是一種開源的服務(wù)器端NoSQL數(shù)據(jù)庫管理系統(tǒng),它提供了一種靈活的框架,可以快速地存儲、處理和管理大量的數(shù)據(jù),這篇文章主要給大家介紹了關(guān)于Mongodb數(shù)據(jù)庫兩種啟動方法的相關(guān)資料,需要的朋友可以參考下
    2023-12-12
  • MongoDB索引的用法介紹

    MongoDB索引的用法介紹

    這篇文章介紹了MongoDB索引的用法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-06-06
  • MongoDB中常用操作$addToSet、$pop和$rename

    MongoDB中常用操作$addToSet、$pop和$rename

    本文主要介紹了MongoDB中常用操作$addToSet、$pop和$rename,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-12-12
  • MongoDB查詢字段沒有創(chuàng)建索引導(dǎo)致的連接超時異常解案例分享

    MongoDB查詢字段沒有創(chuàng)建索引導(dǎo)致的連接超時異常解案例分享

    這篇文章主要介紹了MongoDB查詢字段沒有創(chuàng)建索引導(dǎo)致的連接超時異常解案例分享,本文是生產(chǎn)環(huán)境下總結(jié)而來,需要的朋友可以參考下
    2014-10-10
  • Java操作MongoDB數(shù)據(jù)庫示例分享

    Java操作MongoDB數(shù)據(jù)庫示例分享

    MongoDB是一個文檔型數(shù)據(jù)庫,是NOSQL家族中最重要的成員之一,以下代碼封裝了MongoDB的基本操作。具體都在備注當(dāng)中,要仔細(xì)看哦
    2014-08-08
  • Mongodb中MapReduce實現(xiàn)數(shù)據(jù)聚合方法詳解

    Mongodb中MapReduce實現(xiàn)數(shù)據(jù)聚合方法詳解

    Mongodb是針對大數(shù)據(jù)量環(huán)境下誕生的用于保存大數(shù)據(jù)量的非關(guān)系型數(shù)據(jù)庫,針對大量的數(shù)據(jù)。接下來通過本文給大家介紹Mongodb中MapReduce實現(xiàn)數(shù)據(jù)聚合方法詳解,感興趣的朋友一起學(xué)習(xí)吧
    2016-05-05
  • mongodb 命令行下及php中insert數(shù)據(jù)詳解

    mongodb 命令行下及php中insert數(shù)據(jù)詳解

    這篇文章主要介紹了mongodb 命令行下及php中insert數(shù)據(jù)詳解,需要的朋友可以參考下
    2014-07-07
  • MongoDB集合的增刪改查管理

    MongoDB集合的增刪改查管理

    這篇文章介紹了MongoDB集合的增刪改查管理,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-07-07
  • Linux系統(tǒng)下安裝MongoDB的詳細(xì)方法圖文教程

    Linux系統(tǒng)下安裝MongoDB的詳細(xì)方法圖文教程

    這篇文章主要介紹了Linux系統(tǒng)下安裝MongoDB的詳細(xì)方法圖文教程,需要的朋友可以參考下
    2023-06-06
  • 淺析mongodb中g(shù)roup分組

    淺析mongodb中g(shù)roup分組

    這篇文章主要介紹了淺析mongodb中g(shù)roup分組的實現(xiàn)方法及示例,非常的簡單實用,有需要的小伙伴可以參考下。
    2015-05-05

最新評論