我们有一个由另一个开发人员构建的新web应用程序。我的工作是为其他程序实现一个后端api,用于与数据库交换xml数据。在主应用程序实现对用户进行身份验证之前,一切进展顺利。我想我可以调整curl命令来登录,然后提交/请求数据,但是效果不是很好。我在尝试像

curl --cookie-jar ~/Desktop/cjar --data "user[login]=<username>" --data "user[password]=<pwd>" --data "commit=Sign in" localhost:3000/users/sign_in

然后
curl --cookie ~/Desktop/cjar --data "<root_node></root_node>" localhost:3000/my_update_method

我最新的错误消息是
WARNING: Can't verify CSRF token authenticity
Completed 401 Unauthorized in 1ms

我想这比我收到的重定向消息要好,但我还不知道发生了什么。
我发现了一些相关的帖子,让我觉得这部分与designe有关,部分与rails所做的自动认证有关。当我查看网页传递的信息时,我看到了authenticity_token和utf8 params,我不知道如何为curl命令构造它们。
我可以继续尝试,但我对卷曲和身份验证都很陌生,所以我有点像在黑暗中拍摄,希望有人能帮我节省一些时间。我想我的问题是:
能做到吗?
应该这样做吗?如果没有,我还有别的选择吗?
一篇文章提出了一种绕过身份验证的方法,但是可能只有curl命令需要登录/pwd,而绕过任何其他验证,而不会影响主web应用程序?
更新1:
谢谢你的帮助,我很感激。对第一个curl命令(上面列出)的响应现在只是对登录页面的重定向,所以登录似乎不需要。有人对在哪里开始调试有什么建议吗?没有任何东西进入应用程序,因此没有日志可供查看。以下是饼干的内容,如果对任何人都有意义的话:
# Netscape HTTP Cookie File
# http://curl.haxx.se/rfc/cookie_spec.html
# This file was generated by libcurl! Edit at your own risk.

#HttpOnly_localhost FALSE   /   FALSE   0   _glow_session   BAh7B0kiCmZsYXNoBjoGRUZvOiVBY3Rpb25EaXNwxhc2g6OkZsYXNoSGFzaAk6CkB1c2VkbzoIU2V0BjoKQGhhc2h7ADoMQGNsb3NlZEY6DUBmbGFzaGVzewY6C25vdGljZUkiHFNpZ25lZCBpbiBzdWNjZXNzZnVsbHkuBjsAVDoJQG5vdzBJIg9zZXNzaW9uX2lkBjsARkkiJTIwODc1Y2VjM2ViNzlmZDE3ZjA4ZjVmMDAxNWMxMDU4BjsAVA%3D%3D--d90722b6da386630b33f57902447b440f30d0b2a

我已经将skip_before_filter :verify_authenticity_token添加到处理api请求的控制器中,这消除了我以前看到的错误。
更新2:
经过一些故障排除和调试之后,我想我可能已经登录了,但是当我执行第二个curl时,development.log文件显示
Started POST "/upsert_experiment" for 127.0.0.1 at 2013-10-16 12:14:12 -0500
Processing by WebServiceController#upsert_experiment as */*
  Parameters: {"experiment_xml"=>"<experiment></experiment>"}
Completed 401 Unauthorized in 1ms

这让我觉得授权仍然是个问题。
更新3:
我认为问题是skip_before_filter :verify_authenticity_token不起作用。
我有
class ApplicationController < ActionController::Base
  rescue_from DeviseLdapAuthenticatable::LdapException do |exception|
    render :text => exception, :status => 500
  end
  protect_from_forgery
  before_filter :authenticate_user!
end


class WebServiceController < ApplicationController
  skip_before_filter :verify_authenticity_token
  ...
end

通过这个设置,我得到“在1毫秒内未经授权完成401”。如果我在超类中注释掉protect_from_forgery行,它就会工作,所以伪造保护肯定是个问题。我只是不知道怎么解决。

最佳答案

railsCSRF protection阻止您提出此请求。
从rails文档
CSRF protection automatically include a security token, calculated from the current session and the server-side secret, in all forms and Ajax requests generated by Rails. You won't need the secret, if you use CookieStorage as session storage. If the security token doesn't match what was expected, the session will be reset. Note: In Rails versions prior to 3.0.4, this raised an ActionController::InvalidAuthenticityToken error.
如果要跳过它,请将此行添加到控制器中

skip_before_action :verify_authenticity_token, :only => ["your_update_action"]

09-17 13:22
查看更多