简体   繁体   中英

Connecting to OpenFire server on the Android App through SSL?

Working on a live chat app on Android using XMPP framework / OpenFire and just transferred to a new cloud server but I'm having some problems with the old Android Users Connecting. New users can log in fine and connect to the OpenFire Server.

With the old user accounts it fails the connection the 1st time, but then the 2nd time it connects. Anyone knows what the issue could be?

Can't figure out what the issue is.

hi if your trying to connect xmpp with openfire then just give ssl permission to XMPPTCPConnectionConfiguration with smack library,

 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();
}

and call this when you are connecting with server here is example see

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

for more details visit this example

thanks hope this will help you to solve your problem (Y).

To connect to openfire ( any xmpp server ) from android device using SSL follow this with 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM