SASLErrorException: SASLError using DIGEST-MD5: not-authorized while using Smack 4.2

Posted

技术标签:

【中文标题】SASLErrorException: SASLError using DIGEST-MD5: not-authorized while using Smack 4.2【英文标题】: 【发布时间】:2015-08-03 12:15:26 【问题描述】:

我正在尝试制作一个简单的聊天应用程序。我一直在研究这个并发现了 smack 库。为此,我下载了所有库并在我的计算机中安装了 openfire 服务器,并在其中创建了几个用户进行测试。我终于可以连接到服务器,但后来登录失败。我尝试使用来自 spark 的相同用户名登录,它工作正常。我收到有关 SASL 身份验证的错误。需要一些帮助。

这些是我的导入:

import org.jivesoftware.smack.AbstractXMPPConnection;
import org.jivesoftware.smack.ConnectionConfiguration; 
import org.jivesoftware.smack.SmackException; 
import org.jivesoftware.smack.StanzaListener;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.filter.StanzaFilter;  
import org.jivesoftware.smack.filter.StanzaTypeFilter;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Presence;
import org.jivesoftware.smack.packet.Stanza;
import org.jivesoftware.smack.roster.Roster;
import org.jivesoftware.smack.roster.RosterEntry;
import org.jivesoftware.smack.tcp.XMPPTCPConnection;
import org.jivesoftware.smack.tcp.XMPPTCPConnectionConfiguration;
import org.jxmpp.jid.Jid;

这些是我的参数:

public static final String HOST = "192.168.1.114";
public static final int PORT = 5222;
public static final String SERVICE = "192.168.1.114"; // i dont know if its right.. 
public static final String USERNAME = "test";
public static final String PASSWORD = "test";

这是代码:

 public void connect()
  
    final ProgressDialog dialog = ProgressDialog.show(this, "Connecting...", "Please wait...", false);

    Thread t = new Thread(new Runnable() 
        @Override
        public void run() 
            // Create a connection
            //    ConnectionConfiguration connConfig = new ConnectionConfiguration(HOST, PORT, SERVICE);
            Log.v("checking", "011");

            XMPPTCPConnectionConfiguration.Builder connConfig = XMPPTCPConnectionConfiguration.builder();
            connConfig.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled);  // enabled gives me SSL certificate authorization errors...
            connConfig.setUsernameAndPassword(USERNAME, PASSWORD);

            try 
                connConfig.setServiceName(JidCreate.domainBareFrom(SERVICE));
             catch (XmppStringprepException e) 
                e.printStackTrace();
                Log.v("checking", e.toString() + "1");
            
            connConfig.setHost(HOST);
            connConfig.setPort(PORT);
            connConfig.setDebuggerEnabled(true);


            AbstractXMPPConnection connection = new XMPPTCPConnection(connConfig.build());

            try 
                connection.connect();
                Log.v("XMPPChatDemoActivity",  "[SettingsDialog] Connected to "+connection.getHost());
             catch (XMPPException ex) 
                Log.v("XMPPChatDemoActivity",  "[SettingsDialog] Failed to connect to "+ connection.getHost());            
                Log.v("XMPPChatDemoActivity", ex.toString());
                setConnection(null);
             catch (SmackException e) 
                e.printStackTrace();
                Log.v("XMPPChatDemoActivity", e.toString());
             catch (IOException e) 
                e.printStackTrace();
                Log.v("XMPPChatDemoActivity", e.toString());
             catch (InterruptedException e) 
                e.printStackTrace();
                Log.v("XMPPChatDemoActivity", e.toString());
            
            try 
                connection.login(USERNAME, PASSWORD);
                Log.v("XMPPChatDemoActivity", "Logged in as" + connection.getUser());

                // Set the status to available
                Presence presence = new Presence(Presence.Type.available);
                connection.sendStanza(presence);
                setConnection((XMPPTCPConnection) connection);

                Roster roster = Roster.getInstanceFor(connection);

                Collection<RosterEntry> entries = roster.getEntries();
                for (RosterEntry entry : entries) 

                    Log.v("XMPPChatDemoActivity",  "--------------------------------------");
                    Log.v("XMPPChatDemoActivity", "RosterEntry " + entry);
                    Log.v("XMPPChatDemoActivity", "User: " + entry.getUser());
                    Log.v("XMPPChatDemoActivity", "Name: " + entry.getName());
                    Log.v("XMPPChatDemoActivity", "Status: " + entry.getStatus());
                    Log.v("XMPPChatDemoActivity", "Type: " + entry.getType());
                    Presence entryPresence = roster.getPresence(entry.getUser());

                    Log.v("XMPPChatDemoActivity", "Presence Status: "+ entryPresence.getStatus());
                    Log.v("XMPPChatDemoActivity", "Presence Type: " + entryPresence.getType());

                    Presence.Type type = entryPresence.getType();
                    if (type == Presence.Type.available)
                        Log.d("XMPPChatDemoActivity", "Presence AVIALABLE");
                    Log.v("XMPPChatDemoActivity", "Presence : " + entryPresence);
                
             catch (XMPPException ex) 
                Log.v("XMPPChatDemoActivity", "Failed to log in as "+  USERNAME + " or " + connection.getHost());
                Log.v("XMPPChatDemoActivity", ex.toString());

                setConnection(null);
             catch (SmackException.NotConnectedException e) 
                e.printStackTrace();
             catch (SmackException e) 
                e.printStackTrace();
             catch (IOException e) 
                e.printStackTrace();
             catch (InterruptedException e) 
                e.printStackTrace();
            
            dialog.dismiss();
        
    );
    t.start();
    dialog.show();

这是来自 logcat 的 sn-p:

08-03 06:56:26.812    8824-8824/com.example.sumit.testapp V/ActivityThread﹕ updateVisibility : ActivityRecord15e86073 token=android.os.BinderProxy@26aa149d com.example.sumit.testapp/com.example.sumit.testapp.MainActivity show : true
08-03 06:56:26.902    8824-8874/com.example.sumit.testapp D/SMACK﹕ SENT (0): <stream:stream xmlns='jabber:client' to='192.168.1.114' xmlns:stream='http://etherx.jabber.org/streams' version='1.0' from='test@192.168.1.114' xml:lang='en'>
08-03 06:56:26.922    8824-8875/com.example.sumit.testapp D/SMACK﹕ RECV (0): <?xml version='1.0' encoding='UTF-8'?><stream:stream xmlns:stream="http://etherx.jabber.org/streams" xmlns="jabber:client" from="bahadurxxx" id="e61566be" xml:lang="en" version="1.0">
08-03 06:56:26.922    8824-8875/com.example.sumit.testapp D/SMACK﹕ RECV (0): <stream:features><starttls xmlns="urn:ietf:params:xml:ns:xmpp-tls"></starttls><mechanisms xmlns="urn:ietf:params:xml:ns:xmpp-sasl"><mechanism>DIGEST-MD5</mechanism><mechanism>PLAIN</mechanism><mechanism>ANONYMOUS</mechanism><mechanism>CRAM-MD5</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>
08-03 06:56:26.932    8824-8863/com.example.sumit.testapp V/XMPPChatDemoActivity﹕ [SettingsDialog] Connected to 192.168.1.114
08-03 06:56:26.932    8824-8824/com.example.sumit.testapp I/Timeline﹕ Timeline: Activity_idle id: android.os.BinderProxy@26aa149d time:22662680

08-03 06:56:26.932    8824-8874/com.example.sumit.testapp D/SMACK﹕ SENT (0): <auth xmlns='urn:ietf:params:xml:ns:xmpp-sasl' mechanism='DIGEST-MD5'>=</auth>
08-03 06:56:26.932    8824-8875/com.example.sumit.testapp D/SMACK﹕ RECV (0): <challenge xmlns="urn:ietf:params:xml:ns:xmpp-sasl">cmVhbG09ImJhaGFkdXJ4eHgiLG5vbmNlPSJIVEVaUU5kRWpJM0cxRUtoWHFVYVM4akhyZkpNdTMyaTFPYVBETkJyIixxb3A9ImF1dGgiLGNoYXJzZXQ9dXRmLTgsYWxnb3JpdGhtPW1kNS1zZXNz</challenge>
08-03 06:56:26.932    8824-8874/com.example.sumit.testapp D/SMACK﹕ SENT (0): <response xmlns='urn:ietf:params:xml:ns:xmpp-sasl'>dXNlcm5hbWU9InRlc3QiLHJlYWxtPSIxOTIuMTY4LjEuMTE0Iixub25jZT0iSFRFWlFOZEVqSTNHMUVLaFhxVWFTOGpIcmZKTXUzMmkxT2FQRE5CciIsY25vbmNlPSJVUDR5c3dpOVlMMDY3RTNvMTZrNTVwT1FKdDEzMWVSWSIsbmM9MDAwMDAwMDEscW9wPWF1dGgsZGlnZXN0LXVyaT0ieG1wcC8xOTIuMTY4LjEuMTE0IixyZXNwb25zZT04NzllZmVjZGJhMGUwMDc1N2FmZWYxZmQxOWVhYTI0MyxjaGFyc2V0PXV0Zi04</response>
08-03 06:56:26.942    8824-8875/com.example.sumit.testapp D/SMACK﹕ RECV (0): <failure xmlns="urn:ietf:params:xml:ns:xmpp-sasl"><not-authorized/></failure>
08-03 06:56:26.942    8824-8863/com.example.sumit.testapp V/XMPPChatDemoActivity﹕ Failed to log in as test or 192.168.1.114
08-03 06:56:26.942    8824-8863/com.example.sumit.testapp V/XMPPChatDemoActivity﹕ org.jivesoftware.smack.sasl.SASLErrorException: SASLError using DIGEST-MD5: not-authorized
08-03 06:56:30.582    8824-8824/com.example.sumit.testapp I/Timeline﹕ Timeline: Activity_idle id: android.os.BinderProxy@26aa149d time:22666335
08-03 06:56:30.622    8824-8824/com.example.sumit.testapp E/SpannableStringBuilder﹕ SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
08-03 06:56:30.622    8824-8824/com.example.sumit.testapp E/SpannableStringBuilder﹕ SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
08-03 06:59:27.082    8824-8875/com.example.sumit.testapp D/SMACK﹕ RECV (0): <iq type="get" id="553-31" from="bahadurxxx" to="bahadurxxx/e61566be"><ping xmlns="urn:xmpp:ping"/></iq>
08-03 06:59:27.092    8824-8874/com.example.sumit.testapp D/SMACK﹕ SENT (0): <iq to='bahadurxxx' id='553-31' type='result'></iq>

【问题讨论】:

【参考方案1】:

您的客户端正在尝试使用领域“192.168.1.114”进行身份验证,而服务器仅提供领域“bahadurxxx”。那是行不通的。您应该将SERVICE 设置为“bahadurxxx”或将服务器的主机名更改为“192.168.1.114”。

每个 XMPP 地址都有一个域,如 user@example.com 中的 example.com,就像电子邮件一样。这是您必须进行身份验证的域,也是您将通过 XMPP 用于您自己的服务器的地址。但是,XMPP 服务器不一定需要运行在example.com 上,可能服务器实际上在xmpp.example.com 上。 SRV 记录是表明example.com 存在于xmpp.example.com 上的一种方式。

在 Smack 中,.setServiceName() 设置您要使用的 XMPP 域(毫不奇怪,它已重命名为 .setXmppDomain())。 .setHost() 可以用作另一种表示您想使用xmpp.example.com 的方式。

【讨论】:

非常感谢您,先生。另外,我究竟应该使用什么服务名称,或者更准确地说,什么是服务?

以上是关于SASLErrorException: SASLError using DIGEST-MD5: not-authorized while using Smack 4.2的主要内容,如果未能解决你的问题,请参考以下文章