Jersey Client basic authentication dialogue suppress

704 Views Asked by At

I am using Jeresy Client Api to communicate with Rest services. I want to handle 401 response from the server. Whenever server gives 401 Jersey present its own authentication dialog like browser for username and password.

client.addFilter(new com.sun.jersey.api.client.filter.HTTPBasicAuthFilter(username, password));

Is there a way to hide or suppress this dialgue and present my custom dialgue.

1

There are 1 best solutions below

0
On

I have seen this kind of issue before when I was developing an Eclipse plug-in that used the Jersey Client API to make HTTP requests to a RESTful web service. Every time the web service responded with a 401 HTTP status code, Eclipse would automatically display a standard authentication dialog that asked the user to enter their username and password.

I was able to suppress this unwanted authentication dialog by removing the default Authenticator instance. You can do this using the following code snippet:

Authenticator.setDefault(null);

If you do this from your application's initialisation stage, before you use the Jersey Client API to make any HTTP requests, it should prevent the default authentication dialog from being displayed if a 401 HTTP response occurs.

Please refer to the Authenticator documentation for further details.

If you want to display your own custom authentication dialog instead of the default dialog, you can implement a custom sub-class of Authenticator that displays the desired dialog. You can then register it within your application by using Authenticator.setDefault(customAuthenticator);