如何使用bcrypt和mongoid保护用户密码
我刚开始使用如何使用bcrypt和mongoid保护用户密码,mongoid,bcrypt,bcrypt-ruby,Mongoid,Bcrypt,Bcrypt Ruby,我刚开始使用MongoDB,尤其是Mongoid 当然,我希望确保我的用户的密码保持良好和安全,之前我会使用ActiveRecord并使用bcrypt。我正在寻找一种好的、干净的、安全的、简单的方法来使用Mongoid实现同样的事情 我已经看过了,但我还不知道如何使用它 假设我的简化用户如下所示,如mongoid encryptor自述文件中的示例所示 class User include Mongoid::Document include Mongoid::Encryptor fi
MongoDB
,尤其是Mongoid
当然,我希望确保我的用户
的密码保持良好和安全,之前我会使用ActiveRecord
并使用bcrypt
。我正在寻找一种好的、干净的、安全的、简单的方法来使用Mongoid
实现同样的事情
我已经看过了,但我还不知道如何使用它
假设我的简化用户
如下所示,如mongoid encryptor
自述文件中的示例所示
class User
include Mongoid::Document
include Mongoid::Encryptor
field :name
field :password
encrypts :password
end
在我的WebApp中(在本例中使用Sinatra
),我会定义一个助手,例如
def login (name, cleartxtpass)
return User.where(name: name, password: cleartxtpass).first
end
bcrypt
cleartxtpass
进行任何预处理,还是将Mongoid::Encryptor
直接处理?从文件上看不清楚好吧,经过一番挖掘,我决定不再麻烦使用
Mongoid::Encryptor
,而是坚持使用ActiveRecord
时使用的经过尝试和测试的方法
所以现在我的用户
看起来像
class User
include Mongoid::Document
field :name, type: String
field :password_hash, type: String
index({name: 1}, {unique: true, name: 'user_name_index'})
include BCrypt
def password
@password ||= Password.new(password_hash)
end
def password=(new_password)
@password = Password.create(new_password)
self.password_hash = @password
end
end
def auth_user(username, password)
user = User.where(name: username).first
return user if user && user.password == password
return nil
end
我的authenticate helper方法如下所示
class User
include Mongoid::Document
field :name, type: String
field :password_hash, type: String
index({name: 1}, {unique: true, name: 'user_name_index'})
include BCrypt
def password
@password ||= Password.new(password_hash)
end
def password=(new_password)
@password = Password.create(new_password)
self.password_hash = @password
end
end
def auth_user(username, password)
user = User.where(name: username).first
return user if user && user.password == password
return nil
end
这真是一种享受。好吧,经过一番挖掘之后,我决定不再费心使用
Mongoid::Encryptor
,而是坚持使用ActiveRecord
时使用的经过尝试和测试的方法
所以现在我的用户
看起来像
class User
include Mongoid::Document
field :name, type: String
field :password_hash, type: String
index({name: 1}, {unique: true, name: 'user_name_index'})
include BCrypt
def password
@password ||= Password.new(password_hash)
end
def password=(new_password)
@password = Password.create(new_password)
self.password_hash = @password
end
end
def auth_user(username, password)
user = User.where(name: username).first
return user if user && user.password == password
return nil
end
我的authenticate helper方法如下所示
class User
include Mongoid::Document
field :name, type: String
field :password_hash, type: String
index({name: 1}, {unique: true, name: 'user_name_index'})
include BCrypt
def password
@password ||= Password.new(password_hash)
end
def password=(new_password)
@password = Password.create(new_password)
self.password_hash = @password
end
end
def auth_user(username, password)
user = User.where(name: username).first
return user if user && user.password == password
return nil
end
这是一种享受。简单的方法是:
类用户
include Mongoid::Document
包括ActiveModel::SecurePassword
字段:名称,类型:字符串
字段:密码摘要,类型:字符串
有安全的密码吗
结束
简单的方法是:
类用户
include Mongoid::Document
包括ActiveModel::SecurePassword
字段:名称,类型:字符串
字段:密码摘要,类型:字符串
有安全的密码吗
结束