ios - 信号器自签名证书错误 - 协商请求期间出错 iOS

标签 ios swift signalr ssl-certificate ios11

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

我在连接信号服务器时收到错误,我认为服务器端存在问题,因为他们使用了自签名证书。我如何通过客户端修复(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()
}

最佳答案

iOS 出于充分的理由不允许自签名证书。您应该始终尝试为您的服务器获取有效的证书。从 Let's Encrypt 获取有效的服务器证书非常容易。免费。

如果您确实需要使用自己的证书,请使用自己的证书颁发机构 (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

关于ios - 信号器自签名证书错误 - 协商请求期间出错 iOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48484137/

相关文章:

javascript - SignalR 集线器 : JavaScript runtime error: Unable to get property 'multipleFileHub' of undefined or null reference

ios - 在 webRTC iOS 中设置视频大小

ios - 删除推送通知支持一段时间

swift - 使用 Appcelerator Hyperloop 自定义 TableViewCell

ios - 环球银行金融电信协会3 : How to add headers to columns in UITableViewController?

asp.net - 如何从 Hub 外部将 SignalR 连接添加到 Hub 组?

ios - 使用未声明的标识符“CCJumpBy”

objective-c - 读取和写入图像到 SQLite DB 以供 iPhone 使用

ios - 链接器命令失败 : duplicate symbol for architecture i386

unit-testing - 单元测试 SignalR 集线器