3

我创建了自己的 Warden 策略,用于与 Devise 一起使用。它与 Devise::Strategies::DatabaseAuthenticatable 非常相似,实际上它继承自它并重新实现了身份验证!

不过,我的问题是我想确保原来的 devise_authenticable Warden 策略已经消失。这不在 Warden 将尝试的策略列表中,因为它实际上是一个安全问题。那可能吗?

4

2 回答 2

3

根据我的手动检查和测试,这在 devise.rb 初始化程序中实现了目标:

config.warden do |manager|
  strategies = manager.default_strategies(:scope => :user)
  strategies[strategies.index(:database_authenticatable)] = :alternative_strategy
end

并且该策略是以这种方式实施的(不是这个问题的一部分,但我发现那里的信息相互矛盾,这个是使用 Rails 3.1、devise 1.4.7 和warden 1.0.5 为我工作的那个):

class AlternativeStrategy < Devise::Strategies::Authenticatable
  def authenticate!
  end
end
Warden::Strategies.add(:alternative_strategy, AlternativeStrategy)
于 2011-09-26T08:18:08.217 回答
0

我也刚刚实现了这一点。设计将尝试其列表中的每个策略,直到成功。

对我来说,我没有替换 :database_authenticatable 策略,而是将我的策略添加到列表的开头,并从现有列表的末尾弹出 :database_authenticatable 。

config.warden do |manager|
  # Exiles::Devise::Strategies::BySite implemented in lib/.  It matches the stub in Pablo's answer
  manager.strategies.add( :by_site_auth, Exiles::Devise::Strategies::BySite )

  # add my strategy to the beginning of the list.
  manager.default_strategies(:scope => :user).unshift :by_site_auth

  # remove the default database_authenticatable strategy from the list
  manager.default_strategies(:scope => :user).pop
end
于 2013-04-12T19:50:16.427 回答