对于一个新的网络应用程序,我将需要在我的注册页面(这是管理员),只有一个电子邮件字段。
问题是我对rails完全陌生,所以即使是最基本的东西对我来说也很难……
我使用Railscast #270创建了我的身份验证,它使用了has_secure_password方法。现在,一切都很好,除了我不需要这些废话.我还想使用Action将生成的密码发送到他的电子邮件附件。一个十六进制(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
User_model:
class User < ActiveRecord::Base
attr_accessible :email
has_secure_password
validates_presence_of :password, :email, :on => :create
end
现在,在我看来,我有两个字段。但正如我之前说的,我只想要一个。我想继续使用has_secure_password,它似乎为哈希/salt提供了相当好的安全性。
发布于 2012-01-30 15:33:30
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
,以获得同样数量的实际随机性的更紧凑的表示)
发布于 2012-10-15 06:56:27
下面是一个简单的随机密码代码,其长度为8。
rand_password=('0'..'z').to_a.shuffle.first(8).join
希望能帮上忙。
发布于 2012-01-30 15:32:26
有时Rails中的内容被废弃,因为它们重复了添加到Ruby中的功能,而SecureRandom似乎就是其中之一。
您可以使用任意这些随机生成器方法生成一次性使用的密码。
https://stackoverflow.com/questions/9066245
复制相似问题