Can't verify CSRF token authenticity in rails

南楼画角 提交于 2019-11-27 02:51:57

问题


I am using PaypalAdaptive. It sends ipn_notification properly. ipnNotification action method is as following -

def ipn_notification
    ipn = PaypalAdaptive::IpnNotification.new
    ipn.send_back(request.raw_post.to_json)

    print "=====================request.raw_post#{request.raw_post}=============="

    if ipn.verified?
        PaymentMailer.notify_unknown(request.raw_post).deliver
    else
        logger.info "IT DIDNT WORK"
    end
    render :nothing => true
end

but it's returning error

WARNING: Can't verify CSRF token authenticity rails

Any help for this problem.


回答1:


In your controller:

skip_before_filter :verify_authenticity_token, :only => [:ipn_notification]

For people reading to quickly and distribute -1 (skipping an important part: it's not a POST call from the client...):

  • yes it skips a security BUT... Read after...

  • yes, it's the only way for external website POST requests

  • yes it's safe: you obviously check params and keys when receiving a call from Paypal or alike.




回答2:


The correct solution for this problem without compromising security

In your ajax request send the csrf token value as header.

var csrfToken = $("meta[name='csrf-token']").attr("content");
$.ajaxSetup({
  headers: {
    'X-CSRF-Token': csrfToken
  }
});



回答3:


Add the following line in your application.js

//= require jquery_ujs

And try.



来源:https://stackoverflow.com/questions/11986939/cant-verify-csrf-token-authenticity-in-rails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!