Swift:向URLRequest添加SSL密钥

时间:2021-12-20 01:09:39

I'm trying to make a URLRequest but I need to add SSL key and password to the request but I haven't found any example of how to accomplish this.

我正在尝试创建一个URLRequest,但我需要在请求中添加SSL密钥和密码,但我还没有找到任何如何实现此目的的示例。

This is my requests:

这是我的要求:

func requestFactory(request:URLRequest, completion:@escaping (_ data:Data?)->Void){  
let task = URLSession.shared.dataTask(with: request, completionHandler: { (data, urlRequestResponse, error) in

    if error != nil{
        completion(data)
    }
})
task.resume()

}

I'll really appreciate your help.

我真的很感谢你的帮助。

1 个解决方案

#1


1  

Swift 4 Assuming you have purchased yourself an SSL certificate, Google how to convert your SSL bundle certificate (.crt file) to .der format using OpenSSL in Terminal. Locate the .der file you created in your file system and drag it into your project folder in Xcode. Next, go to your project root and under Build Phases, click on the drop down list 'Copy Bundle Resources' and click the + button to add the .der file to the resource list.

Swift 4假设您已经购买了自己的SSL证书,Google如何使用终端中的OpenSSL将您的SSL捆绑证书(.crt文件)转换为.der格式。找到您在文件系统中创建的.der文件,并将其拖到Xcode中的项目文件夹中。接下来,转到项目根目录,在Build Phases下,单击下拉列表'Copy Bundle Resources'并单击+按钮将.der文件添加到资源列表中。

Next you will need to make a class that implements URLSessionDelegate (in my case I called it URLSessionPinningDelegate) and when you formulate your URLSession call, you will pass in this class as the delegate. You should have a look at how to implement SSL certificate pinning for instructions on how to implement this class. This site here has a perfect and functioning explanation of how to do that.

接下来,您需要创建一个实现URLSessionDelegate的类(在我的例子中,我称之为URLSessionPinningDelegate),当您制定URLSession调用时,您将传递此类作为委托。您应该了解如何实现SSL证书固定,以获取有关如何实现此类的说明。这个网站有一个完美和功能的解释,如何做到这一点。

Below is an example of how to set up the session and task. The password will be passed in the Header of URLRequest when you call request.setValue so check out that documentation too. This should get you started once you've figured out SSL certificate pinning and have set up your backend to authenticate your user's password and also set up trust for your client-side certificate.

以下是如何设置会话和任务的示例。当您调用request.setValue时,密码将在URLRequest的Header中传递,因此也请查看该文档。一旦您确定了SSL证书固定并设置了后端以验证用户密码并为客户端证书设置信任,这应该可以帮助您入门。

if let url = NSURL(string: "https://www.example.com") { // Your SSL server URL
var request = URLRequest(url: url as URL)
let password = "" // Your password value
request.setValue("Authorization", forHTTPHeaderField: password)

let session = URLSession(
configuration: URLSessionConfiguration.ephemeral,
delegate: URLSessionPinningDelegate(),
delegateQueue: nil)

With the added session and request parameters your code would look like this:

使用添加的会话和请求参数,您的代码将如下所示:

let task = session.dataTask(with: request, completionHandler: { (data, response, error) in
            if error != nil {
                print("error: \(error!.localizedDescription): \(error!)")
            } else if data != nil {
                if let str = NSString(data: data!, encoding: String.Encoding.utf8.rawValue) {
                    print("Received data:\n\(str)")
                } else {
                    print("Unable to convert data to text")
                }
            }
        })

        task.resume()

#1


1  

Swift 4 Assuming you have purchased yourself an SSL certificate, Google how to convert your SSL bundle certificate (.crt file) to .der format using OpenSSL in Terminal. Locate the .der file you created in your file system and drag it into your project folder in Xcode. Next, go to your project root and under Build Phases, click on the drop down list 'Copy Bundle Resources' and click the + button to add the .der file to the resource list.

Swift 4假设您已经购买了自己的SSL证书,Google如何使用终端中的OpenSSL将您的SSL捆绑证书(.crt文件)转换为.der格式。找到您在文件系统中创建的.der文件,并将其拖到Xcode中的项目文件夹中。接下来,转到项目根目录,在Build Phases下,单击下拉列表'Copy Bundle Resources'并单击+按钮将.der文件添加到资源列表中。

Next you will need to make a class that implements URLSessionDelegate (in my case I called it URLSessionPinningDelegate) and when you formulate your URLSession call, you will pass in this class as the delegate. You should have a look at how to implement SSL certificate pinning for instructions on how to implement this class. This site here has a perfect and functioning explanation of how to do that.

接下来,您需要创建一个实现URLSessionDelegate的类(在我的例子中,我称之为URLSessionPinningDelegate),当您制定URLSession调用时,您将传递此类作为委托。您应该了解如何实现SSL证书固定,以获取有关如何实现此类的说明。这个网站有一个完美和功能的解释,如何做到这一点。

Below is an example of how to set up the session and task. The password will be passed in the Header of URLRequest when you call request.setValue so check out that documentation too. This should get you started once you've figured out SSL certificate pinning and have set up your backend to authenticate your user's password and also set up trust for your client-side certificate.

以下是如何设置会话和任务的示例。当您调用request.setValue时,密码将在URLRequest的Header中传递,因此也请查看该文档。一旦您确定了SSL证书固定并设置了后端以验证用户密码并为客户端证书设置信任,这应该可以帮助您入门。

if let url = NSURL(string: "https://www.example.com") { // Your SSL server URL
var request = URLRequest(url: url as URL)
let password = "" // Your password value
request.setValue("Authorization", forHTTPHeaderField: password)

let session = URLSession(
configuration: URLSessionConfiguration.ephemeral,
delegate: URLSessionPinningDelegate(),
delegateQueue: nil)

With the added session and request parameters your code would look like this:

使用添加的会话和请求参数,您的代码将如下所示:

let task = session.dataTask(with: request, completionHandler: { (data, response, error) in
            if error != nil {
                print("error: \(error!.localizedDescription): \(error!)")
            } else if data != nil {
                if let str = NSString(data: data!, encoding: String.Encoding.utf8.rawValue) {
                    print("Received data:\n\(str)")
                } else {
                    print("Unable to convert data to text")
                }
            }
        })

        task.resume()