版权声明:本文为博主原创文章,未经博主容许不得转载。java
转载请代表出处:http://www.cnblogs.com/cavalier-/p/6940406.htmlandroid
快速导航git
尴尬TOC目录模板无论用了,那就简单来个目录图片。github
在此为后面的smack学习作笔记,以做备忘。
如下是本次采用的Demo环境:apache
xmpp首次登陆,能够经过自定义的Socket去进行链接服务器,若是你服务器不是配置了很是特殊TLS链接,通常能够是用Smack中的XMPPConnection
类创建链接(我推荐经过API中的XMPPConnection
去链接,能省去不少复杂的过程),下面我讲一下XMPPConnection
的链接配置。安全
XMPPConnection
的链接须要经过XMPPTCPConnectionConfiguration.builder()
配置你在Openfire设置的配置,代码以下:服务器
XMPPTCPConnectionConfiguration.Builder builder = XMPPTCPConnectionConfiguration.builder(); builder.setXmppDomain("server domain"); builder.setHostAddress(InetAddress.getByName("you host address")); //default port 5222 builder.setPort(you port); builder.setDebuggerEnabled(true); builder.setCompressionEnabled(true); builder.setSendPresence(false); builder.setUsernameAndPassword("username", "password");
像如上代码中的,XmppDomain是必须配置(这里的XmppDomain
会拼接在首次创建Socket
的IQ中的to和from中),HostAddress是你运行服务器的域名或IP地址,Port是你登陆的端口,在Openfire中默认是5222端口,但若是你的服务器使用的是Nginx作前置机,那么这里你须要填入你的Nginx配置的端口。session
可能看到这里,你会以为很奇怪,为何还有个进阶配置,难道Openfire难道登陆就是一个普通的Socket登陆吗?这样很容易被黑吧,没错,实际上还有进阶配置知足加密链接和自定义的要求,如TLS登陆或SSL登陆或者压缩通信,以下例子:less
SSLContext sslContext = SSLContext.getInstance("TLS"); MemorizingTrustManager mtm = new MemorizingTrustManager(getApplicationContext()); MemorizingKeyManager memorizingKeyManager = new MemorizingKeyManager(getApplicationContext(), "123456"); sslContext.init(new X509KeyManager[]{(X509KeyManager) memorizingKeyManager} , new X509TrustManager[]{mtm}, new java.security.SecureRandom()); builder.setCustomSSLContext(sslContext); builder.setHostnameVerifier(mtm.wrapHostnameVerifier(new org.apache.http.conn.ssl.StrictHostnameVerifier()));
以上的代码须要注意一点是内中的MemorizingTrustManager
,他来自于MemorizingTrustManager,而另一个MemorizingKeyManager
是自定义的类,内中的代码其实就是自定义了一个继承自X509TrustManager
的类,这里不作深究,若有须要能够联系我。
builder.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled);
builder.setSecurityMode
中填入的另外两种方式,如下是这三种方式的注释和描述:/** * Security via TLS encryption is required in order to connect. If the server * does not offer TLS or if the TLS negotiation fails, the connection to the server * will fail. */ required, /** * Security via TLS encryption is used whenever it's available. This is the * default setting. * <p> * <b>Do not use this setting</b> unless you can't use {@link #required}. An attacker could easily perform a * Man-in-the-middle attack and prevent TLS from being used, leaving you with an unencrypted (and * unauthenticated) connection. * </p> */ ifpossible, /** * Security via TLS encryption is disabled and only un-encrypted connections will * be used. If only TLS encryption is available from the server, the connection * will fail. */ disabled
builder.setCompressionEnabled(true);
PLAIN 、ANONYMOUS 、JIVE-SHAREDSECRET
,这里个人服务器开启的是 PLAIN ,因此我使用 PLAINSASLAuthentication.blacklistSASLMechanism(SASLPlainMechanism.NAME);
经过了上面的配置后,我们能够登陆Openfire系统了,至关简单:
AbstractXMPPConnection xmpptcpConnection = new XMPPTCPConnection(builder.build()); if (!xmpptcpConnection.isConnected()) { xmpptcpConnection.connect(); }else{ System.out.println("Already connected"); }
若是以上的配置和服务器的自定义不匹配,在创建链接的这一步会抛出异常,具体的异常能够经过对应方案处理,这里不详谈。
做为程序猿,难道会使用API够了吗?不,咱们继续折腾,下面一块儿来看一下底层的通信报文流程。
SENT <stream:stream xmlns='jabber:client' to='server domain' xmlns:stream='http://etherx.jabber.org/streams' version='1.0' from='username@server domain' xml:lang='en'>
RECV <?xml version='1.0' encoding='UTF-8'?> <stream:stream xmlns:stream="http://etherx.jabber.org/streams" xmlns="jabber:client" from="im" id="c997c3a8" xml:lang="en" version="1.0"> <stream:features> <starttls xmlns="urn:ietf:params:xml:ns:xmpp-tls"> </starttls> <mechanisms xmlns="urn:ietf:params:xml:ns:xmpp-sasl"> <mechanism>PLAIN</mechanism> <mechanism>ANONYMOUS</mechanism> <mechanism>JIVE-SHAREDSECRET</mechanism> </mechanisms> <compression xmlns="http://jabber.org/features/compress"> <method>zlib</method> </compression> <auth xmlns="http://jabber.org/features/iq-auth"/> <register xmlns="http://jabber.org/features/iq-register"/> </stream:features>
ANONYMOUS
认证方式SENT <auth xmlns='urn:ietf:params:xml:ns:xmpp-sasl' mechanism='ANONYMOUS'>=</auth>
RECV <success xmlns="urn:ietf:params:xml:ns:xmpp-sasl"/>
SENT <stream:stream xmlns='jabber:client' to='server domain' xmlns:stream='http://etherx.jabber.org/streams' version='1.0' from='username@server domain' id='c997c3a8' xml:lang='en'>
RECV <?xml version='1.0' encoding='UTF-8'?> <stream:stream xmlns:stream="http://etherx.jabber.org/streams" xmlns="jabber:client" from="im" id="c997c3a8" xml:lang="en" version="1.0"> <stream:features> <compression xmlns="http://jabber.org/features/compress"> <method>zlib</method> </compression> <bind xmlns="urn:ietf:params:xml:ns:xmpp-bind"/> <session xmlns="urn:ietf:params:xml:ns:xmpp-session"/> </stream:features>
SENT <compress xmlns='http://jabber.org/protocol/compress'><method>zlib</method></compress>
RECV <compressed xmlns='http://jabber.org/protocol/compress'/>
SENT <stream:stream xmlns='jabber:client' to='server domain' xmlns:stream='http://etherx.jabber.org/streams' version='1.0' from='username@server domain' id='c997c3a8' xml:lang='en'>
id="c997c3a8"
仍是那个id
RECV <?xml version='1.0' encoding='UTF-8'?> <stream:stream xmlns:stream="http://etherx.jabber.org/streams" xmlns="jabber:client" from="im" id="c997c3a8" xml:lang="en" version="1.0"> <stream:features> <mechanisms xmlns="urn:ietf:params:xml:ns:xmpp-sasl"> <mechanism>PLAIN</mechanism> <mechanism>ANONYMOUS</mechanism> <mechanism>JIVE-SHAREDSECRET</mechanism> </mechanisms> <bind xmlns="urn:ietf:params:xml:ns:xmpp-bind"/> <session xmlns="urn:ietf:params:xml:ns:xmpp-session"/> </stream:features>
*实际上到这里客户端的登陆已经完成了,可是还没算成功,接下来能够开始作绑定Socket的操做了
SENT <iq id='b86j8-4' type='set'> <bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'> </bind> </iq>
*服务器返回绑定了JID = c997c3a8
的客户端
RECV <iq type="result" id="b86j8-4" to="im/c997c3a8"> <bind xmlns="urn:ietf:params:xml:ns:xmpp-bind"> <jid>c997c3a8@im/c997c3a8</jid> </bind> </iq>
SENT <iq id='b86j8-6' type='set'><session xmlns='urn:ietf:params:xml:ns:xmpp-session'/></iq>
RECV <iq type="result" id="b86j8-6" to="c997c3a8@im/c997c3a8"/>
本篇带领你们了解如何利用smack登陆Openfire和XMPP的报文流程,下一个章节将会带你们进入xmpp的里面,看看获取用户信息等具体的操做。
https://download.igniterealtime.org/smack/docs/latest/documentation/gettingstarted.html/