3

Problem: I try to make rest call using Resttemplate but it gives 401 status code after that retries once again and gives 200 status code. So every time it makes two calls. So My question is why Resttemplate doesn't use BasicCredentialsProvider in a first call?

Below is my RestTemplate configuration.

@Bean
public CloseableHttpClient httpClient() {

        try {

            BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
            credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("XXXX", "XXXXX"));

        CloseableHttpClient httpclient = HttpClientBuilder
                .create()
                .setDefaultCredentialsProvider(credentialsProvider)
                .setDefaultHeaders(headers)
                .build();
        return httpclient;
    }
    catch (Exception e) {
        throw e;
    }
}

@Bean
public RestTemplate restTemplate(){

     RestTemplate template = new RestTemplate();
     HttpComponentsClientHttpRequestFactory requestFactory = new 
     HttpComponentsClientHttpRequestFactory(httpClient());
     template.setRequestFactory(requestFactory);

     return template ;
}

RestTemplate Call

template.getForObject(...;

So here am I missing something which is not taking credential in the first call?

Below are the httpclient related logs

    2017-04-26 16:28:36.698 DEBUG 6100 --- [nio-8080-exec-1] org.apache.http.headers                  : http-outgoing-0 << HTTP/1.1 401 Unauthorized
    2017-04-26 16:28:36.703 DEBUG 6100 --- [nio-8080-exec-1] o.a.h.i.c.TargetAuthenticationStrategy   : Challenge for Negotiate authentication scheme not available
    2017-04-26 16:28:36.703 DEBUG 6100 --- [nio-8080-exec-1] o.a.h.i.c.TargetAuthenticationStrategy   : Challenge for Kerberos authentication scheme not available
    2017-04-26 16:28:36.703 DEBUG 6100 --- [nio-8080-exec-1] o.a.h.i.c.TargetAuthenticationStrategy   : Challenge for NTLM authentication scheme not available
    2017-04-26 16:28:36.703 DEBUG 6100 --- [nio-8080-exec-1] o.a.h.i.c.TargetAuthenticationStrategy   : Challenge for Digest authentication scheme not available
    2017-04-26 16:28:36.706 DEBUG 6100 --- [nio-8080-exec-1] o.a.http.impl.auth.HttpAuthenticator     : Selected authentication options: [BASIC [complete=true]]
    2017-04-26 16:28:36.706 DEBUG 6100 --- [nio-8080-exec-1] o.a.http.impl.execchain.MainClientExec   : Target auth state: CHALLENGED
    2017-04-26 16:28:36.706 DEBUG 6100 --- [nio-8080-exec-1] o.a.http.impl.auth.HttpAuthenticator     : Generating response to an authentication challenge using basic scheme
    2017-04-26 16:28:36.709 DEBUG 6100 --- [nio-8080-exec-1] o.a.http.impl.execchain.MainClientExec   : Proxy auth state: UNCHALLENGED

I had made the same call with HTTPClient directly and it doesn't have any problem.

DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet getRequest = new HttpGet(url);
getRequest.addHeader("content-type", "application/vnd.nativ.mio.v1+json");

BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("XXXXX", "XXXX"));
httpClient.setCredentialsProvider(credentialsProvider);

HttpResponse response = httpClient.execute(getRequest);
NIrav Modi
  • 6,038
  • 8
  • 32
  • 47
  • can you tell what is your url? is it http or https? and also are you trying to reach a load balancer ? – pvpkiran Apr 26 '17 at 13:20
  • Apparently, this is how HttpClient is designed. See this link for more details http://stackoverflow.com/questions/9539141/httpclient-sends-out-two-requests-when-using-basic-auth – pvpkiran Apr 26 '17 at 13:54
  • Its https URL. No there is no loadbalancer – NIrav Modi Apr 26 '17 at 17:41

0 Answers0