rabbitmq ACCESS_REFUSED -使用身份验证机制PLAIN拒绝登录

zbq4xfa0  于 2023-06-23  发布在  RabbitMQ
关注(0)|答案(3)|浏览(244)

我已经在我的windows7机器上安装了“erlang”和“rabbitmq”。但是当我尝试运行这段代码时,我得到了一个异常。

package com.rabbitmq;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
public class SendMessage {
  private final static String QUEUE_NAME = "hello";
  public static void main(String[] argv) throws Exception { 
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("localhost");
    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();
  }
}

我得到了这个例外。
线程"main"出现异常com. rabbitmq. client. AuthenticationFailureException:ACCESS_REFUSED-使用身份验证机制PLAIN拒绝登录。有关详细信息,请参阅代理日志文件。
这是日志:
2016年4月11日::12:45:06 ===添加vhost 'localhost'
=信息报告==== 2016年4月11日::14:08:52 ===接受AMQP连接(127.0.0.1:55327-> 127.0.0.1:5672)<0.360.0> (127.0.0.1:55327 -> 127.0.0.1:5672)
=错误报告==== 2016年4月11日::14:08:52 === AMQP连接错误(127.0.0.1:55327-> 127.0.0.1:5672,状态:<0.360.0>开始): starting):
=信息报告==== 2016年4月11日::14:08:52 ===关闭AMQP连接(127.0.0.1:55327-> 127.0.0.1:5672)<0.360.0> (127.0.0.1:55327 -> 127.0.0.1:5672)
当我尝试列出用户时,我没有得到任何现有用户,add_user也无法在cmd link中工作

de90aj5v

de90aj5v1#

在您的ConnectionFactory中,您需要设置您的用户名和密码,如果您已经创建了任何用户名和密码,或者您可以使用默认用户“guest”和密码“guest”,只能从localhost访问。

myzjeezk

myzjeezk2#

您可以创建新用户(userA)和密码(userA123)。
并设置

factory.setHost("your_pc_ip");
factory.setUsername("userA");
factory.setPassword("userA123");

在发送者和接收者类中。

zynd9foi

zynd9foi3#

在严格遵循官方helloworld指南的同时遇到这个错误消息有点令人烦恼。

通过一些研究,以下是对我有效的方法:

ConnectionFactory factory = new ConnectionFactory();
factory.setHost("10.0.0.2");   // * default rabbitmq port: 5672
factory.setUsername("myUsr");  // ensure a new user is created on the server
factory.setPassword("p@ssword");
factory.setVirtualHost("vh1"); // ensure a new virtual host is created on the server
                               // and the user has granted the access right to it

try (Connection connection = factory.newConnection();
     Channel channel = connection.createChannel()) {

    // create a queue
    String queueName = "myQueueName";
    Boolean durable = false;    // if ture, messages will be retained even after server is restarted
    Boolean exclusive = false;  // if true, other connections will not be able to access this queue
    Boolean autoDelete = false; // if true, queue is deleted when all consumers have finished using it
    Map<String, Object> arguments = null;
    channel.queueDeclare(queueName, durable, exclusive, autoDelete, arguments);

    String message = "Hello World!";
    channel.basicPublish("", queueName, null, message.getBytes());
    System.out.println(" [x] Sent '" + message + "'");
}

在Web管理界面添加新虚拟主机:

从web管理界面添加新用户:

授予用户虚拟主机访问权限:

(点击用户名进入详情页)

相关问题