如何使用Smack XMPP库创建SSL连接?

sta*_*fan 6 java ssl xmpp

我正在构建一个充当XMPP客户端的小程序,我正在使用Smack库.现在,我连接的服务器需要SSL(在Pidgin中我必须检查"强制旧(端口5223)SSL").我无法让Smack连接到这台服务器.可能吗?

小智 6

看看这个帖子.

http://www.igniterealtime.org/community/thread/37678

基本上,您需要将这两行添加到您的代码中:

connConfig.setSecurityMode(ConnectionConfiguration.SecurityMode.enabled);
connConfig.setSocketFactory(new DummySSLSocketFactory());
Run Code Online (Sandbox Code Playgroud)

其中connConfig是您的ConnectionConfiguration对象.从Spark源代码存储库中获取DummySSLSocketFactory.它所做的只是接受任何证书.这似乎对我有用.祝好运!

  • 此DummySSLSocketFactory允许所有过去的证书,即使该证书已过期或不是由根CA签发。因此,我建议获取CA证书并将其存储在KeyStore中并将其添加到应用程序中并使用该证书的方法。请参阅我的答案以获取更多详细信息。 (2认同)

Iqb*_*rol 5

您可以通过以下方式实现此目的:

将 CA 证书存储在 Keystore 中

要将证书存储在密钥库中,请按照以下步骤操作。

第 1 步:下载 bouncycastle JAR 文件。可以从这里下载:Bouncy Castle JAVA Releases

步骤2:使用以下命令将证书存储在keystore中

keytool -importcert -v -trustcacerts -file "<certificate_file_with_path>" -alias "<some_name_for_certificate>" -keystore "<file_name_for_the_output_keystore>" -provider org.bouncycastle.jce.provider.BouncyCastleProvider -providerpath "<bouncy_castle_jar_file_with_path>" -storetype BKS -storepass "<password_for_the_keystore>"
Run Code Online (Sandbox Code Playgroud)

步骤 3:验证密钥库文件

keytool -importcert -v -list -keystore "<file_name_for_the_keystore_with_path>" -provider org.bouncycastle.jce.provider.BouncyCastleProvider -providerpath "<bouncy_castle_jar_file_with_path>" -storetype BKS -storepass "<password_for_the_keystore>"
Run Code Online (Sandbox Code Playgroud)

这将列出密钥库中包含的证书。

我们有一个可以在代码中使用的密钥库。

使用密钥库

生成此密钥库后,将其保存在应用程序的原始文件夹中。使用下面的代码来获取与 openfire 服务器的证书握手。

要使用 XMPP 创建与 openfire 的连接,您可能需要获取配置。同样,请使用以下方法:

public ConnectionConfiguration getConfigForXMPPCon(Context context) {
        ConnectionConfiguration config = new ConnectionConfiguration(URLConstants.XMPP_HOST, URLConstants.XMPP_PORT);
        config.setSASLAuthenticationEnabled(false);
        config.setSecurityMode(ConnectionConfiguration.SecurityMode.enabled);
        config.setCompressionEnabled(false);
        SSLContext sslContext = null;
        try {
            sslContext = createSSLContext(context);
        } catch (KeyStoreException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (KeyManagementException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (CertificateException e) {
            e.printStackTrace();
        }

        config.setCustomSSLContext(sslContext);
        config.setSocketFactory(sslContext.getSocketFactory());

        return config;
 }

private SSLContext createSSLContext(Context context) throws KeyStoreException,
            NoSuchAlgorithmException, KeyManagementException, IOException, CertificateException {
        KeyStore trustStore;
        InputStream in = null;
        trustStore = KeyStore.getInstance("BKS");

        if (StringConstants.DEV_SERVER_IP.equals(URLConstants.XMPP_HOST) || StringConstants.TEST_SERVER_IP.equals(URLConstants.XMPP_HOST))
            in = context.getResources().openRawResource(R.raw.ssl_keystore_dev_test);
        else if(StringConstants.STAGE_SERVER_IP.equals(URLConstants.XMPP_HOST) || StringConstants.STAGE2_SERVER_IP.equals(URLConstants.XMPP_HOST))
            in = context.getResources().openRawResource(R.raw.ssl_keystore_stage);
        else if(StringConstants.PROD_SERVER_IP.equals(URLConstants.XMPP_HOST) || StringConstants.PROD1_SERVER_IP.equals(URLConstants.XMPP_HOST))
            in = context.getResources().openRawResource(R.raw.ssl_keystore_prod);

        trustStore.load(in, "<keystore_password>".toCharArray());

        TrustManagerFactory trustManagerFactory = TrustManagerFactory
                .getInstance(KeyManagerFactory.getDefaultAlgorithm());
        trustManagerFactory.init(trustStore);
        SSLContext sslContext = SSLContext.getInstance("TLS");
        sslContext.init(null, trustManagerFactory.getTrustManagers(),
                new SecureRandom());
        return sslContext;
}
Run Code Online (Sandbox Code Playgroud)

全做完了..!!只需连接.. 现在您的连接已安全。

所有人都遵循我的博客smackssl.blogspot.in中的相同内容