ios 中使用https的知识
先看文章,这篇文章说的是使用AFNetworing进行https时的事项,十分好!http://blog.cnbang.net/tech/2416/
ios中使用https,主要就是使用NSURLCredential,先看些必要的官方文档:
NSURLCredential is an immutable object representing an authentication credential consisting of authentication information specific to the type of credential and the type of persistent storage to use, if any. The URL loading system supports three types of credentials: password-based user credentials, certificate-based user credentials, and certificate-based server credentials (used when verifying the server’s identity). When you create a credential, you can specify that it should be used for a single request, persisted temporarily (until your app quits), or persisted permanently (in the keychain).
虽然credentials 有许多种,但是针对URL来说,只有3种!
因此有3个初始化方法:
+ credentialForTrust:
+ credentialWithUser:password:persistence:
+ credentialWithIdentity:certificates:persistence:
另外,要参看URL Session Programming Guide中的认证部分:
To attempt to authenticate, the application should create an NSURLCredential object with authentication information of the form expected by the server. You can determine the server’s authentication method by calling authenticationMethod on the protection space of the provided authentication challenge. Some authentication methods supported by NSURLCredential are: HTTP basic authentication (NSURLAuthenticationMethodHTTPBasic) requires a user name and password. Prompt the user for the necessary information and create an NSURLCredential object with credentialWithUser:password:persistence:. HTTP digest authentication (NSURLAuthenticationMethodHTTPDigest), like basic authentication, requires a user name and password. (The digest is generated automatically.) Prompt the user for the necessary information and create an NSURLCredential object with credentialWithUser:password:persistence:. Client certificate authentication (NSURLAuthenticationMethodClientCertificate) requires the system identity and all certificates needed to authenticate with the server. Create an NSURLCredential object with credentialWithIdentity:certificates:persistence:. Server trust authentication (NSURLAuthenticationMethodServerTrust) requires a trust provided by the protection space of the authentication challenge. Create an NSURLCredential object with credentialForTrust:.
After you’ve created the NSURLCredential object: For NSURLSession, pass the object to the authentication challenge’s sender using the provided completion handler block.
For NSURLConnection and NSURLDownload, pass the object to the authentication challenge’s sender with useCredential:forAuthenticationChallenge:.
在使用AFNetworking3.0时,如果你访问的是自签名的https地址,那么会要求你把网站的自签名证书加入到工程里,用来验证网站证书。如果你没有这个证书,会报错:
In order to validate a domain name for self signed certificates, you MUST use pinning.
这个pinning,指的是证书锁定,意思就是只有client包含的证书和服务器的证书一致时,才能通过验证。
AFnetworking 3.0 好像默认会去 程序中寻找所有cer文件,并找符合要求的。也有准确指定的方法:
AFSecurityPolicy * securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeCertificate];
securityPolicy.allowInvalidCertificates = YES;
NSString *certificatePath = [[NSBundle mainBundle] pathForResource:@"tomcat" ofType:@"cer"];
NSData *certificateData = [NSData dataWithContentsOfFile:certificatePath];
NSSet *certificateSet = [[NSSet alloc] initWithObjects:certificateData, nil];
[securityPolicy setPinnedCertificates:certificateSet];
manager.securityPolicy = securityPolicy;
这样做的目的就是安全。
系统自带的nsurlsession的证书验证没有这么严格,它把更多的验证任务交给了程序员,如果你自己不去调用其他函数加强验证,系统也不会强制你使用!
ios 中使用https的知识的更多相关文章
- iOS 中对 HTTPS 证书链的验证
这篇文章是我一边学习证书验证一边记录的内容,稍微整理了下,共扯了三部分内容: HTTPS 简要原理: 数字证书的内容.生成及验证: iOS 上对证书链的验证. HTTPS 概要 HTTPS 是运行在 ...
- iOS中XML的相关知识
1.什么是XML “当 XML(扩展标记语言)于 1998 年 2 月被引入软件工业界时,它给整个行业带来了一场风暴.有史以来第一次,这个世界拥有了一种用来结构化文档和数据的通用且适应性强的格式,它不 ...
- iOS中的HTTPS
在WWDC 15,Apple提出的ATS (App Transport Security) ,是 Apple 在推进网络通讯安全的一个重要方式.在 iOS 9 和 OS X 10.11 中,默认情况下 ...
- iOS 中 AFNetworking HTTPS 的使用
由于我们公司由HTTP转HTTPS,出现了一系列问题特此记录下. 一.HTTPS 二.App Transport Security 三.iOS 中用HTTPS 注意的问题 四.使用 AFNetwork ...
- iOS中多线程知识总结(一)
这一段开发中一直在处理iOS多线程的问题,但是感觉知识太散了,所以就把iOS中多线程的知识点总结了一下. 1.基本概念 1)什么是进程?进程的特性是什么? 进程是指在系统中正在运行的一个应用程序. ...
- 【腾讯Bugly干货分享】iOS 中 HTTPS 证书验证浅析
本文来自于腾讯Bugly公众号(weixinBugly),未经作者同意,请勿转载,原文地址:https://mp.weixin.qq.com/s/-fLLTtip509K6pNOTkflPQ 导语 本 ...
- Android 和iOS 中关于View 的一点知识
View的概念和方法十分重要,这里将对Android 和iOS中出现的,关于视图的一些知识点进行总结,预计文章会比较长,要许多时间慢慢补充. 先转载一部分资料,感谢原作者! 原链接为:http://b ...
- Cordoval在iOS中的运用整理
一:关于Cordoval理论知识 1:PhoneGap是手机平台上流行的一款中间件.它构建在各种手机平台所提供的WebView(浏览器内核)组件的基础之上,使用javascript语言对应用开发者提供 ...
- iOS 数据持久化(扩展知识:模糊背景效果和密码保护功能)
本篇随笔除了介绍 iOS 数据持久化知识之外,还贯穿了以下内容: (1)自定义 TableView,结合 block 从 ViewController 中分离出 View,轻 ViewControll ...
随机推荐
- HDU4930 Fighting the Landlords 模拟
Fighting the Landlords Fighting the Landlords Time Limit: 2000/1000 MS (Java/Others) Memory Limit ...
- rose中设置组合
1.在工具栏选择 "association" (关联).2.编辑role B 属性 open specital , role B detail ,multiplic(重数) 为1, ...
- jQuery.lazyload使用及源码分析
前言: 貌似以前自己也写过图片懒加载插件,但是新公司使用的是jQuery.lazyload插件,为了更好的运用,自己还是把源码看了遍,分别记录了如何使用, 插件原理,各个配置属性的完整解释,demo实 ...
- Request 传值 遇到的中文乱码问题
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="xxxx.aspx.cs&quo ...
- WP7/WP8/WP8.1设置存储的区别
最近做的一个软件,由于最初是针对WP7的,现在在只能创建WP8.1的visual studio 2013上转换为了WP8的. 由于要保存字体大小.背景颜色.JSON三个数据,一直使用的是WP7下的存储 ...
- 【bzoj3631】[JLOI2014]松鼠的新家
题目描述 松鼠的新家是一棵树,前几天刚刚装修了新家,新家有n个房间,并且有n-1根树枝连接,每个房间都可以相互到达,且俩个房间之间的路线都是唯一的.天哪,他居然真的住在"树"上.松 ...
- hdu.5211.Mutiple(数学推导 && 在logn的时间内求一个数的所有因子)
Mutiple Accepts: 476 Submissions: 1025 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 6553 ...
- 第2月第1天 命令(Command)模式
http://www.tracefact.net/Design-Pattern/Command.aspx 命令模式把一个请求或者操作封装到一个对象中.命令模式允许系统使用不同的请求把客户端参数化,对请 ...
- ReactiveCocoa源码拆分解析(一)
(整个关于ReactiveCocoa的工程可以在https://github.com/qianhongqiang/QHQReactive下载) ReactiveCocoa的介绍我就不说了,可以自行百度 ...
- formValidator的一些验证实例
原帖地址:http://www.cnblogs.com/talk/archive/2012/01/29/2330887.html $(function () { try { $.formValidat ...