這個關於AFNetworking的HTTPS安全通訊的問題,不少沒有過第一次經驗的以及甚至有過一次經驗的都會有點不肯定。 其實很簡單:php
openssl req -new -x509 -nodes -days 365 -newkey rsa:1024 -out tv.diveinedu.com.crt -keyout tv.diveinedu.com.key
其中: -days 365是指定證書的有效期時間長度,單位是天,從命令運行的時刻算起; -newkey rsa:1024是指定新生成的證書使用1024位長度的RSA非對稱加密算法; -out 指定輸出的證書文件名 -keyout 指定輸出的私鑰文件名 上面這個命令運行後會要輸入一些設置信息:html
Country Name (2 letter code) [AU]:CN State or Province Name (full name) [Some-State]:Hunan Locality Name (eg, city) :Changsha Organization Name (eg, company) [Internet Widgits Pty Ltd]:tv.diveinedu.com Organizational Unit Name (eg, section) :Market Common Name (e.g. server FQDN or YOUR name) :tv.diveinedu.com Email Address :diveinedu@qq.com
若是對搭建Linux後臺HTTPS服務有興趣,須要把證書和私鑰上傳到服務器或者直接在服務器生成,把此證書配置到後臺服務器中,以Nginx爲例進行以下設置:node
sudo touch /etc/nginx/sites-available/tv.diveinedu.com
sudo ln -sf /etc/nginx/sites-available/tv.diveinedu.com /etc/nginx/sites-enabled/tv.diveinedu.com
sudo vim /etc/nginx/sites-enabled/tv.diveinedu.com
server { listen 80;#HTTP默認端口80 server_name tv.diveinedu.com;#主機名,與HTTP請求頭域的HOST匹配 access_log /var/log/nginx/tv.diveinedu.com.log;#訪問日誌路徑 return 301 https://$server_name$request_uri;#強制把全部http訪問跳轉到https } server { listen 443;#HTTPS默認端口443 ssl on;#打開SSL安全Socket ssl_certificate /etc/nginx/tv.diveinedu.com.crt;#證書文件路徑 ssl_certificate_key /etc/nginx/tv.diveinedu.com.key;#私鑰文件路徑 server_name tv.diveinedu.com;#主機名,與HTTP請求頭域的HOST匹配 access_log /var/log/nginx/tv.diveinedu.com.log;#訪問日誌路徑 location / { root /var/www/tv.diveinedu.com/;#網站文檔根目錄 index index.php index.html;#默認首頁 } }
nginx -t
若是沒有錯誤就會輸出:nginx
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful
sudo service nginx restart
而後就去你的域名服務商後臺把你的域名解析到服務器到IP地址就能夠自由訪問了,只不過會瀏覽器訪問會被自動組織並顯示警告,手動添加到信任便可。git
若是公司有錢想爲用戶提供更好的服務和體驗,最好仍是去知名CA認證機構去註冊申請一個有效的證書爲妙! 否則瀏覽器(Chome)會這樣:算法
![輸入圖片說明](https://static.oschina.net/uploads/img/201511/27134919_OW6L.png "12306的自簽名證書"")vim
openssl x509 -outform der -in tv.diveinedu.com.crt -out tv.diveinedu.com.der
<key>NSAppTransportSecurity</key> <dict> <key>NSExceptionDomains</key> <dict> <key>tv.diveinedu.com</key> <dict> <key>NSExceptionAllowsInsecureHTTPLoads</key> <true/> </dict> </dict> </dict>
9.iOS端證書驗證代碼:瀏覽器
//openssl x509 -outform der -in tv.diveinedu.com.crt -out tv.diveinedu.com.der NSString *certFilePath = [[NSBundle mainBundle] pathForResource:@"tv.diveinedu.com" ofType:@"der"]; NSData *certData = [NSData dataWithContentsOfFile:certFilePath]; NSSet *certSet = [NSSet setWithObject:certData]; AFSecurityPolicy *policy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModePublicKey withPinnedCertificates:certSet]; policy.allowInvalidCertificates = YES; policy.validatesDomainName = YES; AFHTTPSessionManager *manager = [AFHTTPSessionManager manager]; manager.securityPolicy = policy; manager.requestSerializer.cachePolicy = NSURLRequestReloadIgnoringLocalCacheData; [manager GET:@"https://tv.diveinedu.com/channel/" parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { NSLog(@"%@",responseObject); } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { NSLog(@"%@",error); }];