证书固定似乎已停止在Alamofire 4和Swift 3中工作
这是我的代码
let pathToCert = Bundle.main.path(forResource: "certificate", ofType: "der")
let localCertificate = NSData(contentsOfFile: pathToCert!)!
let serverTrustPolicy = ServerTrustPolicy.pinCertificates(
certificates: [SecCertificateCreateWithData(nil, localCertificate)!],
validateCertificateChain: true,
validateHost: true
)
let myServer = "...". //string in format without https://
let serverTrustPolicies = [
myServer: serverTrustPolicy
]
afManager = SessionManager(
serverTrustPolicyManager: ServerTrustPolicyManager(policies: serverTrustPolicies)
)
afManager.request("https://www.google.co.uk", method: .get).response { response in
//I get status code 200 here, which should NOT happen
log.info(response)
}
证书正确加载,这是证书
certificate printed inside console
我的问题是,我似乎从我的域和任何其他域收到状态码 200 。
我不应该从其他域
接收 200有人告诉我,在swift 3 / alamofire 4中不应该以这种方式实现SSL证书固定,这可能是真的吗?
此外,证书可能有问题吗?
P.S。我也试过这个代码,但也没有运气:((
let serverTrustPolicies = [
"*.mydomain.com": serverTrustPolicy
]
答案 0 :(得分:0)
let hostname = "YOUR_HOST_NAME"
let endpoint = "YOUR_ENDPOINT"
let cert = "YOUR_CERT" // e.g. for cert.der, this should just be "cert"
// Set up certificates
let pathToCert = Bundle.main.path(forResource: cert, ofType: "der")
let localCertificate = NSData(contentsOfFile: pathToCert!)
let certificates = [SecCertificateCreateWithData(nil, localCertificate!)!]
// Configure the trust policy manager
let serverTrustPolicy = ServerTrustPolicy.pinCertificates(
certificates: certificates,
validateCertificateChain: true,
validateHost: true
)
let serverTrustPolicies = [hostname: serverTrustPolicy]
let serverTrustPolicyManager = ServerTrustPolicyManager(policies: serverTrustPolicies)
// Configure session manager with trust policy
afManager = SessionManager(
configuration: URLSessionConfiguration.default,
serverTrustPolicyManager: serverTrustPolicyManager
)
afManager.request(endpoint, method: .get).responseJSON { response in
debugPrint("All Response Info: \(response)")
}