gpt4 book ai didi

ios - 证书固定在 swift 3/Alamofire 4 中停止工作?

转载 作者:行者123 更新时间:2023-11-30 12:20:12 24 4
gpt4 key购买 nike

证书固定似乎已在 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

我被告知 SSL 证书固定不应在 swift 3/alamofire 4 中以这种方式实现,这是真的吗?

此外,证书是否有问题?

附注我也尝试过这段代码,但也没有运气:(((

let serverTrustPolicies = [
"*.mydomain.com": serverTrustPolicy
]

最佳答案

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)")
}

关于ios - 证书固定在 swift 3/Alamofire 4 中停止工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44878391/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com