RabbitMQ控制台guest用户登录失败

今天前端反映rabbitmq的web控制台使用默认账户guest登录失败——login failed。在查阅了rabbitmq官网文档以及其他资料后找到了该问题的原因,遂总结如下。

1.问题背景

首先,rabbitmq的默认账户和密码都是guest,安装完rabbitmq之后可以使用guest/guest登录。

而通过web控制台登录有以下两种形式:

  • 一种是本地登陆(即在rabbitmq安装的机器上登陆——地址栏输入http://localhost:15672);
  • 另一种是远程登陆(即在其他机器上通过指定IP地址登陆——地址栏输入http://<ip>:15672)。

但是,在rabbitmq3.3.0之后,出于安全性考虑,默认情况下rabbitmq的guest/guest账户将不能实现远程登陆,只能在本地登陆。

2.解决方法

在上面的问题背景下,若想实现成功的远程登陆到rabbitmq控制台上有以下3种方法。

(1)新建一个用户

默认的guest账户无法远程登陆,并不意味着其他用户也无法远程登陆。所以,可以通过如下命令新增用户、设定用户角色以及赋予用户权限实现远程登陆:

1)新增用户
$ rabbitmqctl add_user <用户名> <密码>

2)设定用户administrator角色
$ rabbitmqctl set_user_tags <用户名> administrator

用户角色可以分为超级管理员administrator、监控者monitoring、策略制定者policymaker、普通管理者management等。

3)赋予用户权限
$ rabbitmqctl set_permission -p / <用户名> “.*” “.*” “.*”

用户权限包括配置权限、读权限和写权限。配置权限会影响到exchange、queue的声明和删除。读写权限会影响到从queue里取消息、向exchange发送消息以及queue和exchange的绑定操作。
比如,将queue绑定到某个exchange上,需要具有queue的写权限以及exchange的读权限;向exchange发送消息需要具有exchange的写权限;从queue里取消息需要具有queue的读权限。

此时,就可以通过你添加的新用户/密码来进行rabbitmq的远程登陆了。然而,这个方法并没有解决guest无法登陆的本质问题。

(2)修改rabbitmq.config文件

查阅rabbitmq官方文档(http://www.rabbitmq.com/configure.html),其中有一页篇幅专门介绍了rabbitmq的配置,包括环境变量的设置以及配置文件rabbitmq.config的配置等。

对于rabbitmq.config配置文件,我在服务器上使用find命令并没有搜索到。所以让我疑惑的一点是该配置文件rabbitmq.config是否是默认不启动的、不存在的,有待研究。

对此,我在官网文档上找到这么一段话:

If rabbitmq.config doesn’t exist, it can be created manually. Set the RABBITMQ_CONFIG_FILEenvironment variable if you change the location. The Erlang runtime automatically appends the .config extension to the value of this variable.

如果rabbitmq.cinfig文件不存在,那么可以手动创建它…

继续往下阅读,还有这么一段话:

Example rabbitmq.config File

RabbitMQ server source repository contains an example configuration file named rabbitmq.config.example. This example file contains an example of most of the configuration items you might want to set (with some very obscure ones omitted) along with documentation for those settings. All configuration items are commented out in the example, so you can uncomment what you need. Note that the example file is meant to be used as, well, example, and should not be treated as a general recommendation.

In most distributions we place this example file in the same location as the real file should be placed (see above). However, for the Debian and RPM distributions policy forbids doing so; instead you can find it in /usr/share/doc/rabbitmq-server/ or /usr/share/doc/rabbitmq-server-3.6.10/ respectively.

是说有这么一个配置文件的样本——rabbitmq.config.example,里面包含了所有可配置的条目,我们可以根据自己的需要,将相应的条目的注释去掉,使其配置项生效。而该配置文件样本rabbitmq.config.example可以在上面红色标记的路径下找到。

对此,我们需要做的是先将该rabbitmq.config.example文件拷贝到rabbitmq配置文件路径下(比如配置文件路径是/etc/rabbitmq/rabbitmq.config,我将其拷贝到/etc/rabbitmq/目录下,并重命名为rabbitmq.config)。

附rabbitmq.config.example文件部分截取内容:

然后修改相应的配置项,至于各个配置项所代表的含义是什么,官网文档也给出了不错的解释,其中有这么一段:

loopback_users List of users which are only permitted to connect to the broker via a loopback interface (i.e. localhost).If you wish to allow the default guest user to connect remotely, you need to change this to [].

Default: [<<“guest”>>]

loopback_users配置项,指出该配置项下的用户列表将只能通过本地localhost登陆,若想解除该限制,则将[<<“guest”>>]修改为 []即可。

因此,在配置文件rabbitmq.config中,找到该配置项做出相应的修改就可以实现guest的远程登陆(注意,配置文件中的配置项都被用%%符号注释掉了,若想使其生效,必须将%%删掉,并且配置项最后的一个逗号也要删掉!

这是第二种方法,第三种方法与之类似:
配置文件中的loopback_users配置项有两个,上面是其一,对另一个的修改更加简单,直接将注释%%和逗号去掉即可,两种方法本质是一样的。每个配置项上方都有相应的配置说明,这里就不详说了。

修改了配置项之后,必须重启rabbitmq才能使之生效:

关闭rabbitmq:
$ service rabbitmq-server stop

启动rabbitmq:
$ service rabbitmq-server start

重启rabbitmq之后,回到web控制台,使用guest/guest进行登录,登录成功。

总结:这次出现的问题在中文搜索引擎上搜索了很久,却没有得到满意的答案。可见这是一个小众的问题,讨论类似问题的人并不多。所以,以后遇到问题,应该多看官方文档。

《RabbitMQ控制台guest用户登录失败》有1个想法

发表评论

电子邮件地址不会被公开。 必填项已用*标注