gpt4 book ai didi

ruby-on-rails - 与 jail 长记住_我

转载 作者:行者123 更新时间:2023-12-04 17:40:48 25 4
gpt4 key购买 nike

对于我最近的项目,我正在使用 https://github.com/hassox/rails_warden .除了我找不到实现remember_me的好方法之外,它非常适合我的需求。我知道从安全的角度来看,要正确地记住remember_me 是出了名的困难,所以我希望有一个项目可以完成这项工作。有没有人看到任何东西或得到一个好主意?

最佳答案

好的,这是我解决的方法

 # 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

关于ruby-on-rails - 与 jail 长记住_我,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4461984/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com