Spring Resttetelate with BasicCredentialsProvider返回401 unathorized

时间:2017-04-26 12:25:57

标签: spring authentication spring-boot resttemplate

问题: 我尝试使用Resttemplate进行休息调用,但在再次重试之后它会给出401状态代码并提供200个状态代码。所以每次打两次电话。 所以我的问题是为什么Resttemplate在第一次调用中没有使用BasicCredentialsProvider?

以下是我的RestTemplate配置。

@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(...;

所以我在这里错过了第一次通话中没有取证的东西吗?

以下是httpclient相关日志

    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

我直接与 HTTPClient 进行了相同的调用,但没有任何问题。

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);

0 个答案:

没有答案