[rabbitmq-discuss] question about rabbitmq http api basic auth

Sungwon Jung thedaz at gmail.com
Wed May 15 03:32:24 BST 2013


Hello. I have a question about rabbitmq http api basic auth.

I use rabbitmq v3.1.0 and my code is below.

--
package com.foo.bar.controller;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URLEncoder;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.AuthCache;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.protocol.ClientContext;
import org.apache.http.impl.auth.BasicScheme;
import org.apache.http.impl.client.BasicAuthCache;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HTTP;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class HealthCheckController {
private Log log = LogFactory.getLog(HealthCheckController.class);
 private final String IP = "000.000.000.000";
private final String OK = "ok";
 private String ID = "account";
private String PW = "passwd";

@RequestMapping({"/monitor/check"})
public void check(HttpServletRequest request, HttpServletResponse response) 
throws IOException {
        DefaultHttpClient httpclient = new DefaultHttpClient();
        HttpHost targetHost = new HttpHost(IP, 15672, "http");
        
        HttpPut rq = new HttpPut("/api/aliveness-test/%2ffoo%2fbar");
        
        try {
            httpclient.getCredentialsProvider().setCredentials(
                    new AuthScope(targetHost.getHostName(), 
targetHost.getPort()),
                    new UsernamePasswordCredentials(ID, 
URLEncoder.encode(PW, HTTP.UTF_8)));

            AuthCache authCache = new BasicAuthCache();
            BasicScheme basicAuth = new BasicScheme();
            authCache.put(targetHost, basicAuth);
            BasicHttpContext localcontext = new BasicHttpContext();
            localcontext.setAttribute(ClientContext.AUTH_CACHE, authCache);

            HttpResponse rp = httpclient.execute(targetHost, rq, 
localcontext);
            
            log.info("===================");
            log.info(rp);
            BufferedReader rd = new BufferedReader(new 
InputStreamReader(rp.getEntity().getContent()));
      String line = "";
            while ((line = rd.readLine()) != null) {
            log.info(line);
            } 
            log.info("===================");
        } finally {
            httpclient.getConnectionManager().shutdown();
        }
}
}
--

I call '/monitor/check' from web browser.
but...

11:21:08 [WARN ](DefaultRequestDirector.java:1095) Authentication error: 
basic authorization challenge expected, but not found
11:21:08 [INFO ](HealthCheckController.java:103) ===================
11:21:08 [INFO ](HealthCheckController.java:104) HTTP/1.1 401 Unauthorized 
[Server: MochiWeb/1.1 WebMachine/1.9.0 (someone had painted it blue), Date: 
Wed, 15 May 2013 02:21:08 GMT
, Content-Length: 50]
11:21:08 [INFO ](HealthCheckController.java:108) 
{"error":"not_authorised","reason":"Login failed"}
11:21:08 [INFO ](HealthCheckController.java:110) ===================


account, password, ip and api are all correct. but I cannot authorized.

What's wrong in my code?

Thank you.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.rabbitmq.com/pipermail/rabbitmq-discuss/attachments/20130514/dd36d031/attachment.htm>


More information about the rabbitmq-discuss mailing list