2014-10-30 87 views
1

我正在開發一個允許管理員在RabbitMQ上創建用戶的網站。 我檢查了RabbitMQ api文檔,並找到了一個在windows/unix提示符下創建用戶的例子。命令行作用似乎是這樣的:RabbitMQ以編程方式創建用戶web

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

在提示符下,它工作正常。但在網站上,我不知道如何發送此命令。

說明我正在使用JSF開發Java Web,但歡迎其他任何Web語言示例。

感謝您的任何幫助。

回答

4

經過很長時間在互聯網上搜索,我finnaly發現如何以編程方式在rabbitMQ上創建用戶。基本上,你必須發送一個帶有PUT或POST「狀態」的HHTP請求。由於我在JavaWeb上開發,我可以輕鬆地找到一個Java庫來支持我。我使用的Apache HHTP庫,你可以在這裏找到:

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

所以,我的Java代碼,它的波紋管發佈:

對於庫,進口:

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上。在其他語言遵循相同的邏輯,只是與另一個庫。希望它能幫助我們所有人。對分享知識感到高興!

相關問題