2017-10-17 75 views
1

我有一个新的应用程序,根用户创建另一个用户,当我用模型创建用户。Devise Rails密码

@user = User.new(:email => params[:email], :password => params[:password], :password_confirmation => params[:password_confirmation]) 
@user.save! 

的用户节省了“正确”关于“encrypted_pa​​ssword”的价值和其他领域的表格也一样,但是当我尝试登录我有一个错误 “无效的电子邮件或密码”所以,当我阅读记录密码是。

encrypted_password: $2a$11$wFnpiA.l9HezNXfnAGkttuu2IGIXByETytLrEkdDsa8sBFrc8Bdmq 

但我用另一个密码的实际工作的根密码。 但在表:

encrypted_password: $2a$11$VKOAUk5pjILU1QHYmkpJSem9KKm70QJPS7Oj.nPM/pTuyu1tqZaQO 

所以,我的色器件模型不保存密码的正确的加密。

我的TestController:

class UsersController < ApplicationController 
    before_action :authenticate_user! 

    def index 
    @Users = User.all 
    end 

def create 
    begin 
     @user = User.new(:email => params[:email], :password => params[:password_first], :password_confirmation => params[:password_confirmation]) 
     @user.save! 
     flash[:notice] = "Usuario creado correctamente" 
     redirect_to action: 'index' 
    rescue Exception => e   
      flash[:alert] = "Error al crear al Usuario: " + e.message 
      redirect_to action: 'index' 
    end 
end 

end 

我到底做错了什么?

问候

+1

绝不要做'rescue Exception => e'这是一个非常糟糕的做法,它会吞噬基本的错误,例如'NoMethodError'和语法错误,并且使调试变得非常困难。使用'if @user。保存“,如果为false则渲染新视图,以便用户可以更正任何验证错误。不应将救援用于正常的控制流程。 – max

+0

嗨,我只是想弄清楚这一点。 我的用户有这个通:123 和餐桌上的密码: $ 2A $ $ 11 VKOAUk5pjILU1QHYmkpJSem9KKm70QJPS7Oj.nPM/pTuyu1tqZaQO 但是,当我在模型上创建一个新用户的密码更改为: $ 2A $ $ 11 drZIcAsXTHxyvtbvL3Wg5e0EPG4JJpsAlmxe78ohEjDSA3/nCzHoa 这就是我的问题,为什么该模型不使用Devise的加密。 – UlyssesMarx

+0

好吧'救援例外=> e'除了清除垫子上的错误之外什么都不做。 – max

回答

1

BCrypt超出它的方式来扰乱密码尽可能使其极难扭转哈希操作。对于任何给定的输入字符串BCrypt::Password.create有大约6800000亿万亿万亿个可能输出字符串(2 ),所以这是不可能的,你永远得到两次同样的一个:

hashes = 10.times.map { BCrypt::Password.create('test') } 
# => [ 
# "$2a$10$vMrgjJHqvwnEKIs0fZ76pO3gbWL/0C3ExqK9HOpi/mHYu2.4GAO2K", 
# "$2a$10$KxBOarDzRPHp7QF1GGqNnuplRs1B5rNVfp21IHx1/HzQ0YIcIkLRW", 
# "$2a$10$emCdZAA.GU8GwQZkeJLfAuUTY2aEnhFmZ.GQAhDpJ.JGSh/m6s/k2", 
# "$2a$10$6R6xmGyK7Tb1MKsQb00vpOJKwpi56aj98JLoBJhBN4vWSQb7zagQm", 
# "$2a$10$r4qmb.C.vm88pL2nJK5TdOaWIboYaO6a1xHIRH.QDER6qYR6Ajvo.", 
# "$2a$10$mlVWz4IHTgYHSf3tAgEgpenpDHtGWYev4EUENLs7hnLlm6ikPhUxy", 
# "$2a$10$ixXdZZuc9rIVAozO8tyq5.wlsVOWBc6QWetNh3PvjPj2pGlqh.XOy", 
# "$2a$10$zLzuevtOl.g4RbaHpdeTZ.k4qjE/1m4nh6gN4mhcIKQPSa5sBcG5u", 
# "$2a$10$F/F71.DYEuzxS4W0w5m/a.IRpaVJxeh9sKUJ7DyQb5xU3SvFu1Ib.", 
# "$2a$10$ILXg8R52ZtHHbQbT0FxSFOj8YNqpNLmrH.6FhM3RGMwIuBeP1YXHa" ] 

这是正常的,虽然因为验证例程可以处理检查密码:

hashes.map { |h| BCrypt::Password.new(h) == 'test' } 
# => [true, true, true, true, true, true, true, true, true, true] 

它们全都匹配。重要的是要注意,验证十个密码确实需要很短的时间。这使得BCrypt特别适合用于密码存储:猜测是昂贵的,因此将巨大的词典放在哈希中以查看哪个匹配非常困难。对于像MD5或SHA1这样较弱的哈希来说,情况并非如此,其中每秒100万次操作完全可行。 BCrypt故意大约慢50万倍。

0

从定期体面的Rails crud设置开始,看看问题是否解决不了。

# routes.rb 
resources :users, only: [:create, :index] 

class UsersController < ApplicationController 

    before_action :authenticate_user! 

    # GET /users 
    def index 
    @Users = User.all 
    end 

    # POST /users 
    def create 
    @user = User.new(user_params) 
    if @user.save 
     flash[:success] = "Usuario creado correctamente" 
     redirect_to action: :index 
    else 
     render :new 
    end 
    end 

    def user_params 
    params.require(:user) 
      .permit(:email, :password, :password_confirmation) 
    end 
end 

# app/views/users/_form.html.erb 
<% form_for(@user || User.new) do |f| %> 
    <% if f.object.errors.any? %> 
    <div id="error_explanation"> 
    <h2><%= pluralize(f.object.errors.count, "error") %> prohibited this article from being saved:</h2> 
    <ul> 
     <% f.object.errors.errors.full_messages.each do |msg| %> 
     <li><%= msg %></li> 
     <% end %> 
    </ul> 
    </div> 
    <% end %> 

    <div class="field"> 
    <%= f.label :email %> 
    <%= f.email_field :email %> 
    </div> 
    <div class="field"> 
    <%= f.label :password %> 
    <%= f.password_field :password %> 
    </div> 
    <div class="field"> 
    <%= f.label :password_confirmation %> 
    <%= f.password_field :password_confirmation %> 
    </div> 
</div> 
<% end %> 

# app/views/users/new.html.erb 
<%= render partial: 'form' %> 

您可以嵌入在你的根视图中的部分或由用户索引:

<%= render partial: 'users/form' %> 

请注意,如果记录无效,您应该呈现并不重定向。如果您重定向用户输入,并且您想显示的任何验证消息都消失了。