为了开发一个新的网络应用程序,我的注册页面(仅限管理员)只需要一个电子邮件字段。
问题是我对 Rails 完全陌生,所以即使是像这样的基础知识对我来说也非常困难......
我使用 Railscast #270 创建了我的身份验证,它使用有安全密码方法。
现在,一切都很好,除了我不需要这些废话......
我还想使用 Action Mailer 将生成的密码发送到他的电子邮件地址。
十六进制(8)密码将是完美的(我见过安全随机但好像已经贬值了)
用户控制器:
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
目前,在我看来,我有两个领域。但正如我之前所说,我只想要一个。
我想继续使用 has_secure_password ,它似乎提供了关于哈希/盐的相当好的安全性。
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
为了更紧凑地表示相同数量的实际随机性)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)