2016-02-27 109 views
0

我試圖使用自簽名驗證調用WebAPI。我有殘疾ATS據我可以通過作用於下我的info.plist:如何在swift中調用具有自簽名ceritifcate的WebAPI

<key>NSAppTransportSecurity</key> 
<dict> 
    <key>NSAllowsArbitraryLoads</key> 
    <true/> 
</dict> 

然而,即使添加此代碼後,我仍然收到了同樣的錯誤。代碼和錯誤如下。任何援助將不勝感激。

let postEndpoint: String = "https://api.rimorton.com/com.absolute.am.webapi/api/login" 
    let url = NSURL(string: postEndpoint) 
    let urlRequest = NSURLRequest(URL: url!) 
    let config = NSURLSessionConfiguration.defaultSessionConfiguration() 
    let session = NSURLSession(configuration: config) 

    let task = session.dataTaskWithRequest(urlRequest, completionHandler: { 
     (data, response, error) in 

     guard error == nil else { 
      print("error calling GET on /posts/1") 
      print(error) 
      return 
     } 

     guard let responseData = data else { 
      print("Error: did not receive data") 
      return 
     } 

     // parse the result as JSON, since that's what the API provides 
     let post: NSDictionary 
     do { 
      post = try NSJSONSerialization.JSONObjectWithData(responseData, 
       options: []) as! NSDictionary 
     } catch { 
      print("error trying to convert data to JSON") 
      return 
     } 
     // now we have the post, let's just print it to prove we can access it 
     print("The post is: " + post.description) 

     // the post object is a dictionary 
     // so we just access the title using the "title" key 
     // so check for a title and print it if we have one 
     if let postTitle = post["title"] as? String { 
      print("The title is: " + postTitle) 
     } 
    }) 
    task.resume() 

錯誤: 2016年2月28日19:40:03.521 LANrevTarget_Dev [19173:878737] NSURLSession/NSURLConnection的HTTP加載失敗(kCFStreamErrorDomainSSL,-9813) 錯誤調用GET上/帖/ 1 可選(錯誤域= NSURLErrorDomain代碼= -1202「此服務器的證書無效,您可能正在連接假裝爲」api.rimorton.com「的服務器,這可能會將您的機密信息置於危險之中。」UserInfo = {NSURLErrorFailingURLPeerTrustErrorKey =,NSLocalizedRecoverySuggestion =你想連接服務器嗎?,_kCFStreamErrorDomainKey = 3,_kCFStreamErrorCodeKey = -9813,NSErrorPeerCertificateChainKey = {type = immutable,count = 1,values =( 0: )},NSUnderlyingError = 0x7fb78b542e00 {錯誤域= kCFErrorDomainCFNetwork代碼= -1202 「(空)」 的UserInfo = {_ kCFStreamPropertySSLClientCertificateState = 0,kCFStreamPropertySSLPeerTrust =,_kCFNetworkCFStreamSSLErrorOriginalValue = -9813,_kCFStreamErrorDomainKey = 3,_kCFStreamErrorCodeKey = -9813,kCFStreamPropertySSLPeerCertificates = {type = immutable,count = 1,values =( 0: )}}},NSLocalizedDescription =此服務器的證書無效。您可能正在連接到一個僞裝成「api.rimorton.com」,它可以把你的機密信息的安全服務器,NSErrorFailingURLKey = https://api.rimorton.com/com.absolute.am.webapi/api/login,NSErrorFailingURLStringKey = https://api.rimorton.com/com.absolute.am.webapi/api/login,NSErrorClientCertificateStateKey = 0})

回答

0

允許任意負載沒有按不禁用證書驗證。它只允許非加密的HTTP請求,以及證書的類型不夠安全(例如,缺乏前向保密)的請求。

要允許特定的自簽名密鑰,請閱讀本:

https://developer.apple.com/library/mac/documentation/NetworkingInternet/Conceptual/NetworkingTopics/Articles/OverridingSSLChainValidationCorrectly.html

的例子都是Objective-C的,但他們應該給你的基本理念。具體來說,你需要這個addAnchorToTrust函數。

然後,看看在connection:willSendRequestForAuthenticationChallenge:challenge該網頁上,並且基本上做同樣的事情在你的URLSession:didReceiveChallenge:completionHandler:方法,除了隨處可見它:

[challenge.sender useCredential:credential forAuthenticationChallenge:challenge]; 

你,而不是撥打:

completionHandler(NSURLSessionAuthChallengeUseCredential, credential); 

無論在哪裏,叫

[connection cancel] 

你而不是打電話

completionHandler(NSURLSessionAuthChallengeCancelAuthenticationChallenge, nil); 

希望有所幫助。此外,你需要可信證書的副本存儲在您的應用程序包和閱讀到您的應用程序如下所述:

How can I get SecKeyRef from DER/PEM file

相關問題