ruby - 如何在 Rack 中使用自定义 cookie session 序列化程序?

标签 ruby rack

我目前正在集成 Warden到我正在构建的新 Rack 应用程序中。我想实现 recent patch Rack 允许我指定 session 的序列化方式;具体来说,我想使用 Rack::Session::Cookie::Identity 作为 session 处理器。

不幸的是,文档有点不清楚我应该使用什么语法在我的rackup文件中配置Rack::Session::Cookie,这里有人可以告诉我我做错了什么吗?

config.ru

require 'my_sinatra_app'

app = self

use Rack::Session::Cookie.new(app, Rack::Session::Cookie::Identity.new), {:key => "auth_token"}

use Warden::Manager do |warden| # Must come AFTER Rack::Session
  warden.default_strategies :password
  warden.failure_app Jelli::Auth.run!
end

run MySinatraApp

来自thin的错误消息

!! Unexpected error while processing request: undefined method `new' for #<Rack::Session::Cookie:0x00000110124128>

PS:我使用 bundler 来管理我的 gem 依赖项,并且我同样包含了rack的主分支作为所需的版本。

更新:按照下面评论中的建议,我已阅读 documentation ;遗憾的是,文档中建议的语法不起作用。

更新:我仍然不走运;向任何能帮助我解决这个问题的人提供赏金。

最佳答案

require 'my_sinatra_app'

app = self

session_app = Rack::Session::Cookie.new(app, {
        :coder => Class.new {
                def encode(str); str; end
                def decode(str); str; end
              }.new
      })

use session_app

run MySinatraApp

我看了一下源代码,Rack::Session::Cookie::Identity 很简单:

class Identity
  def encode(str); str; end
  def decode(str); str; end
end

那么这应该足够了。我已经在 Thin 下运行了它,并得到了一个 hello world 应用程序 - 这就是我所做的。

关于ruby - 如何在 Rack 中使用自定义 cookie session 序列化程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4550546/

相关文章:

ruby - rspec 中定义的匿名类不会响应新的

ruby-on-rails - 在救援中输出错误 (Ruby/Rails)

ruby-on-rails - Rails 3 请求调度周期

ruby-on-rails - Rack Gem::LoadError 即使我使用的是 Bundler

ruby - 无法阻止 "rackup"启动的 Webrick

ruby - 从 TestCase 调用 Sinatra 应用程序实例方法

ruby : How to write a gem?

ruby - 如何在 Ruby 中生成比特币地址

ruby-on-rails - Date.today 与实际日期不对应

ruby - 使用 Rack::Session::Cookie 删除当前 session