Creating new user with Smack 4.2.0-beta1 on ejabbe

2019-07-10 04:36发布

问题:

I am doing Chat application with smack . I am new for this technology.

I have created some manual users from Ejabberd server. Using those users I have done one to one chat and group chat.

But I tried to create new user from android code But I am getting this below error

 XMPPError: forbidden - auth
    08-02 08:23:36.273 31097-31097/com.agarangroup.hello W/System.err:     at org.jivesoftware.smack.PacketCollector.nextResultOrThrow(PacketCollector.java:232)
    08-02 08:23:36.273 31097-31097/com.agarangroup.hello W/System.err:     at org.jivesoftware.smack.PacketCollector.nextResultOrThrow(PacketCollector.java:207)

This is the configuration I have in ejabberd server

announce    [{allow,[{acl,admin}]}]
c2s [{deny,[{acl,blocked}]},{allow,[{acl,all}]}]
c2s_shaper  [{none,[{acl,admin}]},{normal,[all]}]
configure   [{allow,[{acl,admin}]}]
local   [{allow,[{acl,all}]}]
max_user_offline_messages   [{5000,[{acl,admin}]},{100,[all]}]
max_user_sessions   [{10,[all]}]
muc_create  [{allow,[{acl,all}]}]
pubsub_createnode   [{allow,[{acl,local}]}]
register    [{allow,[{acl,all}]}]
s2s_shaper  [{fast,[all]}]
trusted_network [{allow,[{acl,loopback}]}]

Updated :

Here I am initialising my connection

 private void initialiseConnection() {
        DomainBareJid serviceName = null;
        try {
            serviceName = JidCreate.domainBareFrom(ServiceAddress);
        } catch (XmppStringprepException e) {
            e.printStackTrace();
        }
        XMPPTCPConnectionConfiguration.Builder config = XMPPTCPConnectionConfiguration
                .builder().setKeystoreType(null);
        // XMPPTCPConnectionConfiguration.builder().setKeystoreType(null);

        config.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled);
        config.setServiceName(serviceName);
        config.setHost(serverAddress);
        config.setPort(5222);
        config.setDebuggerEnabled(true);
        XMPPTCPConnection.setUseStreamManagementDefault(true);
        connection = new XMPPTCPConnection(config.build());
        XMPPConnectionListener connectionListener = new XMPPConnectionListener();
        connection.addConnectionListener(connectionListener);
    }

I am connection this connection after initialise

 connection.connect();
                    DeliveryReceiptManager dm = DeliveryReceiptManager
                            .getInstanceFor(connection);
                    dm.setAutoReceiptMode(AutoReceiptMode.always);
                    dm.addReceiptReceivedListener(new ReceiptReceivedListener() {
                        @Override
                        public void onReceiptReceived(Jid fromJid, Jid toJid, String receiptId, Stanza receipt) {

                        }
                    });
                    connected = true;

Method to create new user

public void createNewUser(){
        try {
           /* UserRegisterUtil.registerAccount(connection,"Mathan","mathan@4792");
            connection.disconnect();
            connection.connect();*/
           Localpart lp = Localpart.from("IamHere");
            // Registering the user
            AccountManager accountManager = AccountManager.getInstance(connection);
            accountManager.sensitiveOperationOverInsecureConnection(true);
            accountManager.createAccount(lp, "mathan123"); // Skipping optional fields like email, first name, last name, etc..
            Toast.makeText(context, "=>User creation completed....",
                    Toast.LENGTH_LONG).show();
            Log.d("xmpp", ">User creation completed....!");
        } catch (SmackException.NoResponseException e) {
            e.printStackTrace();
        } catch (XMPPException.XMPPErrorException e) {
            e.printStackTrace();
        } catch (NotConnectedException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (XMPPException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (SmackException e) {
            e.printStackTrace();
        }
    }

It's an error I got It says Denied by ACL. But I don't know how to change it in windows ejabberd server.

  <error code='403' type='auth'><forbidden xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/><text xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'>Denied by ACL</text></error> 

Can anyone tell me how to create a new user, What I wanna change in this configuration file?

Also I have some doubt

How to get offline messages in android using smack library?

How to get chat history?

回答1:

About user creation: you need to connect (not login) first to your connection.

More, all username must be lowercase:

 accountManager.createAccount(lp.toLowerCase(), "mathan123");


About offline messages: they are dispatched when user will go online

About chat history: sorry, I don't have expirience with Ejabber, but for normal chat 1vs1 you probably need to store on client or add some custom feature on server (like Openfire).



回答2:

  1. Go to /etc/ejabberd/ejabberd.yml

  2. Almost at the end of file, under Section modules, change mod_register:ip_access:trusted_network to mod_register:ip_access:all