简体   繁体   English

AFNetworking通过HTTPS发布帖子请求

[英]AFNetworking make post requests via HTTPS

In my iphone app, currently I'm using AFHTTPRequestOperationManager to send post requests to my server and get data. 在我的iphone应用程序中,目前我正在使用AFHTTPRequestOperationManager将发布请求发送到我的服务器并获取数据。

But I have noticed that some of my App users have hacked these post request and change the values of the post requests to increase their coins in the app. 但我注意到我的一些应用程序用户已经破解了这些发布请求并更改了帖子请求的值以增加他们在应用中的硬币。

So I want to make these whole post requests encrypted/ https (i don't even know the term). 所以我想让这些整个帖子请求加密/ https(我甚至不知道这个术语)。 I want these post requests to not be editable by the users. 我希望这些帖子请求不被用户编辑。

Is there a way to do this with AFNetworking? AFNetworking有办法做到这一点吗? I need an example of a post request and the PHP example of reading that request. 我需要一个post请求的示例和读取该请求的PHP示例。 I searched google but couldn't find anything easy to understand. 我搜索谷歌但找不到任何容易理解的东西。 below is the template of HTTP request i currently use. 下面是我目前使用的HTTP请求的模板。 Please give a detailed answer, I'm no expert in AFNetworking or PHP. 请详细解答,我不是AFNetworking或PHP的专家。

NSString *url = [NSString stringWithFormat:@"%@getlist-s.php", [Globals getSiteUrl]];
        AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
        manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/plain"];

        NSDictionary *params = @{@"user_id": [Globals loggedInUser].Id,
                                 @"action": @"get_list",
                                 @"offset": [NSString stringWithFormat:@"%d", offset]
                                 };

        [manager POST:url parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {


        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            NSLog(@"Error: %@ %@", error, operation.responseString);

        }];

You can simply reconfigure your webserver to use HTTPS instead of HTTP. 您只需重新配置您的Web服务器即可使用HTTPS而不是HTTP。 This uses port 443 instead of 80 and requires an SSL Certificate. 这使用端口443而不是80并且需要SSL证书。 Luckily, you don't have to pay for these anymore, you can get one free via LetsEncrypt. 幸运的是,你不必支付这些了,你就可以用LetsEncrypt一个免费的。 I'd recommend switching off the HTTP endpoint so users can't continue using an older version. 我建议关闭HTTP端点,以便用户无法继续使用旧版本。

You can then update your code so all endpoints are HTTPS instead of HTTP. 然后,您可以更新代码,以便所有端点都是HTTPS而不是HTTP。

HTTPS encrypts not only the contents of the requests and responses but also anything in the URL after the domain name making man in the middle attacks much more difficult. HTTPS不仅会对请求和响应的内容进行加密,还会对域名制作人员在中间攻击后的URL中的任何内容进行加密。 That said, this isn't a foolproof approach, as a user could still MITM their own device with their own root certificate, but how they'd go about doing that is out of the scope of this answer. 也就是说,这不是一个万无一失的方法,因为用户仍然可以使用他们自己的根证书MITM他们自己的设备,但他们如何去做这个超出了这个答案的范围。

Another layer of security would be to use a HMAC (Hash-Based Message Authentication Code). 另一层安全性是使用HMAC(基于哈希的消息认证码)。

The app would generate one by taking the request parameters and a shared secret and hashing them using a secure hashing algorithm and adding this on as an additional parameter to the request. 应用程序将通过获取请求参数和共享密钥并使用安全散列算法对其进行散列并将其作为附加参数添加到请求来生成一个。 The server can verify that it's correct (and the request is authentic) by taking the supplied parameters, using the same shared secret, hashing them in the exact same way and checking that the supplied HMAC and the one it has just generated are the same. 服务器可以通过获取提供的参数,使用相同的共享密钥,以完全相同的方式对它们进行散列并检查提供的HMAC和它刚刚生成的HMID是否相同来验证它是否正确(并且请求是可信的)。

It's important to ensure that your shared secret stays a secret between the app and your server and is NEVER transmitted between the two. 确保您的共享密钥在应用程序和服务器之间保密,并且绝不在两者之间传输,这一点很重要。 If you want to be clever about it, you could generate a shared secret on the fly based on certain common parameters but that could get complicated, error-prone and affect legitimate users. 如果您想要聪明一点,您可以根据某些常见参数动态生成共享密钥,但这可能会变得复杂,容易出错并影响合法用户。

It's a cat and mouse game, nothing is perfect and if someone really wants to cheat the system then they will, but this should deter most casual attackers. 这是一场猫捉老鼠的游戏,没有什么是完美的,如果有人真的想欺骗系统,那么他们会,但这应该阻止大多数偶然的攻击者。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM