【问题标题】:Connecting to OpenFire server on the Android App through SSL?通过 SSL 连接到 Android App 上的 OpenFire 服务器?
【发布时间】:2017-01-02 02:53:44
【问题描述】:

Android 上使用 XMPP 框架/OpenFire 开发实时聊天应用程序,刚刚转移到新的云服务器,但老 Android 用户遇到了一些问题连接。新用户可以正常登录并连接到 OpenFire 服务器。

使用旧用户帐户第一次连接失败,但第二次连接失败。有谁知道可能是什么问题?

无法弄清楚问题是什么。

【问题讨论】:

  • “第一次连接失败,但第二次连接失败”你能提供第一次连接失败时在客户端遇到的异常吗?

标签: android ssl xmpp openfire xmppframework


【解决方案1】:

您好,如果您尝试使用 openfire 连接 xmpp,那么只需使用 smack 库授予 XMPPTCPConnectionConfiguration 的 ssl 权限,

 private XMPPTCPConnectionConfiguration buildConfiguration() throws XmppStringprepException {
    XMPPTCPConnectionConfiguration.Builder builder =
            XMPPTCPConnectionConfiguration.builder();

    builder.setHost(Common.HOST);
    builder.setPort(PORT);
    builder.setCompressionEnabled(false);
    builder.setDebuggerEnabled(true);
    builder.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled);
    builder.setSendPresence(true);

    if (Build.VERSION.SDK_INT >= 14) {
        builder.setKeystoreType("AndroidCAStore");
        builder.setKeystorePath(null);
    } else {
        builder.setKeystoreType("BKS");
        String str = System.getProperty("javax.net.ssl.trustStore");
        if (str == null) {
            str = System.getProperty("java.home") + File.separator + "etc" + File.separator + "security"
                    + File.separator + "cacerts.bks";
        }
        builder.setKeystorePath(str);
    }
    DomainBareJid serviceName = JidCreate.domainBareFrom(Common.HOST);
    builder.setServiceName(serviceName);


    return builder.build();
}

当你连接服务器时调用这个是例子,见

XMPPTCPConnectionConfiguration config = buildConfiguration();
            SmackConfiguration.DEBUG = true;
            this.connection = new XMPPTCPConnection(config);
            this.connection.connect();

更多详情请访问example

谢谢,希望这能帮助您解决问题 (Y)。

【讨论】:

    【解决方案2】:

    要使用 SSL 从 android 设备连接到 openfire(任何 xmpp 服务器),请使用 Smack 进行操作

    // Set key for SSL connection
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
        config.setKeystoreType("AndroidCAStore");
        config.setKeystorekeyPath(null);
    } else {
        config.setKeystoreType("BKS");
        String keyPath = System.getProperty("javax.net.ssl.trustStore");
        if (keyPath == null)
            keyPath = System.getProperty("java.home") + File.separator + "etc"
                    + File.separator + "security" + File.separator + "certs.bks";
            config.setKeystorekeyPath(keyPath);
        }
    }
    
    // Now set custom SSL to configuration
    try {
        SSLContext ssl = SSLContext.getInstance("TLS");
        ssl.init(null, new TrustManager[]{new TLSUtils.AcceptAllTrustManager()}, null);
        ssl.getServerSessionContext().setSessionTimeout(10 * 1000);
        config.setCustomSSLContext(ssl);
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (KeyManagementException e) {
        e.printStackTrace();
    }
    
    config.setSecurityMode(Connectionconfig.SecurityMode.required);
    // config is type of XMPPTCPConnectionConfiguration
    

    【讨论】:

      猜你喜欢
      • 2023-03-26
      • 2014-08-09
      • 1970-01-01
      • 2015-03-13
      • 1970-01-01
      • 2014-12-02
      • 2014-04-17
      • 2013-10-07
      • 1970-01-01
      相关资源
      最近更新 更多