I'm trying to access to RabbitMQ rest, but I got 401 unauthorized error. I want to access to queue information and to get messages number.
I found this as a solution
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);
but it doesn't work. I saw that DefaultHttpClient class is depreciated so I tried something like this
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);
and then to access to REST thought WebTarget, something like this
WebTarget queueREST = RESTClientManager.getClient().target("xx.xx.xx.xx:15672/api/queues/%2F/queue-name");
but I still get error 401 Unauthorized. Any suggestion how to solve this problem?
"guest" user can only connect via localhost. To allow remote connections using guest change rabbitmq.config
and add [{rabbit, [{loopback_users, []}]}].
[source: https://www.rabbitmq.com/access-control.html]
In my case, I fix it with:
sudo sed -i 's/{default_pass, <<"guest">>}$/{default_pass, <<"guest">>},\n {loopback_users, []}/' /etc/rabbitmq/rabbitmq.config