Reputation: 43
So I am working under a corporate proxy which uses NTLM Authentication. The authentication is detected properly and I can access different APIs I need to access through proxy.
Problem comes when I try to access an API endpoint that requires Basic Auth (GET request). Whenever I try to hit it through my code, I get a 401 unauthorized, and I can see in the headers I get the "WWW-Authenticate: Basic realm="Realm".
I have tried various different methods, but the end result is always the same.
I have tried setting the username and password directly in the URI:
HttpGet request = new HttpGet(https://username:[email protected]/end/point)
I have tried using a Basic Credentials Provider:
CredentialsProvider provider = new BasicCredentialsProvider();
provider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password));
CloseableHttpClient httpClient = HttpClientBuilder.create().setDefaultCredentialsProvider(provider).build();
I have tried adding manually the Base64 Authorization header in the request:
request.addHeader("Authorization", "Basic .........................");
Also, please note that I can hit this endpoint through the proxy using PostMan and even the web browser.
EDIT
I have noticed that actually if I run the HttpRequest twice consecutively (with same SessionID and so) the first one will fail with 401, but the second request will actually be successful. Any idea why this may be happening? I'm thinking maybe the proxy is not forwarding the basic auth...
I have tried more things, but at this point all have been futile.
Any help would be greatly appreciated! :)
Upvotes: 1
Views: 909
Reputation: 925
May be you could try out setting the proxy additionally as shown in the example below from the link below :-
https://www.tutorialspoint.com/apache_httpclient/apache_httpclient_proxy_authentication.htm
public class ProxyAuthenticationExample {
public static void main(String[] args) throws Exception {
//Creating the CredentialsProvider object
CredentialsProvider credsProvider = new BasicCredentialsProvider();
//Setting the credentials
credsProvider.setCredentials(new AuthScope("example.com", 80),
new UsernamePasswordCredentials("user", "mypass"));
credsProvider.setCredentials(new AuthScope("localhost", 8000),
new UsernamePasswordCredentials("abc", "passwd"));
//Creating the HttpClientBuilder
HttpClientBuilder clientbuilder = HttpClients.custom();
//Setting the credentials
clientbuilder = clientbuilder.setDefaultCredentialsProvider(credsProvider);
//Building the CloseableHttpClient object
CloseableHttpClient httpclient = clientbuilder.build();
//Create the target and proxy hosts
HttpHost targetHost = new HttpHost("example.com", 80, "http");
HttpHost proxyHost = new HttpHost("localhost", 8000, "http");
//Setting the proxy
RequestConfig.Builder reqconfigconbuilder= RequestConfig.custom();
reqconfigconbuilder = reqconfigconbuilder.setProxy(proxyHost);
RequestConfig config = reqconfigconbuilder.build();
//Create the HttpGet request object
HttpGet httpget = new HttpGet("/");
//Setting the config to the request
httpget.setConfig(config);
//Printing the status line
HttpResponse response = httpclient.execute(targetHost, httpget);
System.out.println(response.getStatusLine());
} }
Upvotes: 1