Smack 4.1 Android 匿名注册到 Openfire [英] Smack 4.1 Android anonymous registration to Openfire

查看:25
本文介绍了Smack 4.1 Android 匿名注册到 Openfire的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 Android Smack 4.1 客户端匿名连接到 openfire 服务器并使用 AccountManager 注册新用户(用户名 = "wq",密码 = "wq"),然后断开连接并以非匿名注册用户身份登录.

3.1.1 注册服务器

<块引用>

当未注册的实体与一个服务器而不是一个服务.通常,服务器启用带内注册,以便实体可以引导"他们参与Jabber 网络;这种引导发生在未注册的和未经身份验证的实体打开到服务器的 TCP 连接和立即与服务器完成注册用例,然后使用新注册的身份进行身份验证.如前所述,当一个服务器收到一个用于注册信息的 IQ-get,它应该假设请求实体未注册,除非该实体已经认证过了.根据本地服务供应,一个如果未注册,服务器可能会返回一个节错误实体在验证之前尝试注册太多次,或者如果实体在成功后尝试注册第二个身份完成注册用例;服务器也可以返回一个如果未注册的实体也等待,则流错误在验证或尝试完成任务之前很久成功完成注册使用后的身份验证案例.

连接构建器看起来像:

xmpptcpConnection = new XMPPTCPConnection(XMPPTCPConnectionConfiguration.builder().setSecurityMode(ConnectionConfiguration.SecurityMode.disabled).setResource(资源).setHost(本地主机).setServiceName(serviceName).setPort(端口).setDebuggerEnabled(真).setSendPresence(真).建造());

和注册:

 if (password != null && username != null) {AccountManager accountManager = AccountManager.getInstance(xmpptcpConnection);accountManager.sensitiveOperationOverInsecureConnection(true);accountManager.createAccount(用户名,密码);}别的 {smackError("用户名或密码错误");}

但是我收到了错误的请求

05-21 21:34:20.801 19535-19577/com.xmpp D/SMACK: SENT (0): <stream:stream xmlns='jabber:client' to='127.0.0.1' xmlns:stream='http://etherx.jabber.org/streams' version='1.0' xml:lang='en'>05-21 21:34:20.805 19535-19578/com.xmpp D/SMACK: RECV (0): <?xml version='1.0' encoding='UTF-8'?><stream:stream xmlns:流="http://etherx.jabber.org/streams" xmlns="jabber:client" from="127.0.0.1" id="cafb2f97" xml:lang="en" version="1.0"><流:特征><starttls xmlns="urn:ietf:params:xml:ns:xmpp-tls"></starttls><机制 xmlns="urn:ietf:params:xml:ns:xmpp-sasl"><机制>匿名</mechanism></mechanisms><压缩xmlns="http://jabber.org/features/compress"><method>zlib</method></compression>;<auth xmlns="http://jabber.org/features/iq-auth"/><注册 xmlns="http://jabber.org/features/iq-register"/></流:特征>05-21 21:34:20.826 19535-19577/com.xmpp D/SMACK: SENT (0): <auth xmlns='urn:ietf:params:xml:ns:xmpp-sasl'mechanism='ANONYMOUS'>=</auth>05-21 21:34:20.828 19535-19578/com.xmpp D/SMACK:RECV (0):<success xmlns="urn:ietf:params:xml:ns:xmpp-sasl"/>05-21 21:34:20.830 19535-19578/com.xmpp D/SMACK: RECV (0): <?xml version='1.0' encoding='UTF-8'?><stream:stream xmlns:流="http://etherx.jabber.org/streams" xmlns="jabber:client" from="127.0.0.1" id="cafb2f97" xml:lang="en" version="1.0"><流:特征><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"><可选/></session></stream:features>05-21 21:34:20.838 19535-19577/com.xmpp D/SMACK:发送(0):<iq id='h3Bqk-3' type='set'><bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'></bind></iq>05-21 21:34:20.841 19535-19578/com.xmpp D/SMACK:RECV (0):<iq type="result" id="h3Bqk-3" to="127.0.0.1/cafb2f97"><bind xmlns="urn:ietf:params:xml:ns:xmpp-bind"><jid>cafb2f97@127.0.0.1/cafb2f97</jid></bind></iq>05-21 21:34:20.843 19535-19564/com.xmpp D/SMACK:用户登录(0):cafb2f97@127.0.0.1:5222/cafb2f9705-21 21:34:20.843 19535-19564/com.xmpp D/SMACK:XMPPConnection 已认证 (0)05-21 21:34:20.844 19535-19577/com.xmpp D/SMACK: SENT (0): <presence id='h3Bqk-5'><c xmlns='http://jabber.org/protocol/caps' hash='sha-1' node='http://www.igniterealtime.org/projects/smack' ver='NfJ3flI83zSdUDzCEICtbypursw='/></presence>05-21 21:35:20.531 19535-19577/com.xmpp D/SMACK: SENT (0): <iq to='127.0.0.1' id='h3Bqk-6' type='get'><查询 xmlns='jabber:iq:register'></query></iq>05-21 21:35:20.535 19535-19578/com.xmpp D/SMACK: RECV (0): <iq type="result" id="h3Bqk-6" from="127.0.0.1" to="cafb2f97@127.0.0.1/cafb2f97"><query xmlns="jabber:iq:register"><username/><password/><email/><name/><xxmlns="jabber:x:data" type="form"><title>XMPP客户端注册</title><instructions>请提供以下信息</instructions><field var="FORM_TYPE" type="隐藏"><值>jabber:iq:register</value></field><field var="username" type="text-single" label="Username"><required/>;</field><field var="name" type="text-single" label="全名"/><field var="email" type="text-single" label="Email"/><field var="password" type="text-private" label="Password"><required/></field></x></query></iq>05-21 21:35:20.537 19535-19577/com.xmpp D/SMACK: SENT (0): <iq to='127.0.0.1' id='h3Bqk-8' type='set'<查询 xmlns='jabber:iq:register'>wqwq/查询>05-21 21:35:20.540 19535-19578/com.xmpp D/SMACK: RECV (0): <iq type="error" id="h3Bqk-8" from="127.0.0.1" to="cafb2f97@127.0.0.1/cafb2f97"><query xmlns="jabber:iq:register"><username>wq</username><email/><name/><password>wq</password></query><error code="400" type="modify"><bad-request xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"/></错误>05-21 21:35:20.542 19535-19564/com.xmpp W/System.err: org.jivesoftware.smack.XMPPException$XMPPErrorException: XMPPError: 错误请求 - 修改05-21 21:35:20.542 19535-19564/com.xmpp W/System.err:在 org.jivesoftware.smack.PacketCollector.nextResultOrThrow(PacketCollector.java:232)05-21 21:35:20.542 19535-19564/com.xmpp W/System.err:在 org.jivesoftware.smack.PacketCollector.nextResultOrThrow(PacketCollector.java:213)05-21 21:35:20.542 19535-19564/com.xmpp W/System.err:在 org.jivesoftware.smackx.iqregister.AccountManager.createAccount(AccountManager.java:272)05-21 21:35:20.542 19535-19564/com.xmpp W/System.err:在 org.jivesoftware.smackx.iqregister.AccountManager.createAccount(AccountManager.java:244)05-21 21:35:20.543 19535-19564/com.xmpp W/System.err:在 com.xmpp.service.SmackConnection.register(SmackConnection.java:349)05-21 21:35:20.543 19535-19564/com.xmpp W/System.err:在 com.xmpp.service.SmackService$1.run(SmackService.java:119)05-21 21:35:20.543 19535-19564/com.xmpp W/System.err:在 android.os.Handler.handleCallback(Handler.java:739)05-21 21:35:20.543 19535-19564/com.xmpp W/System.err:在 android.os.Handler.dispatchMessage(Handler.java:95)05-21 21:35:20.543 19535-19564/com.xmpp W/System.err: at android.os.Looper.loop(Looper.java:135)05-21 21:35:20.543 19535-19564/com.xmpp W/System.err:在 com.xmpp.service.SmackService$2.run(SmackService.java:157)05-21 21:35:20.543 19535-19564/com.xmpp W/System.err:在 java.lang.Thread.run(Thread.java:818)

如果我连接到非匿名帐户,我不会收到错误,仅在连接构建器中添加:

.setUsernameAndPassword(用户名,密码)

但我不想使用另一个已经注册的帐户来注册新用户到服务器.有什么好的解决方案可以通过这种方式传递吗?

解决方案

你必须分离 connect().login() 链.

 xmpptcpConnection.connect();如果(匿名){AccountManager accountManager = AccountManager.getInstance(xmpptcpConnection);accountManager.sensitiveOperationOverInsecureConnection(true);accountManager.createAccount(用户名,密码)}xmpptcpConnection.login();//使用旧的或新创建的用户名,共享首选项中的密码

I want connect anonymously to openfire server with Android Smack 4.1 client and register the new user (username = "wq", password="wq") with AccountManager, and then disconnect and login as non-anonymous registered user.

3.1.1 Registration with a Server

Special care must be taken when an unregistered entity interacts with a server rather than a service. Normally, a server enables in-band registration so that entities can "bootstrap" their participation in the Jabber network; this bootstrapping happens when an unregistered and unauthenticated entity opens a TCP connection to a server and immediately completes the registration use case with the server, then authenticates using the newly-registered identity. As noted, when a server receives an IQ-get for registration information, it SHOULD assume that the requesting entity is unregistered unless the entity has already authenticated. Depending on local service provisioning, a server MAY return a stanza error if an unregistered entity attempts to register too many times before authenticating or if an entity attempts to register a second identity after successfully completing the registration use case; a server MAY also return a stream error if the unregistered entity waits too long before authenticating or attempts to complete a task other than authentication after successfully completing the registration use case.

connection builder looks like:

xmpptcpConnection = new XMPPTCPConnection(XMPPTCPConnectionConfiguration.builder()
        .setSecurityMode(ConnectionConfiguration.SecurityMode.disabled)
        .setResource(resource)
        .setHost(localhost) 
        .setServiceName(serviceName)
        .setPort(port)
        .setDebuggerEnabled(true)
        .setSendPresence(true)
        .build()
);

And registration:

        if (password != null && username != null) {
            AccountManager accountManager = AccountManager.getInstance(xmpptcpConnection);
            accountManager.sensitiveOperationOverInsecureConnection(true);
            accountManager.createAccount(username, password);
        }
        else {
            smackError("Username or password wrong");
        }

But I got bad-request error

05-21 21:34:20.801  19535-19577/com.xmpp D/SMACK﹕ SENT (0): <stream:stream xmlns='jabber:client' to='127.0.0.1' xmlns:stream='http://etherx.jabber.org/streams' version='1.0' xml:lang='en'>
05-21 21:34:20.805  19535-19578/com.xmpp D/SMACK﹕ RECV (0): <?xml version='1.0' encoding='UTF-8'?><stream:stream xmlns:stream="http://etherx.jabber.org/streams" xmlns="jabber:client" from="127.0.0.1" id="cafb2f97" 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>ANONYMOUS</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>
05-21 21:34:20.826  19535-19577/com.xmpp D/SMACK﹕ SENT (0): <auth xmlns='urn:ietf:params:xml:ns:xmpp-sasl' mechanism='ANONYMOUS'>=</auth>
05-21 21:34:20.828  19535-19578/com.xmpp D/SMACK﹕ RECV (0): <success xmlns="urn:ietf:params:xml:ns:xmpp-sasl"/>
05-21 21:34:20.830  19535-19578/com.xmpp D/SMACK﹕ RECV (0): <?xml version='1.0' encoding='UTF-8'?><stream:stream xmlns:stream="http://etherx.jabber.org/streams" xmlns="jabber:client" from="127.0.0.1" id="cafb2f97" 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"><optional/></session></stream:features>
05-21 21:34:20.838  19535-19577/com.xmpp D/SMACK﹕ SENT (0): <iq id='h3Bqk-3' type='set'><bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'></bind></iq>
05-21 21:34:20.841  19535-19578/com.xmpp D/SMACK﹕ RECV (0): <iq type="result" id="h3Bqk-3" to="127.0.0.1/cafb2f97"><bind xmlns="urn:ietf:params:xml:ns:xmpp-bind"><jid>cafb2f97@127.0.0.1/cafb2f97</jid></bind></iq>
05-21 21:34:20.843  19535-19564/com.xmpp D/SMACK﹕ User logged (0): cafb2f97@127.0.0.1:5222/cafb2f97
05-21 21:34:20.843  19535-19564/com.xmpp D/SMACK﹕ XMPPConnection authenticated (0)
05-21 21:34:20.844  19535-19577/com.xmpp D/SMACK﹕ SENT (0): <presence id='h3Bqk-5'><c xmlns='http://jabber.org/protocol/caps' hash='sha-1' node='http://www.igniterealtime.org/projects/smack' ver='NfJ3flI83zSdUDzCEICtbypursw='/></presence>
05-21 21:35:20.531  19535-19577/com.xmpp D/SMACK﹕ SENT (0): <iq to='127.0.0.1' id='h3Bqk-6' type='get'><query xmlns='jabber:iq:register'></query></iq>
05-21 21:35:20.535  19535-19578/com.xmpp D/SMACK﹕ RECV (0): <iq type="result" id="h3Bqk-6" from="127.0.0.1" to="cafb2f97@127.0.0.1/cafb2f97"><query xmlns="jabber:iq:register"><username/><password/><email/><name/><x xmlns="jabber:x:data" type="form"><title>XMPP Client Registration</title><instructions>Please provide the following information</instructions><field var="FORM_TYPE" type="hidden"><value>jabber:iq:register</value></field><field var="username" type="text-single" label="Username"><required/></field><field var="name" type="text-single" label="Full name"/><field var="email" type="text-single" label="Email"/><field var="password" type="text-private" label="Password"><required/></field></x></query></iq>
05-21 21:35:20.537  19535-19577/com.xmpp D/SMACK﹕ SENT (0): <iq to='127.0.0.1' id='h3Bqk-8' type='set'><query xmlns='jabber:iq:register'><username>wq</username><email></email><name></name><password>wq</password></query></iq>
05-21 21:35:20.540  19535-19578/com.xmpp D/SMACK﹕ RECV (0): <iq type="error" id="h3Bqk-8" from="127.0.0.1" to="cafb2f97@127.0.0.1/cafb2f97"><query xmlns="jabber:iq:register"><username>wq</username><email/><name/><password>wq</password></query><error code="400" type="modify"><bad-request xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"/></error></iq>
05-21 21:35:20.542  19535-19564/com.xmpp W/System.err﹕ org.jivesoftware.smack.XMPPException$XMPPErrorException: XMPPError: bad-request - modify
05-21 21:35:20.542  19535-19564/com.xmpp W/System.err﹕ at org.jivesoftware.smack.PacketCollector.nextResultOrThrow(PacketCollector.java:232)
05-21 21:35:20.542  19535-19564/com.xmpp W/System.err﹕ at org.jivesoftware.smack.PacketCollector.nextResultOrThrow(PacketCollector.java:213)
05-21 21:35:20.542  19535-19564/com.xmpp W/System.err﹕ at org.jivesoftware.smackx.iqregister.AccountManager.createAccount(AccountManager.java:272)
05-21 21:35:20.542  19535-19564/com.xmpp W/System.err﹕ at org.jivesoftware.smackx.iqregister.AccountManager.createAccount(AccountManager.java:244)
05-21 21:35:20.543  19535-19564/com.xmpp W/System.err﹕ at com.xmpp.service.SmackConnection.register(SmackConnection.java:349)
05-21 21:35:20.543  19535-19564/com.xmpp W/System.err﹕ at com.xmpp.service.SmackService$1.run(SmackService.java:119)
05-21 21:35:20.543  19535-19564/com.xmpp W/System.err﹕ at android.os.Handler.handleCallback(Handler.java:739)
05-21 21:35:20.543  19535-19564/com.xmpp W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:95)
05-21 21:35:20.543  19535-19564/com.xmpp W/System.err﹕ at android.os.Looper.loop(Looper.java:135)
05-21 21:35:20.543  19535-19564/com.xmpp W/System.err﹕ at com.xmpp.service.SmackService$2.run(SmackService.java:157)
05-21 21:35:20.543  19535-19564/com.xmpp W/System.err﹕ at java.lang.Thread.run(Thread.java:818)

I am not getting errors if I am connected with non-anonymous account, in connection builder only added :

.setUsernameAndPassword(username, password)

but I don't want to use another already registered account for registering new user to server. Is there any nice solution to pass it this way ?

解决方案

You have to separate connect().login() chain.

        xmpptcpConnection.connect();

        if (anonymous) {
            AccountManager accountManager = AccountManager.getInstance(xmpptcpConnection);
            accountManager.sensitiveOperationOverInsecureConnection(true);
            accountManager.createAccount(username, password)
        }

        xmpptcpConnection.login(); //with old or newly created username, password from shared preferences

这篇关于Smack 4.1 Android 匿名注册到 Openfire的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆