2017-05-31 103 views
3

我已經搜索了很多,並且一直未能找到我的問題的答案。爲了讓我們的應用程序更安全,我們被告知使用「證書鎖定」。我們已經使用Alamofire庫來處理所有API調用,因此使用ServerTrustPolicyManager作爲實現證書鎖定的手段似乎很自然。我已經包括在我的應用程序捆綁適當的證書,這裏是我使用爲Alamofire配置我SessionManager代碼:Alamofire ServerTrustPolicy證書鎖定不阻止Charles Proxy Swift 3

let url = "https://www.mycompany.com" 

var manager: SessionManager? { 

    let serverTrustPolicy = ServerTrustPolicy.pinCertificates(
     certificates: ServerTrustPolicy.certificates(), 
     validateCertificateChain: true, 
     validateHost: true 
    ) 

    let serverTrustPolicies: [String: ServerTrustPolicy] = [ 
     url: serverTrustPolicy 
    ] 
    let config = URLSessionConfiguration.default 

    return SessionManager(configuration: config, serverTrustPolicyManager: ServerTrustPolicyManager(policies: serverTrustPolicies)) 

} 

現在,當我想打一個API請求,我有這樣的方法:

func request(routerRequest request: URLRequestConvertible) -> DataRequest { 
    assert(url != "", "A base URL string must be set in order to make request") 
    print("URL: \(url) : \(request)") 
    return (manager ?? SessionManager.default).request(request) 
} 

現在我遇到的問題是,當我使用像Charles Proxy這樣的東西時,這仍然有效......所有的請求仍然在進行。不應該使用證書鎖定防止像Charles代理服務器那樣能夠工作,因爲證書不匹配?

我測試了其他正確使用證書鎖定的應用程序,它們將阻止任何類型的代理(查爾斯或其他)建立連接。如果我嘗試在啓用Charles的情況下運行Uber或Wells Fargo銀行應用程序等應用程序,則每個請求都會被拒絕,並且會看到類似「無法完成請求,ssl證書無效」的錯誤(這是不是逐字的)。

我覺得在配置我的SessionManager後,我缺少一個步驟。我閱讀過的大多數文檔和我遇到的幫助似乎都意味着,在配置管理器以啓用證書鎖定之後,它應該拒絕具有無效證書的任何請求。誰能幫我?我錯過了什麼?

我感謝任何和所有的幫助。提前致謝!

回答

5

我要回答我自己的問題,只是因爲我想在未來可能幫助其他人解決同樣的問題。當我配置上面的serverTrustPolicies時,您創建了一個String : ServerTrustPolicy的字典,我的錯誤在String中位於服務器名稱。

let serverTrustPolicies: [String: ServerTrustPolicy] = [ 
    url: serverTrustPolicy 
] 

url財產是我們使用我們的API,這是https://www.mycompany.com/api的基本URL - 這引起了我的所有問題。一旦我將其調整爲域名www.mycompany.com,鎖定工作就像預期一樣!現在,當我運行Charles Proxy並啓用了鎖定功能時,我拒絕了所有請求,Charles發出了一條錯誤消息,提示「沒有發出請求,可能是SSL證書被拒絕。」

而不必做任何嚴重的字符串處理的,我說這個擴展在將來使用 - 如果你有,你需要提取出該域的多個基本URL的:

extension String { 
    public func getDomain() -> String? { 
     guard let url = URL(string: self) else { return nil } 
     return url.host 
    } 
} 

現在,您可以做這樣的事情:

let serverTrustPolicies: [String: ServerTrustPolicy] = [ 
    url.getDomain() ?? url : serverTrustPolicy 
] 

希望這可以幫助別人在未來! Goodluck

+0

感謝您在此留下評論。我有完全相同的問題! –

+0

很高興幫助! @Forbze – Pierce