簡體   English   中英

Rails Devise gem中的密碼加密問題

[英]Password encryption problem in Rails Devise gem

我現在更改為使用Gem Devise進行用戶身份驗證。 但我不知道如何匹配加密!

我知道我們可以編寫一個新的加密器並將其分配給初始化器,但重點是加密器只接受4個參數(密碼,延伸,鹽,胡椒)。 但就我而言,我確實在加密中包含了用戶的電子郵件和自定義的鹽。

是否可以將用戶的電子郵件和自定義鹽傳遞給加密器?

PS。 我正在使用database_authenticatable

很遺憾沒有人回答我的問題......

但是,我想我找到了答案,雖然它沒有我想象的那么漂亮。

首先,在初始值設定項中創建加密類:

module Devise
  module Encryptors
    class MySha1 < Base
      def self.digest(password, salt)
        Digest::SHA1.hexdigest("#{salt}-----#{password}")
      end

      def self.salt(email)
        Digest::SHA1.hexdigest("#{Time.now}-----#{email}")
      end
    end
  end
end

其次,覆蓋User模型中的一些方法:

# overwrite this method so that we call the encryptor class properly
def encrypt_password
  unless @password.blank?
    self.password_salt = self.class.encryptor_class.salt(email)
    self.encrypted_password = self.class.encryptor_class.digest(@password, self.password_salt)
  end
end

# Because when the database_authenticatable wrote the following method to regenerate the password, which in turn passed incorrect params to the encrypt_password, these overwrite is needed!
def password=(password)
  @password = password
end
def password_digest(pwd)
  self.class.encryptor_class.digest(pwd, self.password_salt)
end

最后,我們必須教授何時加密密碼:

before_save :encrypt_password

我不確定這有多大,但這似乎是一種更好的支持方式: http//presentations.royvandewater.com/authentication-with-devise.html#9

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM