Ember js and Rails 4 CSRF



我已经使用网站上提供的 gem 使用 Ember js 设置了一个 Rails 4 应用程序

宝石文件

gem 'ember-rails'
gem 'ember-source', '1.2.0'

entries_controller.js.咖啡

Tut1.EntriesController = Ember.ArrayController.extend
    addEntry: ->
        entry = @store.createRecord(Tut1.Entry,
        name: @get('newEntryName')
        winner: false
    )
    entry.save()

我在控制台上收到此错误。

POST http://localhost:3000/entries 422 (OK) 

它发布正确,但 rails 正在重新调整"ActionController::InvalidAuthenticityToken",这让我感到困惑,因为主机、来源和引用者是相同的。

Host:localhost:3000
Origin:http://localhost:3000
Referer:http://localhost:3000/

它仍然是跨域的吗?如何对此请求进行身份验证。

有很多

链接到这个问题

http://blog.waymondo.com/2012-12-18-ember-dot-js-and-rails-authentication-gotchas/

$ ->
  token = $('meta[name="csrf-token"]').attr('content')
  $.ajaxPrefilter (options, originalOptions, xhr) ->
    xhr.setRequestHeader('X-CSRF-Token', token)

这不是跨域请求,但是,应用程序控制器中的代码:

protect_from_forgery with: :exception

正在尝试防范 CSRF 攻击。当您发布表单时,它需要一个有效的 CSRF 令牌。这里有更多细节。

解决此问题的一种简单方法是使用rails_csrf。它实质上是从您的服务器请求令牌,然后设置适当的标头,以便使用正确的 CSRF 令牌发出请求。

最新更新