2011-04-18 87 views
3

嗨,大家好。我剛剛開始在Java中使用XMPP,包括服務器端和客戶端。 在服務器端,我使用的是Apache Vysper 0.7,在客戶端,我使用的是Ignite Smack 3.1.0 我使用的是apache vysper演示頁面中的一個小型XMPP嵌入式服務器,它使用源代碼隨附的TLS證書代碼:如何使用java中的Smack XMPP庫處理TLS證書

XMPPServer server = new XMPPServer("localhost"); 

    StorageProviderRegistry providerRegistry = new MemoryStorageProviderRegistry(); 

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

    Entity user = EntityImpl.parseUnchecked("[email protected]"); 
    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做同樣的事情,但我不知道如何。

我嫌API我使用的是這樣的:

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

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

    connection.login(userName, password); 

所以這裏。我需要做什麼來接受或拒絕無效的證書? 感謝您的幫助。

回答

6

在阿帕奇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

+0

謝謝。你的例子可以幫助我,因爲它不會再傷害我的xmpp客戶端,但是我正在尋找的是類似於pidgin處理這種情況的方法。例如:如果(證書有效){做某事}其他{顯示警告與接受拒絕} – 2011-04-19 22:55:26

+0

任何想法如何使用smack 4 api做到這一點? – 2014-08-25 22:41:56

1

我認爲你是looking

config.setSelfSignedCertificateEnabled(true) 
+0

謝謝您的回答,但現在我收到以下錯誤:無法處理來自xmpp服務器的傳入節。你知道那是什麼意思嗎? – 2011-04-19 08:14:37