Ruby on rails Can';t使用BCrypt在Rails上进行身份验证-密码哈希是不同的

Ruby on rails Can';t使用BCrypt在Rails上进行身份验证-密码哈希是不同的,ruby-on-rails,ruby,authentication,hash,Ruby On Rails,Ruby,Authentication,Hash,我试图创建一个简单的身份验证系统,但我似乎有一个问题 注册过程很好,但当我尝试使用完全相同的信息登录时,我不能(我收到“无效的电子邮件或密码”)。正如我看到的,散列比较返回false。这是我的密码: #sessions_controller.rb def create user = User.authenticate(params[:email], params[:password]) if user session[:user_id] = user.id redire

我试图创建一个简单的身份验证系统,但我似乎有一个问题

注册过程很好,但当我尝试使用完全相同的信息登录时,我不能(我收到“无效的电子邮件或密码”)。正如我看到的,散列比较返回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 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

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

Bcrypt的解密方法正确,但你的代码中的罪魁祸首是保存前:加密密码只需将保存前事件更改为创建前事件即可。使用before_save,每次更新用户记录时,都会调用encrypt_password,并以这种方式加密password字段,从而丢失第一个加密密码,尽管您提供了正确的密码,但该密码始终不匹配。我也遇到了同样的问题,经过深入分析,我知道了解决办法

密码显然不是您散列的密码:),否则这将按预期工作。你确定没有空格或特殊字符吗?我建议使用Desive而不是重新发明轮子。使用Desive比使用自己的身份验证系统安全得多。转到
config/application.rb
并设置
config.filter\u参数=[]
重新启动应用程序,创建用户并尝试登录。把所有的日志都放在这里。在您这样做之后,我们可以确认bcrypt(极不可能)或您的数据处理(可能是999999)中是否存在错误。越来越近!从日志中,保存到数据库的第一个密码是“password”
BCrypt::Engine.hash_secret(“密码”,“$2a$10$tpDFvkFUC.OPckDm6xacU.”)=“$2a$10$tpDFvkFUC.OPckDm6xacU.xkfmecg2cdpsi3cjtjnx6k58ujhon6”
。由于这与您的异常检测显示的情况不同,因此我冒昧地猜测数据保存正确,但您将错误或损坏的参数传递给会话
create
@watt:继续删除过滤器,它们可能会告诉您发生了什么。当您输入“password”(根据日志,在第三次未捕获的抛出中,但当您输入“password”)时,您是否也可以为该案例提供失败的哈希?这是gold。不确定这是否是OP的问题,这正是我在尝试运行自己的帐户电子邮件确认设置时遇到的问题。
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)