【问题标题】:Smack and SASL Authentication error - No known authentication mechanisimsSmack 和 SASL 身份验证错误 - 没有已知的身份验证机制
【发布时间】:2015-04-01 14:43:00
【问题描述】:

我正在尝试使用最新版本的 Smack 4.1.0-beta 创建 XMPP 客户端。但是我在尝试登录本地运行的 OpenFire 服务器时遇到了错误。

org.jivesoftware.smack.SmackException: SASL Authentication failed. No known authentication mechanisims.

我尝试了各种用户凭据组合,但到目前为止都没有运气。尝试使用 Pidgin 或 Adium 连接到服务器时,一切正常。任何线索我在代码中遗漏了什么?

XMPPTCPConnectionConfiguration config = XMPPTCPConnectionConfiguration.builder()
            .setUsernameAndPassword("admin", "admin")
            .setServiceName("localhost")
            .setHost("localhost")
            .setSecurityMode(ConnectionConfiguration.SecurityMode.disabled)
            .setPort(5222)
            .build();

    AbstractXMPPConnection connection = new XMPPTCPConnection(config);

    try {

        connection.connect();

        connection.login();

        connection.disconnect();

    } catch (SmackException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (XMPPException e) {
        e.printStackTrace();
    }

【问题讨论】:

    标签: java xmpp openfire smack


    【解决方案1】:

    尝试使用主机/服务名称的 IP 地址

    【讨论】:

    • 这不是一个好主意。移动到另一台服务器时,IP 可能会发生变化,您必须更新在商店中发布的应用程序。这需要时间,而且不是一个好主意。
    【解决方案2】:

    我错误地导入了错误的依赖项。在查看文档 (https://github.com/igniterealtime/Smack/wiki/Smack-4.1-Readme-and-Upgrade-Guide) 时,使用 Gradle 导入正确的依赖项解决了这个问题。

    compile("org.igniterealtime.smack:smack-java7:4.1.0-beta1")
    compile("org.igniterealtime.smack:smack-tcp:4.1.0-beta1")
    compile("org.igniterealtime.smack:smack-extensions:4.1.0-beta1")
    

    【讨论】:

      【解决方案3】:

      这是完整的解决方案,请仔细查看...

      public class NewClientActivity extends Activity {
          EditText etUsername, etPassword;
          Button bSubmit;
          AbstractXMPPConnection mConnection;
          ConnectionListener connectionListener = new ConnectionListener() {
              @Override
              public void connected(XMPPConnection xmppConnection) {
                  Log.d("xmpp", "connected");
                  try {
                      SASLAuthentication.registerSASLMechanism(new SASLMechanism() {
                          @Override
                          protected void authenticateInternal(CallbackHandler callbackHandler) throws SmackException {
      
                          }
      
                          @Override
                          protected byte[] getAuthenticationText() throws SmackException {
                              byte[] authcid = toBytes('\u0000' + this.authenticationId);
                              byte[] passw = toBytes('\u0000' + this.password);
                              return ByteUtils.concact(authcid, passw);
                          }
      
                          @Override
                          public String getName() {
                              return "PLAIN";
                          }
      
                          @Override
                          public int getPriority() {
                              return 410;
                          }
      
                          @Override
                          public void checkIfSuccessfulOrThrow() throws SmackException {
      
                          }
      
                          @Override
                          protected SASLMechanism newInstance() {
                              return this;
                          }
                      });
                      mConnection.login();
                  } catch (XMPPException e) {
                      runOnUiThread(new Runnable() {
                          @Override
                          public void run() {
                              Toast.makeText(NewClientActivity.this, "Incorrect username or password", Toast.LENGTH_LONG).show();
                          }
                      });
                      e.printStackTrace();
                  } catch (SmackException e) {
                      e.printStackTrace();
                  } catch (IOException e) {
                      e.printStackTrace();
                  }
              }
      
              @Override
              public void authenticated(XMPPConnection xmppConnection, boolean b) {
                  Log.d("xmpp", "authenticated");
                  runOnUiThread(new Runnable() {
                      @Override
                      public void run() {
                          Toast.makeText(NewClientActivity.this,"Logged in successfully...",Toast.LENGTH_LONG ).show();
                      }
                  });
              }
      
              @Override
              public void connectionClosed() {
                  Log.d("xmpp", "connection closed");
              }
      
              @Override
              public void connectionClosedOnError(Exception e) {
                  Log.d("xmpp", "cononection closed on error");
              }
      
              @Override
              public void reconnectionSuccessful() {
                  Log.d("xmpp", "reconnection successful");
              }
      
              @Override
              public void reconnectingIn(int i) {
                  Log.d("xmpp", "reconnecting in " + i);
              }
      
              @Override
              public void reconnectionFailed(Exception e) {
                  Log.d("xmpp", "reconnection failed");
              }
          };
      
          @Override
          protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_new_client);
              findViews();
          }
      
          private void findViews() {
              etUsername = (EditText) findViewById(R.id.etUsername);
              etPassword = (EditText) findViewById(R.id.etPassword);
              bSubmit = (Button) findViewById(R.id.bSubmit);
              bSubmit.setOnClickListener(new View.OnClickListener() {
                  @Override
                  public void onClick(View v) {
                      String[] params = new String[]{etUsername.getText().toString(), etPassword.getText().toString()};
                      new Connect().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, params);
                  }
              });
          }
      
          class Connect extends AsyncTask<String, Void, Void> {
              @Override
              protected Void doInBackground(String... params) {
      
                  XMPPTCPConnectionConfiguration config = null;
                  XMPPTCPConnectionConfiguration.Builder builder = XMPPTCPConnectionConfiguration.builder();
                  builder.setServiceName("192.168.1.60").setHost("192.168.1.60")
                          .setDebuggerEnabled(true)
                          .setPort(5222)
                          .setUsernameAndPassword(params[0], params[1])
                          .setSecurityMode(ConnectionConfiguration.SecurityMode.disabled)
                          .setCompressionEnabled(false);
                  config = builder.build();
      
                  mConnection = new XMPPTCPConnection(config);
                  try {
                      mConnection.setPacketReplyTimeout(10000);
                      mConnection.addConnectionListener(connectionListener);
                      mConnection.connect();
                  } catch (SmackException e) {
                      e.printStackTrace();
                  } catch (IOException e) {
                      e.printStackTrace();
                  } catch (XMPPException e) {
                      e.printStackTrace();
                  }
                  return null;
              }
          }
      }
      

      更新 :- 对于 smack 4.2.0-beta2 版本,使用以下代码配置 XmppConnection 以进行 PLAIN 身份验证。

      XMPPTCPConnectionConfiguration.Builder builder = XMPPTCPConnectionConfiguration.builder();
      builder.setHost("example.com");
      builder.setPort(5222);
      /*builder.setServiceName("example.com");*/ //for older version < 4.2.0-beta2
      try
      {
          builder.setXmppDomain(JidCreate.domainBareFrom("example.com"));
      }
      catch (XmppStringprepException e)
      {
          e.printStackTrace();
      }
      /*builder.setServiceName("example.com");*/
      builder.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled);
      builder.setCompressionEnabled(true);
      builder.setConnectTimeout(30000);
      /*builder.setSendPresence(false);*/
      try
      {
          TLSUtils.acceptAllCertificates(builder);
      }
      catch (NoSuchAlgorithmException|KeyManagementException e)
      {
          e.printStackTrace();
      }
      TLSUtils.disableHostnameVerificationForTlsCertificates(builder);
      final Map<String, String> registeredSASLMechanisms = SASLAuthentication.getRegisterdSASLMechanisms();
      for(String mechanism:registeredSASLMechanisms.values())
      {
          SASLAuthentication.blacklistSASLMechanism(mechanism);
      }
      SASLAuthentication.unBlacklistSASLMechanism(SASLPlainMechanism.NAME);
      
      xmppConnection=new XMPPTCPConnection(builder.build());
      

      【讨论】:

      • 像魅力一样工作......你的 Xmpp 超级英雄:D
      • 感谢@KhalidElSayed,我最近更新了答案,如果您使用的是smack 的较新版本,即4.2.0-beta2,请使用它
      • 感谢@GopalSinghSirvi 的更新答案,你真棒:)
      • 设置数据包回复超时时间。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-10-31
      • 1970-01-01
      • 2021-01-19
      • 2012-10-12
      • 2017-09-14
      • 2018-06-23
      • 1970-01-01
      相关资源
      最近更新 更多