我最近更新了 asmack jar。现在我收到这样的错误:
07-18 12:49:29.523:W/XMPPConnection(6817):javax.net.ssl.SSLHandshakeException:java.security.cert.CertPathValidatorException:找不到证书路径的信任 anchor 。
当我尝试连接时。早些时候一切正常(旧版本)。
最佳答案
是的,如果您编写类似下面的代码,以下代码的旧版本 asmack(直到 aSmack-0.8.10)工作正常,没有任何错误,
ConnectionConfiguration connConfig = new ConnectionConfiguration("host_name", 5222 ); connConfig.setSecurityMode(SecurityMode.enabled);
但是对于较新版本的 asmack(aSmack-4.0.4),如果您使用 connConfig.setSecurityMode(SecurityMode.enabled); 则此错误将持续存在;
正如@cOcO 提到的,SSL 配置不正确。为此,您可以使用 MemorizingTrustManager .
它是一个开源库,下载它并在 Eclipse 中作为 android 项目导入,然后作为 libraryProject 添加到你的 android 项目中。
添加此库后,将以下行添加到您的 AndroidManifest.xml
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity android:name="de.duenndns.ssl.MemorizingActivity"
android:theme="@android:style/Theme.Translucent.NoTitleBar" />
</application>
现在在您的 xmpp 服务中添加以下代码
try {
SSLContext sc = SSLContext.getInstance("TLS");
sc.init(null, MemorizingTrustManager.getInstanceList(this.getApplicationContext()), new SecureRandom());
connConfig.setCustomSSLContext(sc);
} catch (NoSuchAlgorithmException e) {
throw new IllegalStateException(e);
} catch (KeyManagementException e) {
throw new IllegalStateException(e);
}
希望以上代码能解决您的问题。
编辑:16_10_2014 有关信任管理器的更多信息,您可以访问此链接 https://github.com/Flowdalic/asmack/wiki/Truststore
关于xmpp - "CertPathValidatorException: Trust anchor for certification path not found."(a)Smack 4.0.0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24819441/