首页 > 解决方案 > Rails 5.2 Cache Store with Redis 不允许用户登录开发环境

问题描述

我在 docker 中运行 rails 5.2 应用程序,直到今天,会话存储一切正常,我尝试使用 Redis将session_storefrom更改为cookie_storeto 。cache_store

在我的 config/initializers/session_store.rb 文件中,我有这个,这是在开发中工作的唯一选项(注意cookie_store):

Rails.application.config.session_store :cookie_store,
                                           key: "_app_#{Rails.env}_session",
                                           domain: %w[.example.ru .example.io],
                                           expire_after: 24.hours,
                                           httponly: false

带有 的配置cache_store在开发环境中不起作用,但在生产环境中起作用。

Rails.application.config.session_store :cache_store,
                                       key: "_app_#{Rails.env}_session",
                                       domain: %w[.example.ru .example.io],
                                       expire_after: 24.hours,
                                       httponly: false

在我的环境文件中,我有这个: config.cache_store = :redis_cache_store, { url: ENV['REDIS_URL'] }

.env文件中我的 redis url 是REDIS_URL=redis://0.0.0.0:6379/0

并且控制台中没有错误日志。知道为什么 cache_store 不能在开发环境中工作吗?

顺便说一句,我正在使用 Devise gem 进行身份验证。

使用服务器日志更新

使用时记录cookie_store

web_1        | Started POST "/api/v1/auth" for 172.23.0.1 at 2018-10-10 15:21:12 +0000
web_1        | Cannot render console from 172.23.0.1! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
web_1        |    (1.0ms)  SELECT "schema_migrations"."version" FROM "schema_migrations" ORDER BY "schema_migrations"."version" ASC
web_1        |   ↳ /usr/local/bundle/gems/activerecord-5.2.1/lib/active_record/log_subscriber.rb:98
web_1        | Processing by API::V1::AuthenticationController#create as HTML
web_1        |   Parameters: {"email"=>"user1@yandex.ru", "password"=>"[FILTERED]", "authentication"=>{"email"=>"user1@yandex.ru", "password"=>"[FILTERED]"}}
web_1        |   User Load (3.1ms)  SELECT  "users".* FROM "users" WHERE "users"."email" = $1 LIMIT $2  [["email", "user1@yandex.ru"], ["LIMIT", 1]]
web_1        |   ↳ app/controllers/api/v1/authentication_controller.rb:15
web_1        |    (0.3ms)  BEGIN
web_1        |   ↳ app/controllers/api/v1/authentication_controller.rb:19
web_1        |   User Update (1.9ms)  UPDATE "users" SET "current_sign_in_at" = $1, "last_sign_in_at" = $2, "sign_in_count" = $3, "updated_at" = $4 WHERE "users"."id" = $5  [["current_sign_in_at", "2018-10-10 15:21:14.709422"], ["last_sign_in_at", "2018-10-10 14:05:32.458533"], ["sign_in_count", 15], ["updated_at", "2018-10-10 15:21:14.716641"], ["id", 106]]
web_1        |   ↳ app/controllers/api/v1/authentication_controller.rb:19
web_1        |    (1.5ms)  COMMIT
web_1        |   ↳ app/controllers/api/v1/authentication_controller.rb:19
web_1        | [active_model_serializers] Rendered ActiveModel::Serializer::Null with Hash (0.15ms)
web_1        | Completed 200 OK in 615ms (Views: 165.5ms | ActiveRecord: 16.7ms)
web_1        | 
web_1        | 
web_1        | Started GET "/dashboard" for 172.23.0.1 at 2018-10-10 15:21:14 +0000
web_1        | Cannot render console from 172.23.0.1! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
web_1        | Processing by UsersController#show as HTML

使用时记录cache_store(这不会重定向到仪表板,它保持在同一页面上并只是重新呈现登录页面)。

web_1        | Started POST "/api/v1/auth" for 172.23.0.1 at 2018-10-10 15:29:43 +0000
web_1        | Cannot render console from 172.23.0.1! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
web_1        | Processing by API::V1::AuthenticationController#create as HTML
web_1        |   Parameters: {"email"=>"user1@yandex.ru", "password"=>"[FILTERED]", "authentication"=>{"email"=>"user1@yandex.ru", "password"=>"[FILTERED]"}}
web_1        |   User Load (1.6ms)  SELECT  "users".* FROM "users" WHERE "users"."email" = $1 LIMIT $2  [["email", "user1@yandex.ru"], ["LIMIT", 1]]
web_1        |   ↳ app/controllers/api/v1/authentication_controller.rb:15
web_1        |    (0.4ms)  BEGIN
web_1        |   ↳ app/controllers/api/v1/authentication_controller.rb:19
web_1        |   User Update (3.2ms)  UPDATE "users" SET "current_sign_in_at" = $1, "last_sign_in_at" = $2, "sign_in_count" = $3, "updated_at" = $4 WHERE "users"."id" = $5  [["current_sign_in_at", "2018-10-10 15:29:44.123755"], ["last_sign_in_at", "2018-10-10 15:21:14.709422"], ["sign_in_count", 16], ["updated_at", "2018-10-10 15:29:44.132142"], ["id", 106]]
web_1        |   ↳ app/controllers/api/v1/authentication_controller.rb:19
web_1        |    (1.6ms)  COMMIT
web_1        |   ↳ app/controllers/api/v1/authentication_controller.rb:19
web_1        | [active_model_serializers] Rendered ActiveModel::Serializer::Null with Hash (0.17ms)
web_1        | Completed 200 OK in 489ms (Views: 199.9ms | ActiveRecord: 16.1ms)
web_1        | 
web_1        | 
web_1        | Started GET "/dashboard" for 172.23.0.1 at 2018-10-10 15:29:44 +0000
web_1        | Cannot render console from 172.23.0.1! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
web_1        | Processing by UsersController#show as HTML
web_1        | Redirected to http://localhost:3000/login
web_1        | Filter chain halted as :authenticate_user! rendered or redirected
web_1        | Completed 302 Found in 4ms (ActiveRecord: 0.0ms)
web_1        | 
web_1        | 
web_1        | Started GET "/login" for 172.23.0.1 at 2018-10-10 15:29:44 +0000
web_1        | Cannot render console from 172.23.0.1! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
web_1        | Processing by HomeController#login as HTML
web_1        |   Rendering home/login.html.haml within layouts/application
web_1        |   Rendered home/login.html.haml within layouts/application (5.0ms)
web_1        | Completed 200 OK in 760ms (Views: 739.1ms | ActiveRecord: 0.0ms)

通过更新日志,我注意到了不同之处,cache_store日志有这个错误 -Filter chain halted as :authenticate_user! rendered or redirected这一定是问题所在。目前正在研究解决该问题的可能方法。

标签: ruby-on-railscachingredis

解决方案


推荐阅读