【问题标题】:Randomly generated password Rails 3.1随机生成的密码 Rails 3.1
【发布时间】:2012-02-22 09:17:37
【问题描述】:

对于一个新的网络应用程序,我需要在我的注册页面(仅限管理员)上只有一个电子邮件字段。

问题是我对 Rails 完全陌生,所以即使是像这样的基础知识对我来说也非常困难......

我使用 Railscast #270 创建了我的身份验证,它使用 has_secure_password 方法。 现在,一切都很好,除了我不需要所有这些废话...... 我还想使用 Action Mailer 将生成的密码发送到他的电子邮件地址。 一个 hex(8) 密码将是完美的(我见过 SecureRandom 但它似乎已被贬值)

Users_Controller:

class UsersController < ApplicationController
  skip_before_filter :is_connected?, :only => [:new, :create]

  def new
    @user = User.new
  end

  def create
    @user = User.new(params[:user])
    if @user.save
      # Tell the Mailer to send a welcome Email after save
      Mailer.confirm_email(@user).deliver

      redirect_to root_url, :notice => "Signed up!"
    else
      render "new"
    end
  end
end

用户模型:

class User < ActiveRecord::Base
  attr_accessible :email
  has_secure_password
  validates_presence_of :password, :email, :on => :create
end

目前,在我看来,我有 2 个字段。但正如我之前所说,我只想要一个。 我想继续使用 has_secure_password ,它似乎为哈希/盐提供了很好的安全性。

【问题讨论】:

  • 您的问题是“如何为我的用户设置密码并通过电子邮件发送给他们?”
  • 基本上我想写一些会生成密码并将其设置为变量的东西,以便可以从我的邮件视图中访问,并且女巫将作为密码清单存储在我的数据库中。我不知道该怎么做。
  • 好的,我照弗雷德里克说的做了。效果很好,但是有没有办法在不将 @random = SecureRandom.hex(8) 传递给视图的情况下做到这一点?因为我不知道该怎么做所以我将它作为隐藏字段传递给我的视图,然后我在我的创建方法中重用它......所以它的控制器>视图>控制器一无所有-_-
  • 在控制器的create方法中,设置密码,然后传给用户,再传给user_mailer。

标签: ruby-on-rails random passwords


【解决方案1】:

Rails 提供了ActiveSupport::SecureRandom,它(取决于 Ruby 版本)只是到 Ruby 的SecureRandom 的桥梁,或者在旧版本的 Ruby 上重新实现了它(如果我的记忆正确的话,SecureRandom 是在 1.8.7 中添加的)

现在 Rails 支持的所有 Ruby 版本都内置了 SecureRandom ActiveSupport::SecureRandom 不再需要并且已被弃用。 SecureRandom 本身无处可去 -

require 'securerandom'
SecureRandom.hex(8)

应该没问题(您可能需要考虑SecureRandom.urlsafe_base64 以更紧凑地表示相同数量的实际随机性)

【讨论】:

    【解决方案2】:

    这是一个长度为 8 的随机密码的简单代码

    rand_password=('0'..'z').to_a.shuffle.first(8).join
    

    希望它会有所帮助。

    【讨论】:

    • 完美!正是我需要的。谢谢! :)
    • 我使用rand_password = (('0'..'9').to_a + ('a'..'z').to_a + ('A'..'Z').to_a).shuffle.first(8).join 只获取没有特殊字符的字母数字符号
    • 您不应该使用安全随机数生成密码吗?
    • 问题是我总是得到相同的结果'GCjE2psB'
    【解决方案3】:

    有时,Rails 中的某些内容已被弃用,因为它们重复了已添加到 Ruby 核心的功能,而 SecureRandom 似乎就是其中之一。

    您可以使用这些随机生成器方法中的任何一种来生成一次性密码。

    【讨论】:

      【解决方案4】:

      创建Randomunique 令牌/密码

      class User < ActiveRecord::Base
      
        before_create :generate_password
      
        def generate_password
          self.password = loop do
            random_token = SecureRandom.urlsafe_base64
            # If you are using FFaker gem then you can use it otherwise
            # SecureRandom is great choice
            # random_token = FFaker::Internet.password
            break random_token unless User.exists?(password: random_token)
          end
        end
      end
      

      这里的主要目的是生成随机令牌,并且不要在数据库中重复该令牌。对于某些情况,例如生成 unique tokenunique invoice number 等,它可能非常有用

      【讨论】:

        猜你喜欢
        • 2010-09-08
        • 2012-11-06
        • 2015-04-13
        • 2012-07-28
        • 2011-09-05
        • 2011-04-18
        • 2018-11-01
        相关资源
        最近更新 更多