Alamofire with a self-signed certificate / ServerTrustPolicy

前端 未结 4 1144
说谎
说谎 2020-12-03 05:13

I want to use Alamofire to communicate with my server over a https connection with a self signed certificate. My environment runs on localhost. I\'ve tried to connect, but t

相关标签:
4条回答
  • 2020-12-03 05:30

    So I know some time has passed, but I had exactly the same problem. And I found a solution with above answers. I had to add 2 things to trustPolicies:

    let defaultManager: Alamofire.Manager = {
        let serverTrustPolicies: [String: ServerTrustPolicy] = [
             // Here host with port (trustPolicy is my var where I pin my certificates)
            "localhost:3443": trustPolicy
             //Here without port
             "localhost": .disableEvaluation
        ]
    
        let configuration = NSURLSessionConfiguration.defaultSessionConfiguration()
        configuration.HTTPAdditionalHeaders = Alamofire.Manager.defaultHTTPHeaders
    
        return Alamofire.Manager(
            configuration: configuration,
            serverTrustPolicyManager: ServerTrustPolicyManager(policies: serverTrustPolicies)
        )
    }()
    

    Also in Info.plist had to add:

    <key>AppTransportSecurity</key>
    <dict>
      <key>AllowsArbitraryLoads</key>
          <true/>
    </dict>
    
    0 讨论(0)
  • 2020-12-03 05:32

    You need to add the port domain when you create your ServerTrustPolicy dictionary.

    let defaultManager: Alamofire.Manager = {
        let serverTrustPolicies: [String: ServerTrustPolicy] = [
            "localhost:3443": .DisableEvaluation
        ]
    
        let configuration = NSURLSessionConfiguration.defaultSessionConfiguration()
        configuration.HTTPAdditionalHeaders = Alamofire.Manager.defaultHTTPHeaders
    
        return Alamofire.Manager(
            configuration: configuration,
            serverTrustPolicyManager: ServerTrustPolicyManager(policies: serverTrustPolicies)
        )
    }()
    
    0 讨论(0)
  • 2020-12-03 05:34

    For swift 4:

    private static var Manager : Alamofire.SessionManager = {
        // Create the server trust policies
        let serverTrustPolicies: [String: ServerTrustPolicy] = [
            "your domain goes here": .disableEvaluation
        ]
        // Create custom manager
        let configuration = URLSessionConfiguration.default
        configuration.httpAdditionalHeaders = Alamofire.SessionManager.defaultHTTPHeaders
        let man = Alamofire.SessionManager(
            configuration: URLSessionConfiguration.default,
            serverTrustPolicyManager: ServerTrustPolicyManager(policies: serverTrustPolicies)
        )
        return man
    }()
    

    Then you call it like this:

    Manager.upload(body.data(using: .utf8)!, to: url, method: .post, headers: headers)
    

    Credits to Cnoon

    0 讨论(0)
  • 2020-12-03 05:45

    My approach for self-signed https. The ServerTrustPolicyManager is an open class, and it's serverTrustPolicy function is open too. So it can be override.

    In my case, the server list will grow in future. If I hard-code the https list, I will need to maintain the list when adding new https server. So, I decide to override the ServerTrustPolicyManager class in order to meet my needs.

    // For Swift 3 and Alamofire 4.0
    open class MyServerTrustPolicyManager: ServerTrustPolicyManager {
    
        // Override this function in order to trust any self-signed https
        open override func serverTrustPolicy(forHost host: String) -> ServerTrustPolicy? {
            return ServerTrustPolicy.disableEvaluation
        }
    }
    

    Then,

        let trustPolicies = MyServerTrustPolicyManager(policies: [:])
        let manager = Alamofire.SessionManager(configuration: sessionConfig, delegate: SessionDelegate(), serverTrustPolicyManager: trustPolicies)
    
    0 讨论(0)
提交回复
热议问题