首页 > 代码库 > Rabbitmq新增用户角色

Rabbitmq新增用户角色

首先,查看一下当前有哪些用户:

$ /usr/local/sbin/rabbitmqctl list_users
Listing users ...
patty    [administrator]
guest    [administrator]

然后,我们新增一个用户wgm并再次查看新增后的用户列表:

$ /usr/local/sbin/rabbitmqctl add_user wgm 123456
$ /usr/local/sbin/rabbitmqctl list_users
Listing users ...
patty    [administrator]
wgm    []
guest    [administrator]

由于此时还未对wgm授予角色, wgm的角色一栏结果还是[],现在, 我们授予wgm administrator角色

$ /usr/local/sbin/rabbitmqctl set_user_tags wgm administrator
Setting tags for user "wgm" to [administrator] ...

$
/usr/local/sbin/rabbitmqctl list_users Listing users ... patty [administrator] wgm [administrator] guest [administrator]

接下来, 查看我们当前在哪个virtual host上,以及该virtual host上所有用户所拥有的权限:

$ /usr/local/sbin/rabbitmqctl list_vhosts
Listing vhosts ...
/

$
/usr/local/sbin/rabbitmqctl list_permissions -p / Listing permissions in vhost "/" ... guest .* .* .* patty .* .* .*

可见当前vhost上有默认用户guest,以及之前通过rabbitmqadmin管理界面添加的用户patty, 他们拥有该vhost所有的配置、读写权限。此时并没有显示wgm,是因为我们还未对他进行赋权,当然,我们也可以单独查看一个用户的权限信息:

$ /usr/local/sbin/rabbitmqctl list_user_permissions wgm
Listing permissions for user "wgm" ...

现在,对wgm这个用户进行授权,赋予其在"/"这个vhost上所有的配置、读写权限。

$ /usr/local/sbin/rabbitmqctl set_permissions -p / wgm ".*" ".*" ".*" 
Setting permissions for user "wgm" in vhost "/" ...

$ /usr/local/sbin/rabbitmqctl list_user_permissions wgm
Listing permissions for user "wgm" ...
/    .*    .*    .*

set_permissions命令格式为:

set_permissions [-p vhost] {user} {conf} {write} {read}, conf对应配置权限, write写权限, read读权限

现在,再查看“/”这个vhost的权限信息,会发现多了一个“wgm”

$ /usr/local/sbin/rabbitmqctl list_permissions -p / 
Listing permissions in vhost "/" ...
guest    .*    .*    .*
wgm    .*    .*    .*
patty    .*    .*    .*

 接下来, 我们通过Python程序验证,使用用户wgm来发送消息信息,然后guest用户去接收消息。

#!/usr/bin/env python
# encoding: utf-8

"""
@version: 1.0
@author: Gaomin Wu
@contact: pattywgm@163.com
@file: send.py
@time: 17/7/11  下午3:47
@desc: 消息发送方
涉及基本的消息发送,不对消息进行确认
"""

import pika

# 远程访问,用户认证
credential = pika.PlainCredentials(username="wgm", password="123456")
connection = pika.BlockingConnection(pika.ConnectionParameters(host="localhost", credentials=credential))

channel = connection.channel()

# 消息的发送者和消费者都可以声明queue, 多次声明只有一次会被创建
# 如果消息发送到一个不存在的queue,将被丢弃,因此,安全起见,最好是在双方都声明一次
channel.queue_declare("hello")

# routing_key指定exchange关联到hello这个queue
channel.basic_publish(exchange="", routing_key="hello", body="Hello Patty!!!")

print(" [x] Sent ‘Hello World!‘")

connection.close()
#!/usr/bin/env python
# encoding: utf-8

"""
@version: 1.0
@author: Gaomin Wu
@contact: pattywgm@163.com
@file: receive.py
@time: 17/7/11  下午3:47
@desc: 消息接收方
"""
import pika

connection = pika.BlockingConnection(pika.ConnectionParameters(host="localhost"))

channel = connection.channel()

channel.queue_declare("hello")

# 消息回调处理函数
def callback(ch, method, properties, body):
    print(" [x] Received %r" % body)


channel.basic_consume(callback,
                      queue=hello,
                      no_ack=True)

print( [*] Waiting for messages. To exit press CTRL+C)
channel.start_consuming()

 

Rabbitmq新增用户角色