我發現密碼摘要在我的rails 4應用程序中無法正常工作 - 密碼摘要以明文形式存儲在數據庫中。使用rails4和use_secure_password以明文形式存儲密碼摘要
user.rb
class User < ActiveRecord::Base
has_secure_password
validates :password_digest, length: { minimum: 6 }
end
我的用戶數據庫遷移文件如下:
class CreateUsers < ActiveRecord::Migration
def change
create_table :users do |t|
t.string :full_name
t.string :email
t.string :password_digest
t.timestamps null: false
end
end
end
任何想法我做錯了嗎?
編輯 我在Gemfile中bcrypt啓用(我跑了捆綁安裝)
gem 'bcrypt', '~> 3.1.7'
當我嘗試在軌控制檯測試中,我得到一個無效的哈希錯誤:
2.2.1 :011 > user = User.new(full_name: 'abcd', email: '[email protected]', password_digest: 'abcdef')
=> #<User id: nil, full_name: "abcd", email: "[email protected]", password_digest: "abcdef", created_at: nil, updated_at: nil>
2.2.1 :012 > User.find_by(full_name: 'david').try(:authenticate, 'abcdef')
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."full_name" = ? LIMIT 1 [["full_name", "david"]]
BCrypt::Errors::InvalidHash: invalid hash
from /usr/local/rvm/gems/ruby-2.2.1/gems/bcrypt-3.1.10/lib/bcrypt/password.rb:60:in `initialize'
from /usr/local/rvm/gems/ruby-2.2.1/gems/activemodel-4.2.4/lib/active_model/secure_password.rb:102:in `new'
from /usr/local/rvm/gems/ruby-2.2.1/gems/activemodel-4.2.4/lib/active_model/secure_password.rb:102:in `authenticate'
您是否在您的Gemfile中啓用了'bcrypt' gem? –
是 - gem'bcrypt','〜> 3.1.7' – user3813256