smack 登錄openfire

13年的時候用smack-3.3.0 寫過連接openfire的各種例子,登錄,註冊,會話,收發文件,還弄了sock5相關的一堆,然後我就不再做這個事情了。

隨着smack和openfire的版本變更,smack現在的穩定版已經到4.1.6,又給了我一個需求,一個用戶有兩個密碼,要求兩個不一樣的密碼都能登錄,噁心不?


做openfire的同事說可以通過關閉sasl機制,然後可以曲線救國的實現這種需求。


在3.3的時候,是有方法直接關閉的 setSASLAuthenticationEnabled( false);

可看smack 的 change log 應該是4.0之後就都沒有了。

有人說:XMPPTCPConnectionConfiguration.builder() 中 .setSecurityMode(SecurityMode.disabled) 它可以,我想說您不看api啊

Sets the TLS security mode used when making the connection.


於是在放下smack3年後,開始建了個java工程,看看怎麼曲線救國啊


1 首先要有實現了xmlpull 的jar包 ,下的是 kxml2-2.3.0.jar,不然會說少類

 xmlpull

http://www.xmlpull.org/v1/doc/changes.html

kxml

https://sourceforge.net/projects/kxml/

 2  下載jxmpp

jxmpp

https://oss.sonatype.org/content/repositories/releases/org/jxmpp/


 3  XMPPTCPConnectionConfiguration config = XMPPTCPConnectionConfiguration .builder().setServiceName("192.168.8.41")

這個servicename 一定要和openfire中配置的domainname一樣,我一開始就不一樣,報

org.jivesoftware.smack.sasl.SASLErrorException: SASLError using DIGEST-MD5: not-authorized

 ,然後去openfire服務器上看了一下打印

SaslException == DIGEST-MD5: digest response format violation. Mismatched URI: xmpp/192.168.8.41; expecting: xmpp/192.168.88.41


4 需要jar包 smack-java7-4.1.6.jar

不然會報下面的錯誤

org.jivesoftware.smack.SmackException$NoResponseException: No response received within reply timeout. Timeout was 5000ms (~5s). Used filter: No filter used or filter was 'null'.


5  選擇sasl 使用的Mechanism

我使用 SASLAuthentication.registerSASLMechanism(new SASLPlainMechanism()); 設置了不起作用,不明白爲什麼,有空的話跑一邊源代碼看看。

後來用下面的方式勉強可以設定Mechanism了。

SASLAuthentication.blacklistSASLMechanism("CRAM-MD5");
        SASLAuthentication.blacklistSASLMechanism("DIGEST-MD5");
        SASLAuthentication.unBlacklistSASLMechanism("PLAIN"); 


到此,就可以順利上線了。


折騰了2個多小時,最終,我也沒有能取消sasl,還是修改了openfire的源碼,就算實現了需求吧。特別特別的沒精神,真懷念以前啊!


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章