题
我建立一个小程序,它作为一个XMPP客户端和我使用的嫌库。现在,我连接到服务器需要SSL(在洋泾浜我要检查“强制旧(端口5223)SSL”)。我遇到了麻烦拍击连接到该服务器。这可能吗?
解决方案
看看这个线程。
http://www.igniterealtime.org/community/thread/37678
从本质上讲,你需要将以下两行添加到您的代码:
connConfig.setSecurityMode(ConnectionConfiguration.SecurityMode.enabled);
connConfig.setSocketFactory(new DummySSLSocketFactory());
其中connConfig是您的ConnectionConfiguration对象。获得从星火源代码库的DummySSLSocketFactory。它所做的是接受几乎任何证书。这似乎为我工作。祝你好运!
其他提示
是的,这是很容易实现的。看看在 ConnectionConfiguration 一>类,且特别是setSecurityMode它接受ConnectionConfiguration.SecurityMode枚举作为参数方法。将其设置为“必需的”力的Smack使用TLS。
这的Javadoc:
Securirty通过TLS加密是 为了连接需要。如果 服务器不提供TLS或者如果 TLS negotiaton失败,连接 到服务器将失败。
可以通过以下实现:
<强>存储在CA证书密钥存储强>
要存储在密钥库中的证书执行这些步骤。
<强>步骤1:强>下载BouncyCastle的JAR文件。它可以从这里下载:充气城堡JAVA发布
<强>步骤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