【问题标题】:MQTT certificate-based authentication in flutter web and mobileFlutter Web 和移动端基于 MQTT 证书的身份验证
【发布时间】:2020-04-28 19:28:57
【问题描述】:

我正在尝试在 Flutter 项目中使用 MQTT 来测试原型。我正在使用自定义的基于证书的身份验证。

我正在使用mqtt_client 包。 在连接 SecurityContext 之前在网络中给出此错误 不支持的操作:默认 SecurityContext getter

下面是我的示例代码

  try {
      SecurityContext securityContext =
          SecurityContext(withTrustedRoots: false);
      securityContext.setTrustedCertificatesBytes(caCertificate);
      securityContext.useCertificateChainBytes(caCertificate);
      securityContext.setClientAuthoritiesBytes(clientCertificate,
          password: String.fromCharCodes(privateKey));
    } catch (e) {
      print(e);
    }

    _client = MqttClient('m.test.com', _identifier);
    _client.port = 8883;
    _client.keepAlivePeriod = 20;
    _client.onDisconnected = onDisconnected;
    _client.secure = true;
    //_client.securityContext = securityContext;
    _client.onBadCertificate = ((X509Certificate cert) => false);
    _client.logging(on: true);```



【问题讨论】:

    标签: flutter mqtt flutter-web


    【解决方案1】:

    这对我有用,但我不明白(动态 a)在做什么......也许有人可以更详细地解释它?

    void initializeMQTTClient() async {
        _client = MqttServerClient(_host, _identifier);
        _client.port = 8883; //1883
        _client.keepAlivePeriod = 20;
        _client.onDisconnected = onDisconnected;
        _client.secure = true; // false
        _client.logging(on: true);
        _client.securityContext = SecurityContext.defaultContext;
        // _client.useWebSocket = false;
        /// Add the successful connection callback
        _client.onConnected = onConnected;
        _client.onSubscribed = onSubscribed;
        _client.onBadCertificate = (dynamic a) => true;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-10-02
      • 1970-01-01
      • 2018-02-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-23
      • 1970-01-01
      相关资源
      最近更新 更多