我正在尝试访问RabbitMQ休息,但我有401个未经授权的错误。我想访问队列信息并获取消息号。
我发现这是一个解决方案
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpHost targetHost = new HttpHost("xx.xx.xx.xx", 15672, "http");
HttpPut request = new HttpPut(
"/api/queues/%2F/queue-name");
httpClient.getCredentialsProvider().setCredentials(
new AuthScope(targetHost.getHostName(), targetHost.getPort()),
new UsernamePasswordCredentials("guest", "guest"));
AuthCache authCache = new BasicAuthCache();
BasicScheme basicAuth = new BasicScheme();
authCache.put(targetHost, basicAuth);
BasicHttpContext localcontext = new BasicHttpContext();
localcontext.setAttribute(ClientContext.AUTH_CACHE, authCache);
request.addHeader("Content-Type", "application/json");
StringEntity input = new StringEntity(
"{\"vhost\":\"/\",\"durable\":\"false\",\"auto_delete\":\"false\",\"arguments\":{}}");
request.setEntity(input);
HttpResponse response = httpClient.execute(targetHost, request, localcontext);
但它不起作用。我看到DefaultHttpClient类被折旧,所以我尝试了类似这样的东西
HttpHost targetHost = new HttpHost("xx.xx.xx.xx", 15672, "http");
HttpPut request = new HttpPut("/api/whoami");
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
CredentialsProvider credentialProvider = new BasicCredentialsProvider();
credentialProvider.setCredentials(
new AuthScope(targetHost.getHostName(), targetHost.getPort()),
new UsernamePasswordCredentials("guest","guest")
);
AuthCache authCache = new BasicAuthCache();
BasicScheme basicAuth = new BasicScheme();
authCache.put(targetHost, basicAuth);
HttpClientContext context = HttpClientContext.create();
context.setCredentialsProvider(credentialProvider);
context.setAuthCache(authCache);
request.addHeader("Content-Type", "application/json");
HttpResponse response = httpClient.execute(targetHost, request, context);
然后访问REST思想WebTarget,类似这样的
WebTarget queueREST = RESTClientManager.getClient().target("xx.xx.xx.xx:15672/api/queues/%2F/queue-name");
但我仍然得到错误401未经授权。有任何建议如何解决这个问题?
答案 0 :(得分:2)
“guest”用户只能通过localhost连接。允许使用访客更改Cannot GET '/'
进行远程连接并添加rabbitmq.config
[来源:https://www.rabbitmq.com/access-control.html]
在我的情况下,我用以下方法解决:
[{rabbit, [{loopback_users, []}]}].
答案 1 :(得分:0)
我使用此https://github.com/rabbitmq/hop找到了解决方案。
答案 2 :(得分:0)
您可以使用更完整的my rabbitmq-management-java-client 库。
例如,用于验证和列出队列的代码段:
RabbitManagementApi api = RabbitManagementApi.newInstance("http://localhost:15672/" , "user" , "password");
List<Queue> queues = api.listQueues("vhost");