【发布时间】:2015-10-31 18:40:16
【问题描述】:
在我说之前我在 Stackoverflow 上搜索了谷歌和很多帖子,但没有任何东西能够为我的问题提供解决方案。为此,我正在创建一个新帖子。
我正在创建一个包含两个子域的应用程序:account.psantos.dev 和 app.psantos.dev。
在:config/initializers/session_store.rb我有:
Rails.application.config.session_store :cookie_store, key: '_psantos.co.ao_session', domain: 'psantos.dev'
我有以下结构文件夹:
- 应用程序
-- 控制器
--- 应用程序1
---- Welcome_controller.rb
--- 帐户
---- Welcome_controller.rb
--views
--- 布局
---- app1.html.erb
---- account.html.erb
--- 应用程序1
---- 欢迎
----- index.html.erb
--- 帐户
---- 欢迎
----- index.html.erb
在:app/views/layout/app1.html.erb (http://app1.psantos.dev) 我有以下行:
<li><%= "Logout", account_sign_out_url, method: :delete %></li>
当我单击此链接(将转到:http://account.psantos.dev)时,出现以下错误:ActionController::InvalidAuthenticityToken at /sign_out
ActionController::InvalidAuthenticityToken
在两个布局文件(app1.html.erb 和 account.html.erb)上,我在 标签之前有这一行:
<%= csrf_meta_tags %>
我该如何解决这个问题?
更新:相关日志
Started GET "/" for 127.0.0.1 at 2015-08-08 12:37:03 +0100 Processing
by APP1::WelcomeController#index as HTML Parameters:
{"subdomain"=>"app1"} Rendered app1/welcome/index.html.erb within
layouts/app1 (0.4ms) [1m[35mEntity Load (0.3ms)[0m SELECT
"entities".* FROM "entities" WHERE "entities"."user_token" = $1 LIMIT
1 [["user_token", "xxxxxxxxxxxxxxxxxxxx"]] Completed 200 OK in 43ms
(Views: 42.0ms | ActiveRecord: 0.3ms)
Started DELETE "/sign_out" for 127.0.0.1 at 2015-08-08 12:37:05 +0100
Processing by Account::SessionsController#destroy as HTML
Parameters: {"subdomain"=>"account"} Can't verify CSRF token
authenticity Completed 422 Unprocessable Entity in 1ms (ActiveRecord:
0.0ms)
ActionController::InvalidAuthenticityToken -
ActionController::InvalidAuthenticityToken: actionpack (4.2.3)
lib/action_controller/metal/request_forgery_protection.rb:181:in
`handle_unverified_request' actionpack (4.2.3)
lib/action_controller/metal/request_forgery_protection.rb:209:in
`handle_unverified_request' actionpack (4.2.3)
lib/action_controller/metal/request_forgery_protection.rb:204:in
`verify_authenticity_token' activesupport (4.2.3)
lib/active_support/callbacks.rb:430:in `block in make_lambda'
activesupport (4.2.3) lib/active_support/callbacks.rb:143:in `block in
halting_and_conditional' activesupport (4.2.3)
lib/active_support/callbacks.rb:502:in `block in call' activesupport
(4.2.3) lib/active_support/callbacks.rb:502:in `call'
【问题讨论】:
-
请向我们展示查看代码和相关的日志文件。
-
嗨@Зелёный,查看日志。我更新了帖子。您需要在哪个视图中显示代码?
-
尝试将
app/controllers/application_controller.rb切换到protect_from_forgery with: :null_session并阅读Rails 中的CSRF。 -
@Зелёный 但这不会打开安全漏洞?
-
为了防止所有其他伪造请求,我们引入了我们的站点知道但其他站点不知道的必需安全令牌。我们在请求中包含安全令牌并在服务器上对其进行验证。
标签: ruby-on-rails session ruby-on-rails-4 subdomain