【问题标题】:Rails InvalidAuthenticityToken with HTTPS + ELBRails InvalidAuthenticityToken 与 HTTPS + ELB
【发布时间】:2017-06-22 23:22:54
【问题描述】:

我正在尝试让 HTTPS 为我的网站工作,但我收到了 ActionController::InvalidAuthenticityToken 用于所有发布请求。我记录了form_authenticity_paramform_authenticity_token,它们实际上是不同的。

在 Elastic Load Balancer 解析 SSL,并将非 SSL 请求发送到 Web 应用程序。预期的 CSRF 令牌存储在基于 cookie 的会话中,因此 HTTP 和 HTTPS 的会话似乎需要不同的令牌。在站点上使用 HTTP 时,post/put 请求工作正常。

我已经被这个问题困扰了一段时间。任何建议都会有所帮助

【问题讨论】:

  • 您解决了这个问题吗?
  • 您的问题是devise/sign_in 还是其他形式?

标签: ruby-on-rails ssl csrf-protection elastic-load-balancer authenticity-token


【解决方案1】:

我也有同样的问题,但不是rails。

我通过在我的 nginx.config 中添加 proxy_set_header X-Forwarded-Proto https; 解决了这个问题

  location @videos {
    proxy_pass http://videos;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto https;
    proxy_set_header Host $http_host;
    proxy_redirect off;
  }

【讨论】:

  • 在 Rails 5.2 升级后,无论出于何种原因,我都遇到了类似的问题。添加以上解决了问题
  • @beaorn 这可能与 Rails 5.1 默认启用选项 action_controller.forgery_protection_origin_check 有关。
猜你喜欢
  • 1970-01-01
  • 2018-11-02
  • 1970-01-01
  • 2014-02-10
  • 2018-11-27
  • 2019-06-12
  • 1970-01-01
  • 2015-11-23
  • 2020-11-13
相关资源
最近更新 更多