我试图使用PUT web服务对特定的id更新数据。所以我的json请求就像
{
id: 1,
status: 'A'
}我的web服务需要一个CSRF令牌。因此,我首先使用相同的url并将标头参数和值作为
x-csrf-token => 'fetch'GET给了我一个令牌,然后传入PUT的请求头,但是我仍然得到CSRF令牌验证失败。
我正在执行GET和PUT的RAKE任务(当然是使用)。
RubyVersion2.0.0 Rails版本4.0.0
欢迎任何建议。
谢谢阿比舍克
发布于 2014-04-07 03:33:08
从Rails的代码
# Returns true or false if a request is verified. Checks:
#
# * is it a GET or HEAD request? Gets should be safe and idempotent
# * Does the form_authenticity_token match the given token value from the params?
# * Does the X-CSRF-Token header match the form_authenticity_token
def verified_request?
!protect_against_forgery? || request.get? || request.head? ||
form_authenticity_token == params[request_forgery_protection_token] ||
form_authenticity_token == request.headers['X-CSRF-Token']
end
# Sets the token value for the current session.
def form_authenticity_token
session[:_csrf_token] ||= SecureRandom.base64(32)
end在我看来。如果在GET请求中将令牌存储在session[:_csrf_token]中,然后将其传递到params[:authenticity_token]中的另一个请求中(默认情况下),则应该可以。
https://stackoverflow.com/questions/22853376
复制相似问题