com.rabbitmq.client.AuthenticationFailureException: ACCESS_REFUSED - Login was refused using authent

使用guest用户远程连接RabbitMQ用户名密码错误

使用的是默认用户guest,密码也是guest

代码:

package com.adtec.rabbitmq;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

public class Send {

  private final static String QUEUE_NAME = "hello";

  public static void main(String[] argv) throws Exception {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("192.168.71.111");
    factory.setUsername("guest");
    factory.setPassword("guest");
    factory.setPort(5672);
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();

    channel.queueDeclare(QUEUE_NAME, false, false, false, null);
    String message = "Hello World!";
    channel.basicPublish("", QUEUE_NAME, null, message.getBytes("UTF-8"));
    System.out.println(" [x] Sent '" + message + "'");

    channel.close();
    connection.close();
  }
}

错误信息:

[AMQP Connection 192.168.71.111:5672] WARN com.rabbitmq.client.impl.ForgivingExceptionHandler - An unexpected connection driver error occured (Exception message: Connection reset)
Exception in thread "main" com.rabbitmq.client.AuthenticationFailureException: ACCESS_REFUSED - Login was refused using authentication mechanism PLAIN. For details see the broker logfile.
	at com.rabbitmq.client.impl.AMQConnection.start(AMQConnection.java:342)
	at com.rabbitmq.client.impl.recovery.RecoveryAwareAMQConnectionFactory.newConnection(RecoveryAwareAMQConnectionFactory.java:62)
	at com.rabbitmq.client.impl.recovery.AutorecoveringConnection.init(AutorecoveringConnection.java:99)
	at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:900)
	at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:859)
	at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:817)
	at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:954)
	at com.adtec.rabbitmq.Send.main(Send.java:17)

我在Windows机器上也装了一个RabbitMQ,也用这个用户登录,结果是可以!!!

package com.adtec.rabbitmq;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

public class Send {

  private final static String QUEUE_NAME = "hello";

  public static void main(String[] argv) throws Exception {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("localhost");
    factory.setUsername("guest");
    factory.setPassword("guest");
    factory.setPort(5672);
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();

    channel.queueDeclare(QUEUE_NAME, false, false, false, null);
    String message = "Hello World!";
    channel.basicPublish("", QUEUE_NAME, null, message.getBytes("UTF-8"));
    System.out.println(" [x] Sent '" + message + "'");

    channel.close();
    connection.close();
  }
}

com.rabbitmq.client.AuthenticationFailureException: ACCESS_REFUSED - Login was refused using authent_第1张图片

查了官网才知道

因为guest用户只是被容许从localhost访问。官网文档描述如下:

"guest" user can only connect via localhost

com.rabbitmq.client.AuthenticationFailureException: ACCESS_REFUSED - Login was refused using authent_第2张图片

有兴趣的可以详细了解:http://www.rabbitmq.com/access-control.html

解决办法就是新建用户并授权

新建test用户,授予administrator权限

rabbitmqctl add_user test root
rabbitmqctl set_user_tags test administrator
rabbitmqctl set_permissions -p / test ".*" ".*" ".*"


com.rabbitmq.client.AuthenticationFailureException: ACCESS_REFUSED - Login was refused using authent_第3张图片

再连接:

com.rabbitmq.client.AuthenticationFailureException: ACCESS_REFUSED - Login was refused using authent_第4张图片

好使了!!!

你可能感兴趣的:(rabbitmq)