作者:笑眯眯马甲61 | 来源:互联网 | 2023-05-19 06:22
ImbuildingasmallprogramthatactsasanXMPPclientandIamusingtheSmacklibrary.Now,the
I'm building a small program that acts as an XMPP client and I am using the Smack library. Now, the server I am connecting to requires SSL (in Pidgin I have to check "Force old (port 5223) SSL"). I'm having trouble getting Smack to connect to this server. Is it possible?
我正在构建一个充当XMPP客户端的小程序,我正在使用Smack库。现在,我连接的服务器需要SSL(在Pidgin中我必须检查“强制旧(端口5223)SSL”)。我无法让Smack连接到这台服务器。可能吗?
3 个解决方案
You can achieve this by the following:
您可以通过以下方式实现此目的:
Storing the CA Certificate in Keystore
将CA证书存储在密钥库中
To store the certificate in a Keystore follow these steps.
要将证书存储在密钥库中,请按照下列步骤操作。
Step 1: Download the bouncycastle JAR file. It can be downloaded from the here: Bouncy Castle JAVA Releases
第1步:下载bouncycastle JAR文件。它可以从这里下载:Bouncy Castle JAVA Releases
Step 2: Use the following command to store the certificate in keystore
步骤2:使用以下命令将证书存储在密钥库中
keytool -importcert -v -trustcacerts -file "" -alias "" -keystore "" -provider org.bouncycastle.jce.provider.BouncyCastleProvider -providerpath "" -storetype BKS -storepass ""
Step 3: Verify the keystore file
第3步:验证密钥库文件
keytool -importcert -v -list -keystore "" -provider org.bouncycastle.jce.provider.BouncyCastleProvider -providerpath "" -storetype BKS -storepass ""
This shall list us the certificate included in the keystore.
这将列出密钥库中包含的证书。
We have a keystore which we can use in our code.
我们有一个密钥库,我们可以在代码中使用它。
Using the keystore
使用密钥库
After generating this keystore, save it in the raw folder of your application. The use the below code to get the certificate handshake with the openfire server.
生成此密钥库后,将其保存在应用程序的原始文件夹中。使用以下代码获取与openfire服务器的证书握手。
To create a connection with openfire using XMPP, you may need to get the config. For the same, use the below method:
要使用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, "".toCharArray());
TrustManagerFactory trustManagerFactory = TrustManagerFactory
.getInstance(KeyManagerFactory.getDefaultAlgorithm());
trustManagerFactory.init(trustStore);
SSLContext sslCOntext= SSLContext.getInstance("TLS");
sslContext.init(null, trustManagerFactory.getTrustManagers(),
new SecureRandom());
return sslContext;
}
All done..!! Just connect.. Now your connection is secured.
全部完成..!!只需连接..现在您的连接是安全的。
All follow the same in my blog at smackssl.blogspot.in
所有这些都在我的博客smackssl.blogspot.in中也是如此