帖子上的真实性令牌无效

Imr*_*ran 18 ruby ruby-on-rails devise ruby-on-rails-3

我正在使用devise注册/进入.

路线

get 'profile' => 'profile#get_profile'
post 'profile' => 'profile#create_profile'
Run Code Online (Sandbox Code Playgroud)

profile_controller

def get_profile
    render json: {user: current_user}, status: :ok
end

def create_profile
    render json: {user: current_user}, status: :ok
end
Run Code Online (Sandbox Code Playgroud)

GET: http:// localhost:3000/user/profile返回预期的输出.然而,

POST请求引发错误说:

ActionController::InvalidAuthenticityToken in User::ProfileController#create_profile.

请揭开这种行为的神秘面纱.

Nic*_*Gnd 31

要禁用,CSRF protection您可以ApplicationController像这样编辑:

class ApplicationController < ActionController::Base
  protect_from_forgery with: :null_session

  # ...
end
Run Code Online (Sandbox Code Playgroud)

或禁用CSRF protection特定控制器:

class ProfilesController < ApplicationController
  skip_before_action :verify_authenticity_token

  # ...
end
Run Code Online (Sandbox Code Playgroud)

:null_session策略会清空会话,而不是提出一个非常适合API的异常.因为会话是空的,所以你不能使用引用的current_user方法或服务助手session.

重要提示:

  • protect_from_forgery with: :null_session 必须仅在特定情况下使用,例如,允许API请求(POST/PUT/PATCH/DELETE)而不使用html表单
  • 随着protect_from_forgery with: :null_session你必须限制与授权系统访问您的数据,因为每个人都可以做的请求对您的API端点
  • 不要删除protect_from_forgery with: :exception通过html表单完成的请求,这很危险!(请在此处阅读http://guides.rubyonrails.org/security.html#cross-site-request-forgery-csrf)

要处理标准请求(通过html表单)和API请求,通常必须为同一资源设置两个不同的控制器.例:

路线

Rails.application.routes.draw do
  resources :profiles

  namespace :api do
    namespace :v1 do
      resources :profiles
    end
  end

end
Run Code Online (Sandbox Code Playgroud)

ApplicationController的

# app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
  protect_from_forgery with: :exception
end
Run Code Online (Sandbox Code Playgroud)

ProfilesController

(html请求的标准控制器)

# app/controllers/profiles_controller.rb
class ProfilesController < ApplicationController

  # POST yoursites.com/profiles
  def create
  end
end
Run Code Online (Sandbox Code Playgroud)

API :: V1 :: ProfilesController

(API请求的控制器)

# app/controllers/api/v1/profiles_controller.rb
module Api
  module V1
    class ProfilesController < ApplicationController
      # To allow only json request
      protect_from_forgery with: :null_session, if: Proc.new {|c| c.request.format.json? }

      # POST yoursites.com/api/v1/profiles
      def create
      end
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

参考:http: //api.rubyonrails.org/classes/ActionController/RequestForgeryProtection/ClassMethods.html#method-i-protect_from_forgery


Aus*_*tio 5

获取请求没有真实性令牌。

您必须使用此将请求伪造内容添加到您的表单中

<%= csrf_meta_tag %> 
Run Code Online (Sandbox Code Playgroud)

并通过javascript地址

$('meta[name="csrf-token"]')
Run Code Online (Sandbox Code Playgroud)