【问题标题】:Rails Tutorial 7.4.4 - How does post method submit form with authenticity token?Rails Tutorial 7.4.4 - post 方法如何提交带有真实性令牌的表单?
【发布时间】:2015-01-26 15:43:12
【问题描述】:

我在Rails Tutorial 7.4.4,我很好奇以下测试帖子表单中的post_via_redirect 方法以及authenticity_token 参数如何。

以下测试将通过:

class UsersSignupTest < ActionDispatch::IntegrationTest
  test "valid signup information will add user to database" do
    assert_difference 'User.count', 1 do
      post_via_redirect users_path, user: { name: "Filius Flitwick",
                               email: "Filius_Flitwick@Hogworts.ORG",
                               password:                "charmsmaster",
                               password_confirmation:   "charmsmaster" }
    end
  end
end

为了防止 CSRF (Cross Site Request Forgery),我假设表单中没有正确的authenticity_token 参数是不会通过验证的。但是,我无法弄清楚authenticity_token 是从哪里放入parameters 的。

事实上,我不确定rails 中的POST 到底在做什么。 POST会先请求一个网页的URL来获取authenticity_token吗?

【问题讨论】:

    标签: ruby-on-rails forms csrf restful-url


    【解决方案1】:

    默认情况下,CSRF 保护在测试环境中是关闭的。您可以通过在config/environments/test.rb 中添加以下行来激活它:

    config.action_controller.allow_forgery_protection = true
    

    请参阅Configuration Rails Application 上的指南。

    【讨论】:

    • 我认为这正是答案。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2016-09-17
    • 2011-03-04
    • 2013-04-21
    • 2015-07-14
    • 2019-12-08
    • 2010-09-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多