协商请求的iOS中的错误 - 用signalr自签名证书错误(self signed certifi

2019-10-28 20:37发布

Error: Optional(["message": Error during negotiation request.])

我发现了错误,而连接signalr服务器,我想有一个与服务器端,因为它们使用自签名证书的问题。 如何我可以通过客户端(SWIFT)确定,如何启用iOS中11自签名证书的工作? 对于签名库。

下面是我的代码:

func test() {

    let persistentConnection = SignalR("http://services.test.com/signalr", connectionType: .persistent)

     let simpleHub1 = Hub("testHub")

    persistentConnection.useWKWebView = false

    persistentConnection.addHub(simpleHub1)

    persistentConnection.received = { data in
        print(data)
    }

    persistentConnection.connected = { [weak self] in

        print("Connected. Connection ID: \(String(describing: self!.hubConnection.connectionID))")
    }

    persistentConnection.starting = { [weak self] in
        print("Starting...")

    }

    persistentConnection.reconnecting = { [weak self] in
        print("Reconnecting...")
    }

    persistentConnection.connected = { [weak self] in
        print("Connected. Connection ID: \(String(describing: self!.hubConnection.connectionID))")
    }

    persistentConnection.reconnected = { [weak self] in
        print("Reconnected. Connection ID: \(String(describing: self!.hubConnection.connectionID))")
    }

    persistentConnection.disconnected = { [weak self] in
        print("Disconnected.")
    }

    persistentConnection.connectionSlow = { print("Connection slow...") }

    persistentConnection.error = { [weak self] error in

            print("Connection timed out. Restarting...")
            persistentConnection.start()
        }
    }
    persistentConnection.start()
}

Answer 1:

iOS不容许有充分理由的自签名的证书。 你应该总是试图让你的服务器的有效证书。 这是很容易获得有效的服务器证书让我们的加密是免费的。

如果你确实需要使用自己的证书,他们签名与自己的证书颁发机构(CA)和CA证书导出到您的设备。 然后,您的设备将接受此CA签名的证书

您可以使用以下OpenSSL的调用与它创建一个证书颁发机构并签署自己的证书:

openssl genrsa -out ca.key.pem 2048
openssl req -x509 -new -days 365 -sha256 -nodes -subj '/C=Country/ST=State/L=Location/CN=CommonName' -key ca.key.pem -out ca.crt.pem
openssl genrsa -out server.key.pem 2048
openssl req -new -sha256 -subj '/C=Country/ST=State/L=Location/CN=CommonName' -key 
server.key.pem -out server.csr.pem
openssl x509 -req -days 365 -in server.csr.pem -CA ca.crt.pem -CAkey ca.key.pem -CAcreateserial -out server.crt.pem -sha256
openssl x509 -in server.crt.pem -text -noout
openssl verify -CAfile ca.crt.pem server.crt.pem


文章来源: self signed certificate error with signalr - Error during negotiation request iOS