Search code examples
javatomcatbasic-authentication

Tomcat manager connection through HttpClient


Does anyone know what I might be doing wrong here? I added the username and password to the tomcat-user.xml but I am still getting a "401 Unauthorized" error.

String url = "http://localhost:9080/manager/list";
HttpPost httppost = new HttpPost(url);
httppost.setHeader("Authorization", "Basic " + userPass);

log.debug("executing request {}", httppost.getRequestLine());
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();

BufferedReader rd = new BufferedReader(new InputStreamReader(entity.getContent()));
String line;
StringBuffer buffer = new StringBuffer();
while ((line = rd.readLine()) != null) {
buffer.append(line);
buffer.append('\n');
}
rd.close();

Solution

  • The answer depends on whether you're using Tomcat 5.5/6 or Tomcat 7.

    Tomcat 7

    You'll need to access the the listing with the following URL:

    http://localhost:9080/manager/text/list
    

    You'll need to configure tomcat-user.xml with a user that has the manager-script role.

    Tomcat 5.5/6

    You'll need to access the the listing with the following URL:

    http://localhost:9080/manager/list
    

    You'll need to configure tomcat-user.xml with a user that has the manager role.

    Update

    You may not be performing the Basic Auth step correctly. The username/pass might need to be base64 encoded. See the highest voted answer in this question: Http Basic Authentication in Java using HttpClient?