OGeek|极客世界-中国程序员成长平台

标题: iphone - 如何让 Ruby on Rails 与 ASIHTTPRequest 一起使用? [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 15:44
标题: iphone - 如何让 Ruby on Rails 与 ASIHTTPRequest 一起使用?

我正在创建一个带有 Ruby on Rails 后端的 iOS 应用程序。我已经完成了逻辑设置并在 Rails 中工作,并通过在 Web 浏览器中的测试进行了验证。我的 Rails 应用程序无法正确响应我的 iOS 应用程序,说出关于真实性 token 的内容。

我在 application.rb 中使用以下代码设置了一个真实性 token (添加此代码后我重新启动了服务器):

protect_from_forgery :secret => 'some_secret_here'

我使用以下代码通过 ASIHTTPRequest 将真实性 token 从 iOS 传递到 Rails:

ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setPostValue"some_secret_here" forKey"authenticity_token"];
[request setPostValue"some value" forKey"some_parameter"];
[request setDelegate:self];
[request startAsynchronous];

我有什么遗漏或做错了吗?

同样的代码在 allow_forgery_protection 设置为 false 时也能正常工作,所以我认为错误在于我尝试传递真实性 token 的方式。



Best Answer-推荐答案


每个页面/请求的真实性 token 都不同,因此您需要找到一种方法以其他方式通过管道发送它。

也许尝试通过 header 发送它,如下所示:

class ApplicationController < ActionController::Base
    before_filter :send_form_authenticity_token

    private
      def send_form_authenticity_token
        response.headers['X-Authenticity-Token'] = form_authenticity_token
      end
end

在您的第一个(和后续)请求的响应回调中,您需要执行以下操作:

NSString *authenticityToken = [[request responseHeaders] objectForKey"X-Authenticity-Token"];

这意味着您执行的第一个请求是 GET 请求。

最好有一个全局状态变量,但这是基本概念。

顺便说一下,如果您的 rails 应用程序只是一个后端类型的应用程序,您就不需要防伪保护。

防伪保护可以避免 XSS 攻击,而这不会在 iPhone 上发生。

关于iphone - 如何让 Ruby on Rails 与 ASIHTTPRequest 一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4506652/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4