【问题标题】:Custom authentication strategy for devise用于设计的自定义身份验证策略
【发布时间】:2011-05-12 12:08:51
【问题描述】:

我需要为https://github.com/plataformatec/devise 编写自定义身份验证策略,但似乎没有任何文档。它是怎么做的?

【问题讨论】:

    标签: ruby-on-rails ruby devise


    【解决方案1】:

    我在设计谷歌组的this thread 中发现了这个非常有用的 sn-p

    initializers/some_initializer.rb:

    Warden::Strategies.add(:custom_strategy_name) do 
      def valid? 
        # code here to check whether to try and authenticate using this strategy; 
        return true/false 
      end 
    
      def authenticate! 
        # code here for doing authentication; 
        # if successful, call  
        success!(resource) # where resource is the whatever you've authenticated, e.g. user;
        # if fail, call 
        fail!(message) # where message is the failure message 
      end 
    end 
    

    将以下内容添加到初始化程序/devise.rb

      config.warden do |manager| 
         manager.default_strategies.unshift :custom_strategy_name 
      end 
    

    【讨论】:

    • 谢谢,非常有用,我用它来验证旧网站的 joomla 用户:-)
    • 非常有帮助的答案,但链接已损坏。你能纠正一下吗?
    • 我强烈建议在实施您自己的策略时使用DatabaseAuthenticatable 作为模板,否则您可能会遇到可记忆无法工作的问题,其设置是在调用validate 时完成的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-03-23
    • 2012-01-29
    • 2016-08-04
    • 2020-07-03
    • 1970-01-01
    • 1970-01-01
    • 2016-09-17
    相关资源
    最近更新 更多