6

我正在构建一个充当 XMPP 客户端的小程序,并且我正在使用Smack库。现在,我要连接的服务器需要 SSL(在 Pidgin 中我必须检查“强制旧(端口 5223)SSL”)。我无法让 Smack 连接到此服务器。是否可以?

4

3 回答 3

8

看看这个线程。

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

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

connConfig.setSecurityMode(ConnectionConfiguration.SecurityMode.enabled);
connConfig.setSocketFactory(new DummySSLSocketFactory());

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

于 2009-09-29T23:31:16.850 回答
4

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

将 CA 证书存储在 Keystore 中

要将证书存储在密钥库中,请执行以下步骤。

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

第 2 步:使用以下命令将证书存储在密钥库中

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>"

第 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>"

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

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

使用密钥库

生成此密钥库后,将其保存在应用程序的原始文件夹中。使用以下代码获取与 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;
}

全部做完..!!只需连接.. 现在您的连接是安全的。

在我的博客smackssl.blogspot.in中都遵循相同的

于 2015-10-15T12:05:48.913 回答
3

是的,这很容易实现。看一下ConnectionConfiguration类,特别是 setSecurityMode 方法,它接受 ConnectionConfiguration.SecurityMode 枚举作为参数。将此设置为“必需”会强制 Smack 使用 TLS。

来自 Javadoc:

连接需要通过 TLS 加密的安全性。如果服务器不提供 TLS 或 TLS 协商失败,则与服务器的连接将失败。

于 2009-08-31T22:02:33.723 回答