SASL错误使用PLAIN:未经授权

14
我是一位有用的助手,可以为您翻译文本。以下是需要翻译的内容:

我正在尝试将我的应用从 aSmack 切换到 Smack 4.1.1。然而,在使用 Smack 时,我在相同的服务器和登录详情方面遇到了麻烦,而在 aSmack 上却非常出色。

这是我在 aSmack 中登录的旧代码 -

void startConnect(boolean sslFlag) throws XMPPException, SmackException, IOException {
        ConnectionConfiguration connectionConfig =
                new ConnectionConfiguration(HOST, Integer.parseInt(PORT), SERVICE);
        connectionConfig.setDebuggerEnabled(true);
        connectionConfig.setCompressionEnabled(false);

        if (sslFlag)
            SASLAuthentication.supportSASLMechanism("PLAIN", 0);

        XMPPConnection connection = new XMPPTCPConnection(connectionConfig);

            connection.connect();
            connection.login(mUserName, mUserPassword, getResource());

            // Set status to online / available
            Presence presence = new Presence(Presence.Type.available);
            connection.sendPacket(presence);
            setConnection(connection);
    }

我正在尝试使用类似的方式在Smack中编写代码 -

public void init() {
        SmackConfiguration.DEBUG = true;
        XMPPTCPConnectionConfiguration config = XMPPTCPConnectionConfiguration.builder()
                .setHost(SERVICE_NAME)
                .setPort(5222)
                .setServiceName(SERVICE_NAME)
                .setSecurityMode(ConnectionConfiguration.SecurityMode.disabled)
                .setDebuggerEnabled(true)
                .build();
        mConnection = new XMPPTCPConnection(config);

//I have tried with blacklisting and unblacklisting all three mechanism.
        //SASLMechanism mechanism = new SASLPlainMechanism(); //This didn't help
        //SASLAuthentication.registerSASLMechanism(mechanism);
        SASLAuthentication.blacklistSASLMechanism("SCRAM-SHA-1");
        SASLAuthentication.blacklistSASLMechanism("DIGEST-MD5");
        SASLAuthentication.unBlacklistSASLMechanism("PLAIN"); 
        try {
            mConnection.connect();
            return;
        } catch (SmackException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (XMPPException e) {
            e.printStackTrace();
        }
        mConnection = null;
    }

    public void login(String username, String password) throws IOException, XMPPException, SmackException {

        if(mConnection==null || !mConnection.isConnected()){
            init();
            if(mConnection==null){
                throw new IOException();
            }
        }
        mConnection.login(username/*+"@"+SERVICE_NAME*/, password); //I have tried both the method by adding @Domain part and without it.

        mChatManager = ChatManager.getInstanceFor(mConnection);
        mChatManager.addChatListener(this);
    }

这是我得到的异常 -

05-21 21:22:29.782  19536-23179/test W/System.err﹕ org.jivesoftware.smack.sasl.SASLErrorException: SASLError using PLAIN: not-authorized
05-21 21:22:29.782  19536-23179/test W/System.err﹕ at org.jivesoftware.smack.SASLAuthentication.authenticationFailed(SASLAuthentication.java:365)
05-21 21:22:29.792  19536-23179/test W/System.err﹕ at org.jivesoftware.smack.tcp.XMPPTCPConnection$PacketReader.parsePackets(XMPPTCPConnection.java:1033)
05-21 21:22:29.792  19536-23179/test W/System.err﹕ at org.jivesoftware.smack.tcp.XMPPTCPConnection$PacketReader.access$200(XMPPTCPConnection.java:937)
05-21 21:22:29.792  19536-23179/test W/System.err﹕ at org.jivesoftware.smack.tcp.XMPPTCPConnection$PacketReader$1.run(XMPPTCPConnection.java:952)
05-21 21:22:29.792  19536-23179/test W/System.err﹕ at java.lang.Thread.run(Thread.java:856)

这个异常与SCRAM和DIGEST-MD5相同,只是名称不同。
这是我从服务器获取的内容 -
05-21 21:22:29.512  19536-23189/test D/SMACK﹕ SENT (0): <stream:stream xmlns='jabber:client' to='xmpp.example.com' xmlns:stream='http://etherx.jabber.org/streams' version='1.0' xml:lang='en'>
05-21 21:22:29.642  19536-23190/test D/SMACK﹕ RECV (0): <?xml version='1.0'?><stream:stream xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' id='214326363' from='xmpp.example.com' version='1.0' xml:lang='en'><stream:features><compression xmlns='http://jabber.org/features/compress'><method>zlib</method></compression><mechanisms xmlns='urn:ietf:params:xml:ns:xmpp-sasl'><mechanism>SCRAM-SHA-1</mechanism><mechanism>PLAIN</mechanism><mechanism>DIGEST-MD5</mechanism></mechanisms><c xmlns='http://jabber.org/protocol/caps' hash='sha-1' node='http://www.process-one.net/en/ejabberd/' ver='hX7OB6oTZugjNIFHZvd95k5UYzc='/><register xmlns='http://jabber.org/features/iq-register'/></stream:features>
05-21 21:22:29.652  19536-23189/test D/SMACK﹕ SENT (0): <auth xmlns='urn:ietf:params:xml:ns:xmpp-sasl' mechanism='PLAIN'>ADEwMDAAMTAwMA==</auth>
05-21 21:22:29.782  19536-23190/test D/SMACK﹕ RECV (0): <failure xmlns='urn:ietf:params:xml:ns:xmpp-sasl'><not-authorized/></failure>

这里可能存在什么问题?我尝试了所有三种机制:PLAIN、DIGEST-MD5和默认的SCRAM-SHA-1。我也尝试过加入域名和不加入域名。我试着在配置中添加用户名和密码或直接在登录方法中添加它们。
我也尝试使用所需的安全模式,但会产生以下错误 -
05-21 21:27:53.658  25643-26009/test D/SMACK﹕ SENT (0): <stream:stream xmlns='jabber:client' to='xmpp.example.com' xmlns:stream='http://etherx.jabber.org/streams' version='1.0' xml:lang='en'>
05-21 21:27:53.788  25643-26010/test D/SMACK﹕ RECV (0): <?xml version='1.0'?><stream:stream xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' id='4179863647' from='xmpp.example.com' version='1.0' xml:lang='en'><stream:features><compression xmlns='http://jabber.org/features/compress'><method>zlib</method></compression><mechanisms xmlns='urn:ietf:params:xml:ns:xmpp-sasl'><mechanism>SCRAM-SHA-1</mechanism><mechanism>PLAIN</mechanism><mechanism>DIGEST-MD5</mechanism></mechanisms><c xmlns='http://jabber.org/protocol/caps' hash='sha-1' node='http://www.process-one.net/en/ejabberd/' ver='hX7OB6oTZugjNIFHZvd95k5UYzc='/><register xmlns='http://jabber.org/features/iq-register'/></stream:features>
05-21 21:27:54.229  25643-26009/test D/SMACK﹕ SENT (0): <auth xmlns='urn:ietf:params:xml:ns:xmpp-sasl' mechanism='PLAIN'>ADEwMDAAMTAwMA==</auth>
05-21 21:27:59.264  25643-25970/test W/System.err﹕ org.jivesoftware.smack.SmackException$NoResponseException: No response received within reply timeout. Timeout was 5000ms (~5s). Used filter: No filter used or filter was 'null'.
05-21 21:27:59.454  25643-26010/test W/AbstractXMPPConnection﹕ Connection closed with error
    org.jivesoftware.smack.SmackException$SecurityRequiredByClientException: SSL/TLS required by client but not supported by server
            at org.jivesoftware.smack.tcp.XMPPTCPConnection.afterFeaturesReceived(XMPPTCPConnection.java:898)
            at org.jivesoftware.smack.AbstractXMPPConnection.parseFeatures(AbstractXMPPConnection.java:1367)
            at org.jivesoftware.smack.tcp.XMPPTCPConnection.access$800(XMPPTCPConnection.java:139)
            at org.jivesoftware.smack.tcp.XMPPTCPConnection$PacketReader.parsePackets(XMPPTCPConnection.java:998)
            at org.jivesoftware.smack.tcp.XMPPTCPConnection$PacketReader.access$200(XMPPTCPConnection.java:937)
            at org.jivesoftware.smack.tcp.XMPPTCPConnection$PacketReader$1.run(XMPPTCPConnection.java:952)
            at java.lang.Thread.run(Thread.java:856)
05-21 21:27:59.494  25643-25970/test W/System.err﹕ at org.jivesoftware.smack.SASLAuthentication.authenticate(SASLAuthentication.java:250)
05-21 21:27:59.494  25643-25970/test W/System.err﹕ at org.jivesoftware.smack.tcp.XMPPTCPConnection.loginNonAnonymously(XMPPTCPConnection.java:365)
05-21 21:27:59.524  25643-25970/test W/System.err﹕ at org.jivesoftware.smack.AbstractXMPPConnection.login(AbstractXMPPConnection.java:452)
05-21 21:27:59.544  25643-25970/test W/System.err﹕ at org.jivesoftware.smack.AbstractXMPPConnection.login(AbstractXMPPConnection.java:427)
05-21 21:27:59.574  25643-25970/test W/System.err﹕ at test.Managers.XMPPManager.login(XMPPManager.java:84)
05-21 21:27:59.594  25643-25970/test W/System.err﹕ at test.API.LoginAPI.callAPI(LoginAPI.java:31)
05-21 21:27:59.604  25643-25970/test W/System.err﹕ at test.API.BaseAPI$XMPPTask.doInBackground(BaseAPI.java:70)
05-21 21:27:59.624  25643-25647/test D/dalvikvm﹕ GC_CONCURRENT freed 1558K, 17% free 30564K/36743K, paused 13ms+32ms, total 111ms
05-21 21:27:59.624  25643-25970/test W/System.err﹕ at test.API.BaseAPI$XMPPTask.doInBackground(BaseAPI.java:45)
05-21 21:27:59.624  25643-25970/test W/System.err﹕ at android.os.AsyncTask$2.call(AsyncTask.java:287)
05-21 21:27:59.624  25643-25970/test W/System.err﹕ at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
05-21 21:27:59.664  25643-25970/test W/System.err﹕ at java.util.concurrent.FutureTask.run(FutureTask.java:137)
05-21 21:27:59.664  25643-25970/test W/System.err﹕ at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
05-21 21:27:59.664  25643-25970/test W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
05-21 21:27:59.664  25643-25970/test W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
05-21 21:27:59.664  25643-25970/test W/System.err﹕ at java.lang.Thread.run(Thread.java:856)

注:
我需要在Smack 4.1.1中解决该问题。我的代码在aSmack中已经运行良好,我需要将其升级到Smack。现在我想这已经很清楚了。

5个回答

25
我也遇到了同样的错误。
在连接到xmpp服务器之后,我调用了
mConnection.login("test@192.168.0.166", "ilink@2012");

但它没有起作用。
为了解决这个问题,我在我的代码中做了如下更改。

SASLAuthentication.unBlacklistSASLMechanism("PLAIN");
SASLAuthentication.blacklistSASLMechanism("DIGEST-MD5");
mConnection.login("test", "ilink@2012");

我添加了两行代码,用于将 DIGEST-MD5 加入黑名单列表,启用 PLAIN SASLMechanism 并从用户名中删除 IP 地址。

以下是我的完整工作代码,供您参考。

package com.ilink.xmpptest;

import java.io.IOException;
import org.jivesoftware.smack.AbstractXMPPConnection;
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.ConnectionListener;
import org.jivesoftware.smack.SASLAuthentication;
import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.tcp.XMPPTCPConnection;
import org.jivesoftware.smack.tcp.XMPPTCPConnectionConfiguration;

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends ActionBarActivity implements ConnectionListener {
    private static final String TAG = MainActivity.class.getSimpleName();

    private AbstractXMPPConnection mConnection;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        new ConnectToXmppServer().execute();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void authenticated(XMPPConnection arg0, boolean arg1) {
        Log.i(TAG, "Authenticated");
    }

    @Override
    public void connected(XMPPConnection arg0) {
        Log.i(TAG, "Connected");
        try {
            SASLAuthentication.unBlacklistSASLMechanism("PLAIN");
            SASLAuthentication.blacklistSASLMechanism("DIGEST-MD5");
            mConnection.login("test", "ilink@2012");
        } catch (XMPPException | SmackException | IOException e) {
            e.printStackTrace();
            Log.e(TAG, e.getMessage());
        }
    }

    @Override
    public void connectionClosed() {
        Log.i(TAG, "Connection closed");
    }

    @Override
    public void connectionClosedOnError(Exception arg0) {
        Log.i(TAG, "Connection closed on error");
    }

    @Override
    public void reconnectingIn(int arg0) {
        Log.i(TAG, "Reconnecting in");
    }

    @Override
    public void reconnectionFailed(Exception arg0) {
        Log.i(TAG, "Reconnection failed");
    }

    @Override
    public void reconnectionSuccessful() {
        Log.i(TAG, "Reconnection successful");
    }

    private class ConnectToXmppServer extends AsyncTask<Void, Void, Void> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            Log.i(TAG, "Connecting to xmpp server started...");
        }

        @Override
        protected Void doInBackground(Void... params) {
            try {
                XMPPTCPConnectionConfiguration config = XMPPTCPConnectionConfiguration
                        .builder()
                        .setSecurityMode(ConnectionConfiguration.SecurityMode.disabled)
                        .setServiceName("192.168.0.166")
                        .setHost("192.168.0.166")
                        .setPort(5222)
                        .setCompressionEnabled(false).build();
                mConnection = new XMPPTCPConnection(config);
                mConnection.setPacketReplyTimeout(1000);
                mConnection.addConnectionListener(MainActivity.this);
                mConnection.connect();
            } catch (XMPPException | SmackException | IOException e) {
                e.printStackTrace();
                Log.e(TAG, e.getMessage());
            }

            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
            Log.i(TAG, "Connecting to xmpp server finished...");
        }
    }
}

4
非常感谢!关于这个问题缺乏文档实在太糟糕了,我已经花了两天时间搜索相关信息但收效甚微......你的帖子是我看到的最有帮助的。请多多指教! - Fingolfin

7

好的,那是一个非常愚蠢的错误。目前的代码完全没有问题。我不小心把用户名放在了密码的位置。当我试图回到aSmack时,我才发现了这个错误。


在我的情况下,我发送了错误的用户名和密码;-) - Farrukh Najmi
我犯了完全相同的错误,天哪 - Gabbr Issimo

5

授权策略:

PLAIN认证 - 取消对PLAIN的黑名单限制,禁止使用SHA-1和MD5

MD5认证 - 取消对MD5的黑名单限制,禁止使用SHA-1和PLAIN

SCRAM-SHA-1认证 - 取消对SCRAM-SHA-1和PLAIN的黑名单限制,禁止使用MD5

SASLAuthentication.unBlacklistSASLMechanism("AuthName");
SASLAuthentication.blacklistSASLMechanism("AuthName");

认证名称: PLAINSCRAM-SHA-1MD5

** 在服务器端检查启用了哪种认证方法


4
        SASLAuthentication.blacklistSASLMechanism("SCRAM-SHA-1");
        SASLAuthentication.blacklistSASLMechanism("DIGEST-MD5");
        SASLAuthentication.unBlacklistSASLMechanism("PLAIN");

对于非SSL连接,这对我有用。(在connected方法中)

编辑: 如果用户帐户已在服务器上创建,则可能会收到此错误消息。请检查它是来自注册(帐户创建)还是登录。


0

我认为以下属性适合您,我正在使用3.1版本

config.setVerifyChainEnabled(false);
config.setReconnectionAllowed(true);
config.setSASLAuthenticationEnabled(false);
config.setSecurityMode(SecurityMode.disabled);
config.setDebuggerEnabled(false);

2
这并不是一个正确的答案。XMPPTCPConnectionConfiguration并没有所有这些方法。我猜你是从aSmack的角度来回答的。我的问题是关于Smack 4.1.1的。 - 0xC0DED00D
我正在使用3.1版本,这就是为什么我写下了“我认为”的原因。 - Jaspreet Chhabra

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