asmack XMPP新用户注册

5
尊敬的StackOverflow用户,请帮忙看一下。 我在Ubuntu上搭建了ejabberd服务器,添加了虚拟主机,并设置了 {access, register, [{allow, all}]}. 并注册了一个管理员账户。我不是Linux专家,但我已经完成了这个基本的设置。 现在,我可以用Pidgin从我的Win 7电脑登录管理员账户,并访问许多管理功能。最重要的是我可以在服务器上创建新用户。 问题是,我无法使用asmack库在我的Android客户端上执行此操作。我可以建立连接并登录,但是当我尝试通过帐户管理器或发送IQ数据包注册新用户时,会收到403禁止的响应错误。
AccountManager am = new AccountManager(connection);
Map<String, String> attributes = new HashMap<String, String>();
attributes.put("username", "my_user_name");
attributes.put("password", "my_password");
attributes.put("email", "foo@foo.com");
attributes.put("name", "my_full_name");
am.createAccount("my_user_name", "my_password", attributes);


Registration reg = new Registration();
reg.setType(IQ.Type.SET);
reg.setTo(connection.getServiceName());
//      attributes.put("username", username);
//      attributes.put("password", password);
//      reg.setAttributes(attributes);
reg.addAttribute("username", username);
reg.addAttribute("password", password);
reg.addAttribute("email", email);
reg.addAttribute("name", fullName);
PacketFilter filter = new AndFilter(new PacketIDFilter(
    reg.getPacketID()), new PacketTypeFilter(IQ.class));
PacketCollector collector = connection.createPacketCollector(filter);
connection.sendPacket(reg);

有人遇到了类似的问题或者能告诉我我做错了什么吗?

谢谢

这是我的LogCat日志

10-10 10:00:26.249: DEBUG/StatusBarPolicy(1639): [BRIGHTHY] curNetwork=22003 curHPLMN=22003
10-10 10:00:26.839: INFO/System.out(21277): 10:00:26 AM SENT (1080244736): <iq id="fMJxx-4" to="morena.local" type="get"><query xmlns="jabber:iq:register"></query></iq>
10-10 10:00:26.869: INFO/System.out(21277): 10:00:26 AM RCV  (1080244736): <iq from='morena.local' to='admin@morena.local/Smack' id='fMJxx-4' type='result'><query xmlns='jabber:iq:register'><instructions>Choose a username and password to register with this server</instructions><username>admin</username><password/><registered/></query></iq>
10-10 10:00:26.959: INFO/System.out(21277): 10:00:26 AM SENT (1080244736): <iq id="fMJxx-5" to="morena.local" type="set"><query xmlns="jabber:iq:register"><password>new_user</password><username>new_user@morena.local</username></query></iq>
10-10 10:00:26.969: INFO/System.out(21277): 10:00:26 AM RCV  (1080244736): <iq from='morena.local' to='admin@morena.local/Smack' id='fMJxx-5' type='error'><query xmlns='jabber:iq:register'><password>new_user</password><username>new_user@morena.local</username></query><error code='403' type='auth'><forbidden xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/></error></iq>
10-10 10:00:31.659: WARN/System.err(21277): forbidden(403)
10-10 10:00:31.659: WARN/System.err(21277):     at org.jivesoftware.smack.AccountManager.createAccount(AccountManager.java:246)
10-10 10:00:31.659: WARN/System.err(21277):     at org.jivesoftware.smack.AccountManager.createAccount(AccountManager.java:207)
10-10 10:00:31.659: WARN/System.err(21277):     at org.me.my_project.FriendProfileActivity.connectToServer(FriendProfileActivity.java:698)
10-10 10:00:31.659: WARN/System.err(21277):     at org.me.my_project.FriendProfileActivity.connectLogin(FriendProfileActivity.java:578)
10-10 10:00:31.659: WARN/System.err(21277):     at org.me.my_project.FriendProfileActivity$2.run(FriendProfileActivity.java:433)
10-10 10:00:31.659: INFO/System.out(21277): 10:00:31 AM SENT (1080244736): <iq id="fMJxx-6" to="morena.local" type="get"><query xmlns="jabber:iq:register"></query></iq>
10-10 10:00:31.679: INFO/System.out(21277): 10:00:31 AM RCV  (1080244736): <iq from='morena.local' to='admin@morena.local/Smack' id='fMJxx-6' type='result'><query xmlns='jabber:iq:register'><instructions>Choose a username and password to register with this server</instructions><username>admin</username><password/><registered/></query></iq>
10-10 10:00:31.689: INFO/System.out(21277): 10:00:31 AM SENT (1080244736): <iq id="fMJxx-7" to="morena.local" type="set"><query xmlns="jabber:iq:register"><email>some@some.com</email><password>new_user1</password><username>new_user1@morena.local</username><name>new_user1@MORENA.LOCAL</name></query></iq>
10-10 10:00:31.699: INFO/System.out(21277): 10:00:31 AM RCV  (1080244736): <iq from='morena.local' to='admin@morena.local/Smack' id='fMJxx-7' type='error'><query xmlns='jabber:iq:register'><email>some@some.com</email><password>new_user1</password><username>new_user1@morena.local</username><name>new_user1@MORENA.LOCAL</name></query><error code='403' type='auth'><forbidden xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/></error></iq>
10-10 10:00:31.729: WARN/System.err(21277): forbidden(403)
10-10 10:00:31.729: WARN/System.err(21277):     at org.me.my_project.utilities.CreateChatAccount.createAccount(CreateChatAccount.java:100)
10-10 10:00:31.729: WARN/System.err(21277):     at org.me.my_project.utilities.CreateChatAccount.createAccount(CreateChatAccount.java:143)
10-10 10:00:31.729: WARN/System.err(21277):     at org.me.my_project.FriendProfileActivity.connectToServer(FriendProfileActivity.java:706)
10-10 10:00:31.729: WARN/System.err(21277):     at org.me.my_project.FriendProfileActivity.connectLogin(FriendProfileActivity.java:578)
10-10 10:00:31.729: WARN/System.err(21277):     at org.me.my_project.FriendProfileActivity$2.run(FriendProfileActivity.java:433)

你的Windows电脑上,你确定防火墙没有屏蔽5222端口吗? - Maggie
你解决了这个问题吗? - CoronaPintu
7个回答

7

我找到了!问题出在服务器配置上(但我仍然不明白为什么在这次更改之前我能够通过 Pidgin 注册新用户)。设置

%% In-band registration
{access, register, [{allow, all}]}.

似乎无法与ejabberd的新版本一起使用。您需要添加:
{mod_register, [
      {access_from, register},
       ...
                ] ...

如果您希望允许所有用户注册,请执行以下操作。如果您只想让管理员有此权限,则需要添加新的访问规则。

{access, register_from, [{allow, admin}]}.

并且。
{mod_register, [
      {access_from, register_from},
       ...
                ] ...

5

以下代码可用于注册新账户:

try
{
    connection.connect ();
    Log.i (TAG, "Connect");
    mAccount = new AccountManager (connection);
    if (mAccount.supportsAccountCreation ())
    {
        mAccount.createAccount ("user", "pass");
    }

以下是对于ejabberd的设置:
{access, register, [{allow, all}]}。
这是一个非常安全的设置,因为它可以在没有经过身份验证的帐户的情况下记录(该方法supports.AccountCreation()返回true)。

3

您是否在mod_register中指定了ip_access选项?如果Pidgin和您的ejabberd服务器某种方式都使用相同的IP,则会有一个类似于以下内容的默认规则:

{mod_register, [
    ...
    %%
    %% Only clients in the server machine can register accounts
    %%
    {ip_access, [{allow, "127.0.0.0/8"},
                 {deny, "0.0.0.0/0"}]},
    ...
] ...

这将允许这两个IP注册帐户,但不能使用不同IP的Android客户端。

我一直面临着相关的问题,但不知何故,对于你起作用的{access_from, register_from}解决方案对我来说并没有按预期起作用。您能否限制仅由admin用户创建新帐户的权限?


1
我遇到了同样的问题并找到了解决方法:
在服务器上进行更改
登录ejabbered的管理员界面后,转到Virtual Host-Nodes-Modules-mod_register并添加:
[{welcome_message,  {"Welcome!", "Welcome to this Jabber server."}}, {access_from, register}]

您将无法从您的客户端成功创建账户。


0

尝试在ejabberd.cfg中进行以下更改。

%%{ip_access, [
%% {allow, "127.0.0.0/8"},
%% {deny, "0.0.0.0/0"}]}

伴随着 -

%% In-band registration
{access, register, [{allow, all}]}.

&

{mod_register, [
      {access_from, register},
       ...
                ] 

希望现在它能为您工作。


0
使用Smack库在通过管理员或其他账户登录后注册新用户。
/** * 在Jabber服务器上注册新客户端 */
public void registerUser()
{
    AccountManager manager = connection.getAccountManager();
    try {
        manager.createAccount("Romain Guy","halejag");//username & paswd


    } catch (XMPPException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

这会抛出异常。我已经安装了Openfire和Spark,因此没有ejabbaer/ejabberd.cfg文件。你能帮我解决Openfire的问题吗? - CoronaPintu

0
    Create user everytime
    Very easy and work everytime


    open ejabberd.cfg file using

    sudo nano /etc/ejabberd/ejabberd.cfg

    change 600 option to infinity

    {registration_timeout, 600}.

    {registration_timeout, infinity}.

我已经安装了Openfire和Spark,因此没有ejabbaer / ejabberd.cfg文件。你能帮我解决Openfire的问题吗? - CoronaPintu

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接