和典狱长一起记住我

发布于 2024-10-08 06:50:26 字数 205 浏览 3 评论 0原文

对于我最新的项目,我使用 https://github.com/hassox/rails_warden。它非常适合我的需求,只是我找不到实现 Remember_me 的好方法。我知道从安全角度来看,记住我是非常困难的,所以我希望有一个项目可以完成这项工作。有人看到过什么或者有什么好主意吗?

For my lastest project I'm using https://github.com/hassox/rails_warden. It suits my needs very well except that I can't find a good way to implement remember_me. I know that it's notoriously difficult to get remember_me right from a security point of view so I'm hoping there's a project out there that will do the job. Anyone seen anything or get a good idea?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

起风了 2024-10-15 06:50:26

好的,这就是我解决的方法

 # User model must have remember_token attribute

 # in config.ru
 use Rack::Cookies
 run MyApp

  # in lib/strategies.rb
  Strategies.add(:cookie) do
    def valid?
      env['rack.cookies']['user.remember.token']      
    end

    def authenticate!
      if user = User.find_by_remember_token(cookies['user.remember.token'])
        success! user
      else
        fail! "Could not log in"
      end
    end
  end

  Manager.after_authentication :scope => :user do |user, auth, opts|
    auth.env['rack.cookies']['user.remember.token'] = user.generate_remember_token! # sets its remember_token attribute to some large random value and returns the value
  end

  Manager.before_logout :scoper => :user do |user, auth, opts|
    user.update_attribute :remember_token, nil
  end

Ok here's how I solved it

 # User model must have remember_token attribute

 # in config.ru
 use Rack::Cookies
 run MyApp

  # in lib/strategies.rb
  Strategies.add(:cookie) do
    def valid?
      env['rack.cookies']['user.remember.token']      
    end

    def authenticate!
      if user = User.find_by_remember_token(cookies['user.remember.token'])
        success! user
      else
        fail! "Could not log in"
      end
    end
  end

  Manager.after_authentication :scope => :user do |user, auth, opts|
    auth.env['rack.cookies']['user.remember.token'] = user.generate_remember_token! # sets its remember_token attribute to some large random value and returns the value
  end

  Manager.before_logout :scoper => :user do |user, auth, opts|
    user.update_attribute :remember_token, nil
  end
南城追梦 2024-10-15 06:50:26

Devise 是 Warden 之上的身份验证解决方案,具有 可记住实现。

Devise, which is an authentication solution on top of Warden, has a rememberable implementation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文