RESTEasy client framework authentication credentials

jnorris picture jnorris · Dec 11, 2009 · Viewed 14.2k times · Source

RESTEasy (a JAX-RS implementation) has a nice client framework, eg:

ServiceApi client = ProxyFactory.create(ServiceApi.class, baseUri);

How do you provide HTTP authentication credentials to this client?

Answer

Z. Cass picture Z. Cass · Apr 19, 2012

jnorris's answer uses some deprecated classes. Here is an updated way that uses non-deprecated classes.

    import org.apache.http.HttpStatus;
    import org.apache.http.auth.Credentials;
    import org.apache.http.auth.UsernamePasswordCredentials;
    import org.apache.http.impl.client.DefaultHttpClient;
    ...
    DefaultHttpClient httpClient = new DefaultHttpClient();

    Credentials credentials = new UsernamePasswordCredentials(userName,
            password);
    httpClient.getCredentialsProvider().setCredentials(
            org.apache.http.auth.AuthScope.ANY, credentials);

    ClientExecutor clientExecutor = new ApacheHttpClient4Executor(
            httpClient);
    proxy = ProxyFactory
            .create(UserAccessProxy.class, host, clientExecutor);