Wednesday, February 03, 2010

RESTEasy client : authentication

Was just playing with RESTEasy and needed to do authentication against the server.
Came up with this solution:


String userId = "myName";
String password = "verySecret";
 
Credentials credentials = new UsernamePasswordCredentials(userId, password);
HttpClient httpClient = new HttpClient();
httpClient.getState().setCredentials(AuthScope.ANY, credentials);
httpClient.getParams().setAuthenticationPreemptive(true);
 
ClientExecutor clientExecutor = new ApacheHttpClientExecutor(httpClient);
 
try {
URI uri = new URI("http://.....");
ClientRequestFactory fac = new ClientRequestFactory(clientExecutor,uri);
 
ClientRequest request = fac.createRequest("http://.....);

I don't know if there is a better way, as there are a lot of steps to do here. Just thought I should share as documentation is a little sparse here.

2 comments:

joakim said...

Yep, we are doing it about the same way, but using the apache4 client.

Combined this with SSL and You get a pretty good security.

Srinadh Ventrapragada said...

Hello

Can you post sample working example?

Thanks in advance.