Ruby on rails 从Restful身份验证迁移到Desive

Ruby on rails 从Restful身份验证迁移到Desive,ruby-on-rails,authentication,Ruby On Rails,Authentication,许多Rails2.3应用程序使用Restful身份验证,但该插件似乎与Rails3存在一些问题。在升级到Rails 3时,我一直在使用Desive。有没有办法从Restful身份验证顺利过渡到Desive?是否有人进行了迁移,展示了如何更新用户模型?我已将应用程序从Restful身份验证更新为Desive。以下是我的迁移: class AlterUsersForDevise < ActiveRecord::Migration def self.up remove_column

许多Rails2.3应用程序使用Restful身份验证,但该插件似乎与Rails3存在一些问题。在升级到Rails 3时,我一直在使用Desive。有没有办法从Restful身份验证顺利过渡到Desive?是否有人进行了迁移,展示了如何更新用户模型?

我已将应用程序从Restful身份验证更新为Desive。以下是我的迁移:

class AlterUsersForDevise < ActiveRecord::Migration
  def self.up
    remove_column :users, :name
    change_column :users, :email, :string, :default => "", :null => false, :limit => 128
    rename_column :users, :crypted_password, :encrypted_password
    change_column :users, :encrypted_password, :string, :limit => 128, :default => "", :null => false
    rename_column :users, :salt, :password_salt
    change_column :users, :password_salt, :string, :default => "", :null => false, :limit => 255
    add_column :users, :reset_password_token, :string
    change_column :users, :remember_token, :string, :limit => 255
    rename_column :users, :remember_token_expires_at, :remember_created_at

    add_column :users, :sign_in_count, :integer, :default => 0
    add_column :users, :current_sign_in_at, :datetime
    add_column :users, :last_sign_in_at, :datetime
    add_column :users, :current_sign_in_ip, :string
    add_column :users, :last_sign_in_ip, :string

    rename_column :users, :activation_code, :confirmation_token
    change_column :users, :confirmation_token, :string, :limit => 255
    rename_column :users, :activated_at, :confirmed_at

    add_column :users, :confirmation_sent_at, :datetime
  end

  def self.down
    add_column :users, :name, :string, :limit => 100, :default => ""
    rename_column :users, :encrypted_password, :crypted_password
    change_column :users, :crypted_password, :string, :limit => 40
    rename_column :users, :password_salt, :salt
    change_column :users, :salt, :string, :limit => 40
    remove_column :users, :reset_password_token
    change_column :users, :remember_token, :string, :limit => 40
    rename_column :users, :remember_created_at, :remember_token_expires_at

    remove_column :users, :sign_in_count
    remove_column :users, :current_sign_in_at
    remove_column :users, :last_sign_in_at
    remove_column :users, :current_sign_in_ip
    remove_column :users, :last_sign_in_ip

    rename_column :users, :confirmation_token, :activation_code
    change_column :users, :confirmation_token, :string, :limit => 40
    rename_column :users, :confirmed_at, :activated_at

    remove_column :users, :confirmation_sent_at
  end
end
class AlterUsersForDevise“”、:null=>false、:limit=>128
重命名列:用户,:加密密码,:加密密码
更改列:用户,:加密密码,:字符串,:限制=>128,:默认=>“”,:空=>false
重命名列:用户,:salt,:密码\u salt
更改列:users,:password,:string,:default=>false,:limit=>255
添加列:用户,:重置\u密码\u令牌,:字符串
更改列:用户,:记住\u标记,:字符串,:限制=>255
重命名列:用户,:记住\u令牌\u到期时间,:记住\u创建时间
添加列:users,:sign_in_count,:integer,:default=>0
添加列:用户,:当前登录,:日期时间
添加列:用户,:上次登录,:日期时间
添加列:用户,:当前\u登录\u ip,:字符串
添加列:users,:last\u sign\u in\u ip,:string
重命名\列:用户,:激活\代码,:确认\令牌
更改列:用户,:确认\u标记,:字符串,:限制=>255
重命名列:用户,:已激活,:已确认
添加列:用户,:确认\u发送时间,:日期时间
结束
def自动关闭
添加列:users,:name,:string,:limit=>100,:default=>
重命名列:用户,:加密的\u密码,:加密的\u密码
更改列:用户,:加密密码,:字符串,:限制=>40
重命名列:用户,:密码\u salt,:salt
更改列:users,:salt,:string,:limit=>40
删除\u列:用户,:重置\u密码\u令牌
更改列:用户,:记住\u标记,:字符串,:限制=>40
重命名列:用户,:记住\u已创建\u在,:记住\u令牌\u过期\u在
删除列:用户,:在计数中签名
删除列:用户,:当前登录
删除列:用户,:最后一次登录
删除列:用户,:当前\u登录\u ip
删除列:用户,:最后一次\u登录\u ip
重命名列:用户,:确认\u令牌,:激活\u代码
更改列:用户,:确认\u标记,:字符串,:限制=>40
重命名列:用户,:已确认,:已激活
删除列:用户,:确认\u已发送\u
结束
结束
到目前为止,我的应用程序还没有上线。因此,我使用Desive中的密码加密,而不是Restful授权中的密码加密。若您的应用程序已经处于活动状态,并且您有活动用户,那个么您应该将Desive配置为使用SHA1,从Restful身份验证到加密和解密密码。否则,所有用户都必须请求新密码

您可以在Desive初始值设定项中对此进行配置


希望这能有所帮助……

我的密码加密有问题(但我找到了答案,请参阅我的其他回复)。旧应用使用了旧版本的Restful身份验证。它是这样处理密码加密的:

# before filter
def encrypt_password
  return if password.blank?
  self.salt = Digest::SHA1.hexdigest("--#{Time.now.to_s}--#{login}--") if new_record?
  self.crypted_password = encrypt(password)
end

# Encrypts some data with the salt.
def self.encrypt(password, salt)
  Digest::SHA1.hexdigest("--#{salt}--#{password}--")
end

# Encrypts the password with the user salt
def encrypt(password)
  self.class.encrypt(password, salt)
end

如果我将designe的
config.encryptor
设置为
:restful\u authentication\u sha1
则不起作用。

以下是如何克服密码问题:

您需要制作一个自定义加密程序,如下所示:

# /config/initializers/devise_encryptor.rb
require "digest/sha1"  

module Devise
  module Encryptors
    class OldRestfulAuthentication < Base
      def self.digest(password, stretches, salt, pepper)
        Digest::SHA1.hexdigest("--#{salt}--#{password}--")
      end
    end
  end
end

那就够了

这里有一个很好的关于从restful_身份验证迁移到Desive的指南

编辑原因:之前的链接将人们带到一个空白页。

在我的例子中,它是有效的(analized authentication.rb和by_password.rb在旧的gem restful_认证中):

config/initializers/designe.rb添加以下内容:

config.encryptor = :restful_authentication
config.stretches = 10 #REST_AUTH_DIGEST_STRETCHES frome Restful Authentication file config/initializers/site_key.rb
config.pepper = 'mashauronilavrechkumyachik' #REST_AUTH_SITE_KEY frome Restful Authentication file config/initializers/site_key.rb
app/models/user.rb添加:可加密

devise :database_authenticatable, :registerable,
       :recoverable, :rememberable, :trackable, :validatable,
       :encryptable, :omniauthable, :authentication_keys => [:login]
config/initializers/designe_encryptor.rb使用以下内容创建:

# -*- encoding : utf-8 -*-
require "digest/sha1"

module Devise
  module Encryptable
    module Encryptors
      class RestfulAuthentication < Base

        def self.digest(password, stretches, salt, pepper)
          digest = pepper
          stretches.times do
            digest = secure_digest(digest, salt, password, pepper)
          end
          digest
        end

        def self.secure_digest(*args)
          Digest::SHA1.hexdigest(args.flatten.join('--'))
        end

        def self.encrypt_password
          return if password.blank?
          self.password_salt = make_token if new_record?
          self.encrypted_password = encrypt(password)
        end

        def self.make_token
          secure_digest(Time.now, (1..10).map{ rand.to_s })
        end

        def self.encrypt(password)
          self.password_digest(password, stretches, salt, pepper)
        end
      end
    end
   end
end
#-*-编码:utf-8-*-
需要“摘要/sha1”
模块设计
可加密模块
模块加密机
类RestfulAuthentication
在另一个答案中查看我的解决方案。谢谢,这很有效。我还有一个问题,我在下面的2个答案中解决了。更正了以前导致空白页面的链接,该页面上写着“创建新页面”
# -*- encoding : utf-8 -*-
require "digest/sha1"

module Devise
  module Encryptable
    module Encryptors
      class RestfulAuthentication < Base

        def self.digest(password, stretches, salt, pepper)
          digest = pepper
          stretches.times do
            digest = secure_digest(digest, salt, password, pepper)
          end
          digest
        end

        def self.secure_digest(*args)
          Digest::SHA1.hexdigest(args.flatten.join('--'))
        end

        def self.encrypt_password
          return if password.blank?
          self.password_salt = make_token if new_record?
          self.encrypted_password = encrypt(password)
        end

        def self.make_token
          secure_digest(Time.now, (1..10).map{ rand.to_s })
        end

        def self.encrypt(password)
          self.password_digest(password, stretches, salt, pepper)
        end
      end
    end
   end
end