首页 文章

连接到openfire服务器时出现Samck 4.1错误(org.jivesoftware.smack.sasl.SASLErrorException:使用DIGEST-MD5的SASLError:未经授权)

提问于
浏览
2

public void connect(){AsyncTask connectionThread = new AsyncTask(){@ Override protected Boolean doInBackground(Void ... arg0){boolean isConnected = false;

StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);

            XMPPTCPConnectionConfiguration.Builder configBuilder = XMPPTCPConnectionConfiguration.builder();
            configBuilder.setUsernameAndPassword("ahsan", "vbase123");
            configBuilder.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled);
            configBuilder.setResource("test");
            configBuilder.setServiceName("192.168.10.106");
            configBuilder.setHost("192.168.10.106");
            configBuilder.setPort(5222);
            configBuilder.setSendPresence(true);
            configBuilder.setDebuggerEnabled(true);
         //configBuilder.setSocketFactory(SSLSocketFactory.getDefault());
           /* try {
                TLSUtils.acceptAllCertificates(configBuilder);
            } catch (NoSuchAlgorithmException e) {
            } catch (KeyManagementException e) {
            }

            configBuilder.setHostnameVerifier(new HostnameVerifier() {
                @Override
                public boolean verify(String hostname, SSLSession session) {
                    return true;
                }
            });
  • /
connection = new XMPPTCPConnection(configBuilder.build());
            // Connect to the server
            try {Log.d("XAMPP","connected to server");
                connection.connect();
                Log.d("XAMPP","connected to server");
            } catch (SmackException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (XMPPException e) {
                e.printStackTrace();
            }
            // Log into the server
            try {
                Log.d("XAMPP","Login to server");
                connection.login();
                Presence presence = new Presence(Presence.Type.available);
                connection.sendPacket(presence);
                setConnection(connection);
                Roster roster = Roster.getInstanceFor(connection);
                Collection<RosterEntry> entries = roster.getEntries();
                for (RosterEntry entry : entries) {

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

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

                    Presence.Type type = entryPresence.getType();
                    if (type == Presence.Type.available)
                        Log.d("XMPPChatDemoActivity", "Presence AVIALABLE");
                    Log.d("XMPPChatDemoActivity", "Presence : " + entryPresence);
                }
                Log.d("XAMPP", "login to server");
            } catch (XMPPException e) {
                e.printStackTrace();
            } catch (SmackException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

            // Disconnect from the server
           // connection.disconnect();

            return isConnected;
        }
    };
    connectionThread.execute();
}

此代码的错误堆栈跟踪如下:

5-08 18:24:04.162    1305-1493/com.vb.testapp D/XAMPP﹕ connected to server
05-08 18:24:04.185    1305-1502/com.vb.testapp D/SMACK﹕ SENT (0): <stream:stream xmlns='jabber:client' to='192.168.10.106' xmlns:stream='http://etherx.jabber.org/streams' version='1.0' from='ahsan@virtualbase@192.168.10.106' xml:lang='en'>
05-08 18:24:04.189    1305-1503/com.vb.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="virtualbase" id="da5ea5f2" xml:lang="en" version="1.0">
05-08 18:24:04.194    1305-1503/com.vb.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>
05-08 18:24:04.196    1305-1493/com.vb.testapp D/XAMPP﹕ connected to server
05-08 18:24:04.196    1305-1493/com.vb.testapp D/XAMPP﹕ Login to server
05-08 18:24:04.198    1305-1502/com.vb.testapp D/SMACK﹕ SENT (0): <auth xmlns='urn:ietf:params:xml:ns:xmpp-sasl' mechanism='DIGEST-MD5'>=</auth>
05-08 18:24:04.204    1305-1503/com.vb.testapp D/SMACK﹕ RECV (0): <challenge xmlns="urn:ietf:params:xml:ns:xmpp-sasl">cmVhbG09InZpcnR1YWxiYXNlIixub25jZT0icEZvL1NsYkV3Rms3cTBRNUlpNTlEc2I5RFJMM2FRQ2NuNmF4VVdBOSIscW9wPSJhdXRoIixjaGFyc2V0PXV0Zi04LGFsZ29yaXRobT1tZDUtc2Vzcw==</challenge>
05-08 18:24:04.206    1305-1502/com.vb.testapp D/SMACK﹕ SENT (0): <response xmlns='urn:ietf:params:xml:ns:xmpp-sasl'>dXNlcm5hbWU9ImFoc2FuQHZpcnR1YWxiYXNlIixyZWFsbT0iMTkyLjE2OC4xMC4xMDYiLG5vbmNlPSJwRm8vU2xiRXdGazdxMFE1SWk1OURzYjlEUkwzYVFDY242YXhVV0E5Iixjbm9uY2U9IjBWT1lBRFE4U09ZNDJDVUcxWldCSG9JMTRJbjFYdzdtIixuYz0wMDAwMDAwMSxxb3A9YXV0aCxkaWdlc3QtdXJpPSJ4bXBwLzE5Mi4xNjguMTAuMTA2IixyZXNwb25zZT0xNDkxNmJjMDFhODI3MGVmNjA4ODViZTNmNmQ1NzA2MixjaGFyc2V0PXV0Zi04</response>
05-08 18:24:04.209    1305-1503/com.vb.testapp D/SMACK﹕ RECV (0): <failure xmlns="urn:ietf:params:xml:ns:xmpp-sasl"><not-authorized/></failure>
05-08 18:24:04.212    1305-1493/com.vb.testapp W/System.err﹕ org.jivesoftware.smack.sasl.SASLErrorException: SASLError using DIGEST-MD5: not-authorized
05-08 18:24:04.212    1305-1493/com.vb.testapp W/System.err﹕ at org.jivesoftware.smack.SASLAuthentication.authenticationFailed(SASLAuthentication.java:365)
05-08 18:24:04.212    1305-1493/com.vb.testapp W/System.err﹕ at org.jivesoftware.smack.tcp.XMPPTCPConnection$PacketReader.parsePackets(XMPPTCPConnection.java:1011)
05-08 18:24:04.212    1305-1493/com.vb.testapp W/System.err﹕ at org.jivesoftware.smack.tcp.XMPPTCPConnection$PacketReader.access$200(XMPPTCPConnection.java:915)
05-08 18:24:04.212    1305-1493/com.vb.testapp W/System.err﹕ at org.jivesoftware.smack.tcp.XMPPTCPConnection$PacketReader$1.run(XMPPTCPConnection.java:930)
05-08 18:24:04.212    1305-1493/com.vb.testapp W/System.err﹕ at java.lang.Thread.run(Thread.java:818)

2 回答

  • 2

    尝试在配置连接时添加这些行

    configBuilder.setSecurityMode(XMPPTCPConnectionConfiguration.SecurityMode.required );
    SASLMechanism mechanism = new SASLDigestMD5Mechanism();
    SASLAuthentication.registerSASLMechanism(mechanism);
    SASLAuthentication.blacklistSASLMechanism("SCRAM-SHA-1");  
    SASLAuthentication.unBlacklistSASLMechanism("DIGEST-MD5");
    
  • 2

    导入 smack-sasl-provided-4.1.0.jar 以消除此SASLAuthentication错误 .
    我遇到了同样的问题,并通过导入上面提到的lib来解决 .
    另外看看this question .
    我早些时候在SO上发布过 .

相关问题