Search code examples
javahttpconfluenceapache-httpcomponentsconfluence-rest-api

Java 8: Apache HttpClient failing authentication


I am attempting to use Apache HttpClient API to access Atlassian Confluence wiki pages.

Here is my code:

public class ConcfluenceTest{

    public static void main(String[] args) {
        String pageID = "107544635";
        String hostName = "valid_hostname";
        String hostScheme = "https";
        String username = "verified_username";
        String password = "verified_password";
        int port = 443;

        //set up the username/password authentication
        BasicCredentialsProvider credsProvider = new BasicCredentialsProvider();
        credsProvider.setCredentials(
            new AuthScope(hostName, port, AuthScope.ANY_REALM, hostScheme),
            new UsernamePasswordCredentials(username, password));

        HttpClient client = HttpClientBuilder.create()
            .setDefaultCredentialsProvider(credsProvider)
            .build();

        try {

            HttpGet getRequest = new HttpGet("valid_url");
            System.out.println(getRequest.toString());

            HttpResponse response = client.execute(getRequest);

            //Parse the response
            BufferedReader rd = new BufferedReader(
                new     InputStreamReader(response.getEntity().getContent()));
            StringBuffer result = new StringBuffer();
            String line = "";
            while ((line = rd.readLine()) != null) {
                result.append(line);
            }

            System.out.println(result.toString());

        } catch (UnsupportedEncodingException e) {
            System.out.println(e.getStackTrace());
        } catch (IOException e) {
            System.out.println(e.getStackTrace());
        }

    }
}

When I attempt to execute this code, the printed response is the HTML of the Log In screen, which means that the authentication failed. This code does, however, return the correct response when I provide it with the URL to a page that is not restricted to registered users (i.e credentials aren't required). I also tried all permutations of port/scheme.

Can someone tell me what I am missing?


Solution

  • Afaik, if http-basic-auth is supported, something like

    user:password@server:port/path 
    

    should work, too. You could see if that works with a browser.

    If Confluence dosen't support basic auth, use firebug to find out the action of the login-form (eg. the path, something like /dologin.action), the method (POST) and the Names of the user/password fields.

    With that information you can create a request like this:

    HttpPost httpPost = new HttpPost(fullFormActionUrlWithServerAndPort);
    List <NameValuePair> nvp = new ArrayList <NameValuePair>();
    nvp.add(new BasicNameValuePair("name-of-the-user-field", "your-user-name"));
    nvp.add(new BasicNameValuePair("name-of-the-pass-field", "your-password"));
    httpPost.setEntity(new UrlEncodedFormEntity(nvp));