Jersey Client API - authentication

cdugga picture cdugga · Jul 21, 2011 · Viewed 78.5k times · Source

I'm using the Jersey client API to submit SOAP requests to a JAX-WS webservice. By default Jersey is somehow using my Windows Nt credentials for authentication when challenged. Can anyone explain where Jersey does this in the code? And can it be overriden?

I have tried using HTTPBasicAuthFilter and adding as a filter on the Client. I have also tried adding my credentials to the WebResoruce queryParams field however neither are being picked up.

Answer

iain picture iain · Dec 9, 2011

At first I got this working as documented in the Jersey User guide

Authenticator.setDefault (authinstance);

However I did not like this as it relied on setting a global authenticator. After some research I discovered that Jersey has a HTTPBasicAuthFilter which is even easier to use.

Client c = Client.create();
c.addFilter(new HTTPBasicAuthFilter(user, password));

See: https://jersey.github.io/nonav/apidocs/1.10/jersey/com/sun/jersey/api/client/filter/HTTPBasicAuthFilter.html https://jersey.github.io/nonav/apidocs/1.10/jersey/com/sun/jersey/api/client/filter/Filterable.html#addFilter(com.sun.jersey.api.client.filter.ClientFilter)