0

我正在尝试创建一个简单的身份验证系统,但我似乎遇到了问题。

注册过程正常,但是当我尝试使用完全相同的信息登录时,我不能(我收到“无效的电子邮件或密码”)。如我所见,哈希比较返回 false。这是我的代码:

#sessions_controller.rb

def create
  user = User.authenticate(params[:email], params[:password])
  if user
    session[:user_id] = user.id
    redirect_to root_url, :notice => "Logged in!"
  else
    flash.now.alert = "Invalid email or password"
    render "new"
  end
end

class User < ActiveRecord::Base
attr_accessor :password
before_save :encrypt_password

validates_confirmation_of :password
validates_presence_of :password, :on => :create
validates_presence_of :name
validates_presence_of :email
validates_uniqueness_of :email

def self.authenticate(email, password)
  user = User.where(email: email).first
  # throw Exception.new(user.password_hash) #uncaught throw #<Exception: $2a$10$9FHhPyb7BW01ktwTTgZHX.hlKKv4ajX/dX9D/xNGmZoajJTdGG4N.>
  # throw Exception.new(user.password_salt) #uncaught throw #<Exception: $2a$10$9FHhPyb7BW01ktwTTgZHX.>
  # throw Exception.new(BCrypt::Engine.hash_secret(password, user.password_salt)) #uncaught throw #<Exception: $2a$10$9FHhPyb7BW01ktwTTgZHX.O62xalJit020Jb0g5XDdB5V8dGMslQS>
  if user && user.password_hash == BCrypt::Engine.hash_secret(password, user.password_salt)
    user
  else
    nil
  end
end

def encrypt_password
  if password.present?
    self.password_salt = BCrypt::Engine.generate_salt
    self.password_hash = BCrypt::Engine.hash_secret(password, password_salt)
  end
end
end

因此,正如您在 user.rb 中的注释行中看到的那样,我在尝试登录时获得的密码哈希与原始密码不一样。显然,我输入的密码是正确的。

user.password_hash = $2a$10$9FHhPyb7BW01ktwTTgZHX.hlKKv4ajX/dX9D/xNGmZoajJTdGG4N.
user.password_salt = $2a$10$9FHhPyb7BW01ktwTTgZHX.
BCrypt::Engine.hash_secret(password, user.password_salt) = $2a$10$9FHhPyb7BW01ktwTTgZHX.O62xalJit020Jb0g5XDdB5V8dGMslQS

你能在这里给我一个提示吗?我做错了什么?

非常感谢!


//稍后编辑:还添加用户控制器,也许这会有所帮助。

class UsersController < ApplicationController

  def new
    @user = User.new(user_params)
  end

  def create
    @user = User.new(user_params)
    if @user.save
      redirect_to root_url, :notice => "Signed up!"
    else
      render "new"
    end   
  end

  private
    def user_params
      params.fetch(:user).permit(:name, :email, :password, :password_confirmation) if params[:user]
    end

end

编辑:发布用于注册/登录的日志

Started GET "/sign_up" for 127.0.0.1 at 2013-10-11 11:23:13 +0300
Processing by UsersController#new as HTML
  Rendered users/new.html.erb within layouts/application (31.8ms)
Completed 200 OK in 48ms (Views: 41.8ms | ActiveRecord: 1.2ms)


Started POST "/users" for 127.0.0.1 at 2013-10-11 11:24:30 +0300
Processing by UsersController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"LPLEs9at6BLGgjikYynnEzA/JAMMVl9IYGId1zEyNEg=", "user"=>{"name"=>"johntest", "email"=>"johntest@johntest.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Create User"}
   (0.1ms)  BEGIN
  User Exists (0.4ms)  SELECT 1 AS one FROM `users` WHERE `users`.`email` = BINARY 'johntest@johntest.com' LIMIT 1
  SQL (0.3ms)  INSERT INTO `users` (`created_at`, `email`, `name`, `password_hash`, `password_salt`, `updated_at`) VALUES ('2013-10-11 08:24:30', 'johntest@johntest.com', 'johntest', '$2a$10$tpDFvkFUC.OPckDm6xacU.xkjFmECg2CDpsi3cjTJNX6K58ujHOn6', '$2a$10$tpDFvkFUC.OPckDm6xacU.', '2013-10-11 08:24:30')
   (39.2ms)  COMMIT
Redirected to http://localhost:3000/
Completed 302 Found in 141ms (ActiveRecord: 40.0ms)

Started GET "/" for 127.0.0.1 at 2013-10-11 11:24:30 +0300
Processing by TroublesController#frontpage as HTML
  Trouble Load (0.2ms)  SELECT `troubles`.* FROM `troubles`
  CACHE (0.0ms)  SELECT `troubles`.* FROM `troubles`
  Rendered troubles/_marker_infowindow.html.erb (0.8ms)
  Rendered troubles/_marker_infowindow.html.erb (0.1ms)
  Rendered /home/alex/.rvm/gems/ruby-2.0.0-p247/gems/gmaps4rails-1.5.6/app/views/gmaps4rails/_gmaps4rails.html.erb (1.9ms)
  Rendered troubles/frontpage.html.erb within layouts/application (3.9ms)
Completed 200 OK in 21ms (Views: 13.5ms | ActiveRecord: 0.2ms)

[...](loading assets)

Started GET "/log_in" for 127.0.0.1 at 2013-10-11 11:24:52 +0300
Processing by SessionsController#new as HTML
  Rendered sessions/new.html.erb within layouts/application (1.1ms)
Completed 200 OK in 14ms (Views: 12.8ms | ActiveRecord: 0.0ms)


Started POST "/sessions" for 127.0.0.1 at 2013-10-11 11:25:05 +0300
Processing by SessionsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"LPLEs9at6BLGgjikYynnEzA/JAMMVl9IYGId1zEyNEg=", "name"=>"johntest", "email"=>"johntest@johntest.com", "password"=>"[FILTERED]", "commit"=>"Log in"}
  User Load (0.4ms)  SELECT `users`.* FROM `users` WHERE `users`.`email` = 'johntest@johntest.com' ORDER BY `users`.`id` ASC LIMIT 1
  Rendered sessions/new.html.erb within layouts/application (1.7ms)
Completed 200 OK in 99ms (Views: 10.9ms | ActiveRecord: 0.4ms)

[...](loading assets)

所以我去了注册页面,填写了详细信息,我被转发到主页,上面写着“已注册!”。我点击登录,输入详细信息,它显示“无效的电子邮件或密码”。

4

1 回答 1

1

Bcrypt 以正确的方式解密,但代码中的罪魁祸首是before_save :encrypt_password只需将之前的保存事件更改为before_create事件。使用 before_save ,每次更新用户记录时都会调用 encrypt_password 并且它会加密密码字段,这样您就会丢失第一个加密密码,尽管您提供了正确的密码,但该密码永远不会匹配。我遇到了同样的问题,经过深入分析后,我知道了解决方法。

于 2015-06-26T23:19:40.947 回答