【问题标题】:How to do post request from a static site to rails app?如何从静态站点向 Rails 应用程序发布请求?
【发布时间】:2019-01-06 00:43:43
【问题描述】:

我首先在一个域 app.example.com 中创建了 rails 应用程序,并在另一个域 www.example.com 中创建了静态站点。然后在 rails 应用程序中添加devise gem 并在静态站点中创建一个登录表单。这是登录表单的代码:

<form action="app.example.com/users/sign_in" method="post">
    <input name="utf8" type="hidden" value="✓">
    Email:<input type="text" name="email" value="admin@gmail.com">
    Password:<input type="password" name="password" value="password">
    <input name="authenticity_token", value="", type="hidden">
    <button>Submit</button>
</form>

从静态站点登录后是否可以重定向到rails应用程序,因为它需要authentity_token并且很难在静态站点中生成authentity_token,并且可能存在csrf的问题。

【问题讨论】:

  • 我认为更好的解决方案是在您的 Rails 应用程序中使用设计登录表单(无布局)的视图,并在您的静态站点中拥有该视图的 iframe。
  • 在制作登录页面的 iframe 后如何重定向到app.example.com? Rails 应用程序也显示在 iframe 中。

标签: ruby-on-rails devise csrf static-site


【解决方案1】:

您可以通过子类化Devise::SessionsController 来禁用此操作的CSRF protection

# app/controllers/my_sessions_controller.rb
class MySessionsController < Devise::SessionsController
  skip_before_action :verify_authenticity_token, only: :create
end

确保更改config/routes.rb 中的devise_for :users 调用:

devise_for :users, controllers: { sessions: 'my_sessions' }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-11-14
    • 2011-08-05
    • 2020-12-31
    • 1970-01-01
    • 2018-02-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多