Rails:如何在不使用has_secure_password的情况下编写自己的身份验证

麦片

在我的应用中,管理员仅使用电子邮件创建用户。用户尝试设置密码时不起作用。下面的代码仅在我删除验证时有效。

def password=(password_str)
  @password = password_str 
  self.password_salt   = BCrypt::Engine.generate_salt
  self.password_digest = BCrypt::Engine.hash_secret(password_str, password_salt)
end

def authenticate(password)
  password.present? && password_digest.present? && password_digest == BCrypt::Engine.hash_secret(password, password_salt)
end

validates :password             , length: { minimum: 6 }, :if => :setting_password?
validates :password_confirmation, presence: true        , :if => :setting_password?

def setting_password?
 self.password || self.password_confirmation
end

1)我只有在评论密码验证时才能设置密码

2)password_confirmation字段按原样保存密码。

迈克·HR

对于大多数身份验证类型(以及我大部分时间使用的身份验证)而言,这是一个有用的解决方案,但是如果您确实需要在没有has_secure_password的情况下编写身份验证,那么Ryan Bates的Railscasts可以满足您的要求

class User < ActiveRecord::Base
  attr_accessible :email, :password, :password_confirmation

  attr_accessor :password
  before_save :encrypt_password

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

  def self.authenticate(email, password)
    user = find_by_email(email)
    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

对于rails 4,这是不同的(模型上没有更多的attr_accessors),对此进行修改,我们需要一个attr_reader:password以及一种设置密码和password_confirmation的方式,以便:

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

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

  def self.authenticate(email, password)
    user = find_by_email(email)
    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

  ##
  # accessor
  def password=(unencrypted_password)
    unless unencrypted_password.blank?
      @password = unencrypted_password
    end
  end

  ##
  # accessor
  def password_confirmation=(unencrypted_password)
    @password_confirmation = unencrypted_password
  end
end

但是,如果我们在这里查看has_secure_password方法,则会发现业务逻辑位于password =(未加密)中,而不是保存前的回调中(这很明显),我认为这可能是执行此操作的最干净的方法。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用Firebase身份验证,如何在不注销当前活动帐户的情况下创建帐户?

如何在不使用“权限”的情况下使用Spring Security的JDBC身份验证?

如何在Rails 4.2中使用has_secure_password加密密码

是否可以在不使用IIS的情况下使用Windows集成身份验证?

如何在不使用任务侦听器的情况下获取Firebase身份验证令牌?

如何在没有Windows身份验证的情况下使用Active Directory用户在SQL SERVER 2008中进行身份验证

如何在不进行身份验证的情况下使用python从telnet获取数据

如何在不使用Auth0通用登录的情况下配置auth0身份验证/授权?

如何在不设置环境变量的情况下使用Javascript向Google Sheets API进行身份验证?

如何在不使用websockets加密的情况下在节点js中创建基本身份验证系统?

如何在不使用密码提示的情况下不使用公共密钥身份验证而进行rsync?

Rails has_secure_password身份验证错误

如何在无需身份验证的情况下共享OneDrive(Office 365)文档?

Rails:has_secure_password不执行验证

在不使用Ember-cli的情况下使用简单的身份验证

如何在不使用Cognito的情况下对dynamoDB进行身份验证?

如何在不使用ASP控件的情况下对登录表单进行身份验证

Ember简单身份验证,如何在不使用私有API的情况下更新商店?

如何在不使用身份验证系统的情况下在django中更改密码?

Azure 自动化 - 如何在不使用资源管理器帐户的情况下进行身份验证

如何在没有标准身份验证的情况下使用 laravel RegisterController (php artisan make:Auth)

如何在没有任何 json 文件的情况下使用 Firebase Auth 进行身份验证?

如何在没有令牌身份验证的情况下使用 jupyter notebook?

如何在不提交服务密钥的情况下使用服务密钥对 gcloud SDK 进行身份验证?

在不使用 Django rest 框架的情况下对用户进行身份验证?

如何在不使用 public 的情况下编写脚本?

如何防止 Firestore 中的用户在不使用 Firebase 身份验证的情况下创建和阅读多个文档?

Firebase 身份验证:在不使用“sendPasswordResetEmail”的情况下更新(未登录用户)密码

如何在不使用 cloudfront 的情况下在 aws s3 中启用基本身份验证?