Home Database Mysql Tutorial AFNetworking 2.x 的SSL身份认证

AFNetworking 2.x 的SSL身份认证

Jun 07, 2016 pm 03:00 PM
ssl Jump Authentication

1.如果想跳过ssl验证的话 AFHTTPSessionManager * client = [[AFHTTPSessionManager alloc] initWithBaseURL:[NSURL URLWithString:baseUrl]];[[client securityPolicy] setAllowInvalidCertificates:YES]; 2.加入ssl证书 一般来讲如果app用了web service ,

1.如果想跳过ssl验证的话

AFHTTPSessionManager * client = [[AFHTTPSessionManager alloc] initWithBaseURL:[NSURL URLWithString:baseUrl]];[[client securityPolicy] setAllowInvalidCertificates:YES];
Copy after login

2.加入ssl证书

一般来讲如果app用了web service , 我们需要防止数据嗅探来保证数据安全.通常的做法是用ssl来连接以防止数据抓包和嗅探

其实这么做的话还是不够的.我们还需要防止中间人攻击(不明白的自己去百度)。攻击者通过伪造的ssl证书使app连接到了伪装的假冒的服务器上,这是个严重的问题!

那么如何防止中间人攻击呢?

首先web服务器必须提供一个ssl证书,需要一个 .crt 文件,然后设置app只能连接有效ssl证书的服务器。

在开始写代码前,先要把 .crt 文件转成 .cer 文件,然后在加到xcode 里面

openssl x509 -in 你的证书.crt -out 你的证书.cer -outform der
Copy after login

如果你用的是NSURLConnection,你需要这样检查证书,必须添加一个 NSURLConnectionDelegate  

- (<span>void</span>)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
Copy after login

用 AFNetworking 2.x的话就简单多了,只需要添加一个AFSecurityPolicy 和 setAFHTTPRequestOperationManager

要经常用的话,推荐写一个方法,然后以后用就可以复制黏贴了

AFNetworking 2.x 的SSL身份认证

- (AFSecurityPolicy*<span>)customSecurityPolicy
{
    </span><span>/*</span><span>*** SSL Pinning ***</span><span>*/</span><span>
    NSString </span>*cerPath = [[NSBundle mainBundle] pathForResource:<span>@"你的证书</span><span>"</span> ofType:<span>@"</span><span>cer</span><span>"</span><span>];
    NSData </span>*certData =<span> [NSData dataWithContentsOfFile:cerPath];
    AFSecurityPolicy </span>*securityPolicy =<span> [[AFSecurityPolicy alloc] init];
    [securityPolicy setAllowInvalidCertificates:NO];
    [securityPolicy setPinnedCertificates:@[certData]];
    [securityPolicy setSSLPinningMode:AFSSLPinningModeCertificate];
    </span><span>/*</span><span>*** SSL Pinning ***</span><span>*/</span>

    <span>return</span><span> securityPolicy;
}</span>
Copy after login

AFNetworking 2.x 的SSL身份认证

 

 然后在管理器里这么设置

 

 

AFNetworking 2.x 的SSL身份认证

 AFHTTPRequestOperationManager *manager =<span> [AFHTTPRequestOperationManager manager];

    </span><span>/*</span><span>*** SSL Pinning ***</span><span>*/</span><span>
    [manager setSecurityPolicy:[self customSecurityPolicy]];
    </span><span>/*</span><span>*** SSL Pinning ***</span><span>*/</span><span>
    
    [manager GET:@"网站的url" parameters:</span><span>params</span> success:^(AFHTTPRequestOperation *operation, NSDictionary*<span> responseObject) {
        </span><span>//</span><span>这里是你自己的代码了</span>
    } failure:^(AFHTTPRequestOperation *operation, NSError *<span>error) {
        </span><span>//</span><span>注释同上</span>
 }];
Copy after login
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Can't wait for universal controls? Now share your keyboard and mouse between Mac, PC with Barrier Can't wait for universal controls? Now share your keyboard and mouse between Mac, PC with Barrier Apr 14, 2023 pm 12:04 PM

How to Share Keyboard and Mouse Between Mac/PC Using Barrier You need to make sure that the computers you want to share your mouse and keyboard with are on the same network, and you'll be switching back and forth between different Macs during the initial setup. Get the latest version of Barrier here (DMG for Mac, exe for Windows) – Download it to each computer you want to be able to use your keyboard and mouse Install Barrier from the DMG (or use the exe to Windows) Copy to the /Applications folder on each Mac you plan to use it on and right-click Barr

After Java8 (291), TLS1.1 is disabled and JDBC cannot connect to SqlServer2008 using SSL. How to solve the problem? After Java8 (291), TLS1.1 is disabled and JDBC cannot connect to SqlServer2008 using SSL. How to solve the problem? May 16, 2023 pm 11:55 PM

After Java8-291, TLS1.1 is disabled, so that JDBC cannot connect to SqlServer2008 using SSL. What should I do? The following is the solution to modify the java.security file 1. Find the java.security file of jre. If it is jre, go to {JAVA_HOME}/jre/ In lib/security, for example????C:\ProgramFiles\Java\jre1.8.0_301\lib\security. If it is the Eclipse green installation-free portable version, search for java.security in the installation folder, such as????xxx\plugins \org

How to implement SSL passthrough in HAProxy How to implement SSL passthrough in HAProxy Mar 20, 2024 am 09:30 AM

Keeping web servers load balanced is one of the key measures to prevent downtime. Using a load balancer is a reliable approach, with HAProxy being a highly regarded choice. Using HAProxy, you can accurately configure the load balancing method and support SSL passthrough to ensure the security of communication between the client and the server. It starts by exploring the importance of implementing SSL passthrough in HAProxy, followed by a detailed discussion of the steps required to implement this feature and an example for better understanding. What is SSL passthrough? Why is it important? As a load balancer, HAProxy accepts and distributes the load flowing to your web servers across configured servers. Load distribution is targeted to client devices and

MySQL: Introduction to SSL connection and setup steps MySQL: Introduction to SSL connection and setup steps Sep 08, 2023 pm 03:51 PM

MySQL: Introduction to SSL connection and summary of setup steps: MySQL provides SSL (SecureSocketsLayer) connection to encrypt the data transmitted between the client and the server. This article will introduce the concept and role of SSL connections, and provide steps and related code examples to set up SSL connections in MySQL. Introduction: As networks and data transmission continue to expand, data security becomes more and more important. By using an SSL connection we can add

Nginx with SSL: Configure HTTPS to protect your web server Nginx with SSL: Configure HTTPS to protect your web server Jun 09, 2023 pm 09:24 PM

Nginx is a high-performance web server software and a powerful reverse proxy server and load balancer. With the rapid development of the Internet, more and more websites are beginning to use the SSL protocol to protect sensitive user data, and Nginx also provides powerful SSL support, making the security performance of the web server even further. This article will introduce how to configure Nginx to support the SSL protocol and protect the security performance of the web server. What is SSL protocol? SSL (SecureSocket

Sharepoint install SSL certificate? Sharepoint install SSL certificate? Feb 19, 2024 am 11:27 AM

Installing an SSL certificate on SharePoint is a critical step in securing your website and providing an encrypted connection. By following the correct installation steps, you can ensure the security of your website data, improve your ranking in search engines, and provide a better user experience for your visitors. Get an SSL Certificate Contact a trusted Certificate Authority (CA) to purchase an SSL certificate. Provide the required authentication and domain ownership verification information. After completing the verification process, you will receive the SSL certificate file. Prepare the Certificate File Open your SSL certificate file using a text editor. Copy the certificate contents to a new text file. Save the file as yourdomain.cer, making sure to change "yourdomain&#8221

The use of Nginx Proxy Manager and SSL certificate: ensuring website security The use of Nginx Proxy Manager and SSL certificate: ensuring website security Sep 26, 2023 am 11:22 AM

Use of NginxProxyManager and SSL certificates: To ensure website security, specific code examples are required. Summary: This article aims to introduce the use of NginxProxyManager and SSL certificates to ensure website security. This article will introduce the basic concepts and functions of NginxProxyManager, and use specific code examples to show how to configure an SSL certificate to ensure the security of the website. Introduction In the modern Internet environment, website security is of paramount importance. and

Fix: ERR_CERT_WEAK_SIGNATURE_ALGORITHM error in Chrome on Windows PC Fix: ERR_CERT_WEAK_SIGNATURE_ALGORITHM error in Chrome on Windows PC Apr 18, 2023 am 09:41 AM

Many Windows users have recently started complaining about an issue where they receive error messages while browsing chrome browser found unsecured web pages and throws error message YourconnectionisnotprivatewithaerrorcodeNET::ERR_CERT_WEAK_SIGNATURE_ALGORITHMonWindows11 system. Now Windows users are not sure what causes this issue and how they can fix it to browse the web with ease. Some of the reasons that may cause this error message are mentioned below. SSL certificate cache issue corrupted browsing data

See all articles