RabbitMQ 以编程方式创建用户 web [英] RabbitMQ create user programmatically web
问题描述
我正在开发一个允许管理员在 RabbitMQ 上创建用户的网站.我检查了 RabbitMQ api doc,并找到了一个在 windows/unix 提示符下创建用户的示例.命令行如下所示:
I'm developing a web site which allows the administrator to create users on RabbitMQ. I checked the RabbitMQ api doc, and found an example to create user on windows/unix prompt. The command line seens like this:
curl -i -u ADMIN_USER: ADMIN_PASSW -H "content-type:application/json" -XPUT
{"password":"PASSW_WRITTEN","tags":"administrator"}
http://IP_ADRESS:PORT/api/users/USER_NAME
这里是文档的链接
http://hg.rabbitmq.com/rabbitmq-management/raw-file/rabbitmq_v3_4_1/priv/www/api/index.html
在提示中,它工作正常.但是在一个网站上,我不知道如何发送这个命令.
In the prompt, it works fine. But on a web site, I have no idea how to send this command.
注意我正在使用 JSF 在 Java Web 上进行开发,但欢迎使用任何其他 Web 语言示例.
Note I'm developping on Java Web with JSF, but any other web language example is welcome.
感谢您的帮助.
推荐答案
在网上找了很久,终于找到了在RabbitMQ上编程创建用户的方法.基本上,您必须发送带有 PUT 或 POST状态"的 HTTP 请求.因为我是在 Java Web 上开发的,所以我可以很容易地找到一个 Java 库来支持我.我使用了 Apache HTTP 库,你可以在这里找到它:
After a long time searching on the internet, I finally found how to create users on RabbitMQ programmatically. Basically, you have to send a HTTP request with PUT or POST "status". Since I'm developing on Java Web, I could easily find a Java library to support me. I used Apache HTTP library, you can find it here:
http://hc.apache.org/downloads.cgi
所以,我的 Java 代码发布在下面:
So, my Java code it's posted below:
对于库,导入:
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.commons.codec.binary.Base64;
创建新用户的代码:
// First, save your user/pw with permission to create new users.
// NOTE: this user is already created on RabbitMQ with permission to create new users
String enc = new String( Base64.encodeBase64( "USER_NAME_WITH_PERMISSION:PASS_W".getBytes() ) );
try{
HttpPut putCriaUsuario = new HttpPut( "http://RABBIT_MQ_IP:PORT/api/users/USER_NAME_TO_CREATE );
putCriaUsuario.addHeader( "Authorization", "Basic " + enc ); // RabbitMQ requires a user with create permission, create it mannually first
putCriaUsuario.addHeader( "content-type", "application/json" );
putCriaUsuario.setEntity( new StringEntity( "{\"password\":\"YOUR_PASS_WORD\",\"tags\":\"none\"}" ) );
client.execute( putCriaUsuario );
//After create, configure RabbitMQ permissions
HttpPut putConfiguraPermissoes = new HttpPut( "http://RABBIT_MQ_IP:PORT/api/permissions/QUEUE_NAME/USER_NAME_CREATED" );
putConfiguraPermissoes.addHeader( "Authorization", "Basic " + enc );
putConfiguraPermissoes.addHeader( "content-type", "application/json" );
putConfiguraPermissoes.setEntity( new StringEntity( "{\"configure\":\"^$\",\"write\":\".*\",\"read\":\".*\"}" ) ); // Permission you wanna. Check RabbitMQ HTTP API for details
client.execute( putConfiguraPermissoes );
}catch( UnsupportedEncodingException ex ){
ex.printStackTrace();
}catch( IOException ex ){
ex.printStackTrace();
}
这是 Java,因此可以在桌面应用程序或 Java Web 上使用.在其他语言上遵循相同的逻辑,只是使用另一个库.希望对我们大家有帮助.乐于分享知识!
This is Java, so it can be used on desktop application or Java Web. On other language follows the same logic, just with another libs. Hope it help us all. Fells happy for sharing knowledge!
这篇关于RabbitMQ 以编程方式创建用户 web的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!