如何在Java中使用Smack XMPP库处理TLS证书

3

大家好。我刚开始在Java中尝试使用XMPP,包括服务器和客户端。 在服务器端,我正在使用Apache Vysper 0.7,在客户端我正在使用Ignite Smack 3.1.0。 我正在使用来自Apache Vysper演示页面的小型XMPP嵌入式服务器,该服务器使用源代码附带的TLS证书:

    XMPPServer server = new XMPPServer("localhost");  

    StorageProviderRegistry providerRegistry = new MemoryStorageProviderRegistry();  

    AccountManagement accountManagement = (AccountManagement) providerRegistry.retrieve(AccountManagement.class);  

    Entity user = EntityImpl.parseUnchecked("user@localhost");  
    accountManagement.addUser(user, "password");

    server.setStorageProviderRegistry(providerRegistry);  

    server.addEndpoint(new TCPEndpoint());  

    server.setTLSCertificateInfo(new File("bogus_mina_tls.cert"), "boguspw");  

    server.start();  
    System.out.println("Vysper server is running...");

问题在于这不是一个正确/有效的证书。如果我使用 pidgin 测试我的服务器,警报窗口会弹出并告诉我证书无效,并显示一个按钮以便我添加一个例外。

我想做的就是用 Smack API 做同样的事情,但我不知道该怎么做。

在我的 Smack API 上,我正在使用类似于以下内容的东西:

    ConnectionConfiguration config = new ConnectionConfiguration("localhost",5222, "localhost");
    config.setSASLAuthenticationEnabled(false);

    connection = new XMPPConnection(config);
    connection.connect();

    connection.login(userName, password);

那么,这是解决无效证书需要做的事情。我需要做什么来接受或拒绝无效证书? 感谢您的帮助。

2个回答

6
在Apache Vysper的集成测试中,我们使用类似以下内容的代码:
ConnectionConfiguration connectionConfiguration = new ConnectionConfiguration("localhost", 5222);
connectionConfiguration.setSecurityMode(ConnectionConfiguration.SecurityMode.required);
connectionConfiguration.setSASLAuthenticationEnabled(true);
connectionConfiguration.setKeystorePath("src/main/resources/bogus_mina_tls.cert");
connectionConfiguration.setTruststorePath("src/main/resources/bogus_mina_tls.cert");
connectionConfiguration.setTruststorePassword("boguspw");

例如,请参见:https://svn.apache.org/repos/asf/mina/vysper/trunk/server/core-inttest/src/test/java/org/apache/vysper/xmpp/modules/extension/xep0199_xmppping/AbstractIntegrationTestCase.java


谢谢。你的例子对我很有帮助,因为它不再使我的XMPP客户端崩溃了,但我正在寻找一种类似于Pidgin处理这种情况的方式。例如:如果(证书有效){做某事} else {显示接受拒绝的警报}。 - Doua Beri
有没有任何想法如何使用Smack 4 API来实现这个? - Jan Galinski

1

我想你正在寻找

config.setSelfSignedCertificateEnabled(true)

谢谢您的回答,但现在我收到以下错误:无法处理来自XMPP服务器的传入数据包。您知道这是什么意思吗? - Doua Beri

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接