我正在开发一个小程序,作为XMPP客户端,并且我正在使用Smack库。现在,我连接的服务器需要SSL(在Pidgin中,我必须勾选“强制旧版(端口5223)SSL”)。我无法让Smack连接到这个服务器。这是否可能?
请查看这个帖子。
http://www.igniterealtime.org/community/thread/37678
基本上,您需要将以下两行代码添加到您的代码中:
connConfig.setSecurityMode(ConnectionConfiguration.SecurityMode.enabled);
connConfig.setSocketFactory(new DummySSLSocketFactory());
其中connConfig是您的ConnectionConfiguration对象。从Spark源代码库获取DummySSLSocketFactory。它只接受几乎任何证书。这对我来说似乎有效。祝你好运!
将CA证书存储在Keystore中
要将证书存储在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>"
步骤三:验证密钥库文件
这将列出包含在密钥库中的证书。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>"
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上,所有内容都遵循相同的规则。
Context
和 R
类是什么? - Alastair BrayneContext
是应用程序或类级别的上下文,您可以使用 this
传递。而 R
则是一个类,我可以从中获取存储在硬盘原始目录中的密钥库,您也可以通过其他方式直接将该资源传递给它。 - Iqbal S