如何将请求从静态站点发布到Rails应用程序?

时间:2018-07-29 16:57:16

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

我首先在一个域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应用程序,因为它需要authenticity_token且很难在静态站点中生成authenticity_token,而且可能存在csrf的问题。

1 个答案:

答案 0 :(得分:0)

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

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

请确保您更改了devise_for :users中的config/routes.rb呼叫:

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