[英]Trying to connect to XMPP server using android Smack client library throws CertPathValidatorException
This is my piece of code: 这是我的代码:
XMPPTCPConnectionConfiguration.Builder connectionBuilder =
XMPPTCPConnectionConfiguration.builder();
connectionBuilder
.setHost(MY_HOST)
.setServiceName(MY_SERVICE_NAME)
.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled)
.setDebuggerEnabled(true);
XMPPTCPConnection connection = new XMPPTCPConnection(connectionBuilder.build());
connection.connect();
When running, I get this exception: 运行时,出现以下异常:
W/System.err: org.jivesoftware.smack.SmackException: javax.net.ssl.SSLHandshakeException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.
Does anyone have any fast solution to overcome this issue? 有没有人有解决此问题的快速解决方案? I am not interested in deep understanding of the SSL protocol etc. I also don't care about security on this stage of my project. 我对SSL协议等的深入了解不感兴趣。在项目的此阶段,我也不关心安全性。
I also want to mention that I succeeded connecting to the server using Swift application, so the issue is probably not on the server side. 我还想提到我已成功使用Swift应用程序连接到服务器,因此问题可能不在服务器端。 Thanks in advance. 提前致谢。
// Create a connection to the jabber.org server.
// Create the configuration for this new connection
InetAddress addr = null;
try {
addr = InetAddress.getByName("192.***.**.**");
} catch (UnknownHostException e) {
e.printStackTrace();
}
HostnameVerifier verifier = new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
return false;
}
};
DomainBareJid serviceName = null;
try {
serviceName = JidCreate.domainBareFrom("localhost");
} catch (XmppStringprepException e) {
e.printStackTrace();
}
XMPPTCPConnectionConfiguration config =
XMPPTCPConnectionConfiguration.builder()
.setUsernameAndPassword(USER_CURRENT_USER, "password")
.setPort(5222)
.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled)
.setXmppDomain(serviceName)
.setHostnameVerifier(verifier)
.setHostAddress(addr)
.setDebuggerEnabled(true)
.build();
connection = new XMPPTCPConnection(config);
try {
connection.connect();
} catch (SmackException | IOException | InterruptedException | XMPPException e) {
e.printStackTrace();
}
try this and let me know if it worked for you.. 试试这个,让我知道它是否对您有用。
检查此链接.. XMPPTCPConnectionConfiguration
您必须将尝试访问的服务器的证书添加到JDK信任库,或禁用主机名的验证,如上所示。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.