Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ssl/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用bcrypt和mongoid保护用户密码_Mongoid_Bcrypt_Bcrypt Ruby - Fatal编程技术网

如何使用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
    字段:名称,类型:字符串
    字段:密码摘要,类型:字符串
    有安全的密码吗
    结束