RabbitMQ以编程方式创建用户web

时间:2014-10-30 12:55:21

标签: rabbitmq java-web-start

我正在开发一个允许管理员在RabbitMQ上创建用户的网站。 我查看了RabbitMQ api文档,并找到了一个在windows / unix提示符下创建用户的示例。命令行看起来像这样:

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

以下是doc

的链接

http://hg.rabbitmq.com/rabbitmq-management/raw-file/rabbitmq_v3_4_1/priv/www/api/index.html

在提示中,它工作正常。但是在网站上,我不知道如何发送这个命令。

注意我正在使用JSF在Java Web上开发,但欢迎任何其他Web语言示例。

感谢您的帮助。

2 个答案:

答案 0 :(得分:4)

经过长时间的互联网搜索,我终于找到了如何以编程方式在rabbitMQ上创建用户。基本上,您必须发送带有PUT或POST“status”的HHTP请求。由于我正在开发JavaWeb,我可以轻松找到一个Java库来支持我。我使用了Apache HHTP库,你可以在这里找到它:

http://hc.apache.org/downloads.cgi

所以,我的Java代码发布在下面:

对于libs,导入:

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 alredy 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 permission

  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上使用。在其他语言上遵循相同的逻辑,只是与另一个库。希望它能帮助我们所有人。很高兴分享知识!

答案 1 :(得分:0)

对于那些在授予权限方面遇到问题的人来说,这个对我有用

HttpPut putConfiguraPermissoes = new HttpPut("http://127.0.0.1:15672/api/permissions/%2F/THE_NAME_OF_THE_NEW_USER" );  
putConfiguraPermissoes.addHeader( "Authorization", "Basic " + enc );
putConfiguraPermissoes.addHeader( "content-type", "application/json" );
putConfiguraPermissoes.setEntity( new StringEntity( "{\"configure\":\".*\",\"write\":\".*\",\"read\":\".*\"}" ) );
CloseableHttpClient client2 = HttpClients.createDefault();
lient2.execute( putConfiguraPermissoes );