Merge branch 'khatwaniNikhil-BAEL-248_Update_deprecated_API'
This commit is contained in:
commit
8ed8d686f7
|
@ -342,8 +342,8 @@
|
|||
|
||||
<properties>
|
||||
<!-- Spring -->
|
||||
<org.springframework.version>4.3.4.RELEASE</org.springframework.version>
|
||||
<org.springframework.security.version>4.2.0.RELEASE</org.springframework.security.version>
|
||||
<org.springframework.version>4.3.6.RELEASE</org.springframework.version>
|
||||
<org.springframework.security.version>4.2.1.RELEASE</org.springframework.security.version>
|
||||
|
||||
<!-- persistence -->
|
||||
<hibernate.version>5.2.5.Final</hibernate.version>
|
||||
|
@ -351,7 +351,7 @@
|
|||
|
||||
<!-- http -->
|
||||
<httpcore.version>4.4.5</httpcore.version>
|
||||
<httpclient.version>4.5.2</httpclient.version>
|
||||
<httpclient.version>4.5.3</httpclient.version>
|
||||
|
||||
<!-- logging -->
|
||||
<org.slf4j.version>1.7.21</org.slf4j.version>
|
||||
|
|
|
@ -0,0 +1,39 @@
|
|||
package org.baeldung.client;
|
||||
|
||||
import java.net.URI;
|
||||
|
||||
import org.apache.http.HttpHost;
|
||||
import org.apache.http.client.AuthCache;
|
||||
import org.apache.http.client.protocol.HttpClientContext;
|
||||
import org.apache.http.impl.auth.BasicScheme;
|
||||
import org.apache.http.impl.client.BasicAuthCache;
|
||||
import org.apache.http.protocol.BasicHttpContext;
|
||||
import org.apache.http.protocol.HttpContext;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
|
||||
|
||||
public class HttpComponentsClientHttpRequestFactoryBasicAuth extends HttpComponentsClientHttpRequestFactory {
|
||||
|
||||
HttpHost host;
|
||||
|
||||
public HttpComponentsClientHttpRequestFactoryBasicAuth(HttpHost host) {
|
||||
super();
|
||||
this.host = host;
|
||||
}
|
||||
|
||||
protected HttpContext createHttpContext(HttpMethod httpMethod, URI uri) {
|
||||
return createHttpContext();
|
||||
}
|
||||
|
||||
private HttpContext createHttpContext() {
|
||||
|
||||
AuthCache authCache = new BasicAuthCache();
|
||||
|
||||
BasicScheme basicAuth = new BasicScheme();
|
||||
authCache.put(host, basicAuth);
|
||||
|
||||
BasicHttpContext localcontext = new BasicHttpContext();
|
||||
localcontext.setAttribute(HttpClientContext.AUTH_CACHE, authCache);
|
||||
return localcontext;
|
||||
}
|
||||
}
|
|
@ -1,15 +1,10 @@
|
|||
package org.baeldung.client;
|
||||
|
||||
import org.apache.http.auth.AuthScope;
|
||||
import org.apache.http.auth.UsernamePasswordCredentials;
|
||||
import org.apache.http.client.config.RequestConfig;
|
||||
import org.apache.http.impl.client.BasicCredentialsProvider;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
import org.apache.http.impl.client.HttpClientBuilder;
|
||||
import org.apache.http.HttpHost;
|
||||
import org.springframework.beans.factory.FactoryBean;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.http.client.ClientHttpRequestFactory;
|
||||
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
|
||||
import org.springframework.http.client.support.BasicAuthorizationInterceptor;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
|
@ -40,16 +35,10 @@ public class RestTemplateFactory implements FactoryBean<RestTemplate>, Initializ
|
|||
|
||||
@Override
|
||||
public void afterPropertiesSet() {
|
||||
final int timeout = 5;
|
||||
|
||||
final RequestConfig config = RequestConfig.custom().setConnectTimeout(timeout * 1000).setConnectionRequestTimeout(timeout * 1000).setSocketTimeout(timeout * 1000).build();
|
||||
|
||||
final BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
|
||||
credentialsProvider.setCredentials(new AuthScope("localhost", 8082, AuthScope.ANY_REALM), new UsernamePasswordCredentials("user1", "user1Pass"));
|
||||
final CloseableHttpClient client = HttpClientBuilder.create().setDefaultRequestConfig(config).setDefaultCredentialsProvider(credentialsProvider).build();
|
||||
|
||||
final ClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(client);
|
||||
HttpHost host = new HttpHost("localhost", 8082, "http");
|
||||
final ClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactoryBasicAuth(host);
|
||||
restTemplate = new RestTemplate(requestFactory);
|
||||
restTemplate.getInterceptors().add(new BasicAuthorizationInterceptor("user1", "user1Pass"));
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue