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 下载jxmppjxmpp
https://oss.sonatype.org/content/repositories/releases/org/jxmpp/
这个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的源码,就算实现了需求吧。特别特别的没精神,真怀念以前啊!