Merge branch 'BAEL-248_Update_deprecated_API' of https://github.com/khatwaniNikhil/tutorials into khatwaniNikhil-BAEL-248_Update_deprecated_API
This commit is contained in:
		
						commit
						ae748d1ae2
					
				| @ -342,8 +342,8 @@ | |||||||
|      |      | ||||||
|     <properties> |     <properties> | ||||||
|         <!-- Spring --> |         <!-- Spring --> | ||||||
|         <org.springframework.version>4.3.4.RELEASE</org.springframework.version> |         <org.springframework.version>4.3.6.RELEASE</org.springframework.version> | ||||||
|         <org.springframework.security.version>4.2.0.RELEASE</org.springframework.security.version> |         <org.springframework.security.version>4.2.1.RELEASE</org.springframework.security.version> | ||||||
| 
 | 
 | ||||||
|         <!-- persistence --> |         <!-- persistence --> | ||||||
|         <hibernate.version>5.2.5.Final</hibernate.version> |         <hibernate.version>5.2.5.Final</hibernate.version> | ||||||
| @ -351,7 +351,7 @@ | |||||||
| 
 | 
 | ||||||
|         <!-- http --> |         <!-- http --> | ||||||
|         <httpcore.version>4.4.5</httpcore.version> |         <httpcore.version>4.4.5</httpcore.version> | ||||||
|         <httpclient.version>4.5.2</httpclient.version> |         <httpclient.version>4.5.3</httpclient.version> | ||||||
| 
 | 
 | ||||||
|         <!-- logging --> |         <!-- logging --> | ||||||
|         <org.slf4j.version>1.7.21</org.slf4j.version> |         <org.slf4j.version>1.7.21</org.slf4j.version> | ||||||
|  | |||||||
| @ -1,15 +1,11 @@ | |||||||
| package org.baeldung.client; | package org.baeldung.client; | ||||||
| 
 | 
 | ||||||
| import org.apache.http.auth.AuthScope; | import org.apache.http.HttpHost; | ||||||
| import org.apache.http.auth.UsernamePasswordCredentials; | import org.baeldung.http.client.HttpComponentsClientHttpRequestFactoryBasicAuth; | ||||||
| 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.springframework.beans.factory.FactoryBean; | import org.springframework.beans.factory.FactoryBean; | ||||||
| import org.springframework.beans.factory.InitializingBean; | import org.springframework.beans.factory.InitializingBean; | ||||||
| import org.springframework.http.client.ClientHttpRequestFactory; | 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.stereotype.Component; | ||||||
| import org.springframework.web.client.RestTemplate; | import org.springframework.web.client.RestTemplate; | ||||||
| 
 | 
 | ||||||
| @ -40,16 +36,10 @@ public class RestTemplateFactory implements FactoryBean<RestTemplate>, Initializ | |||||||
| 
 | 
 | ||||||
|     @Override |     @Override | ||||||
|     public void afterPropertiesSet() { |     public void afterPropertiesSet() { | ||||||
|         final int timeout = 5; |         HttpHost host = new HttpHost("localhost", 8082, "http"); | ||||||
| 
 |         final ClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactoryBasicAuth(host); | ||||||
|         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); |  | ||||||
|         restTemplate = new RestTemplate(requestFactory); |         restTemplate = new RestTemplate(requestFactory); | ||||||
|  |         restTemplate.getInterceptors().add(new BasicAuthorizationInterceptor("user1", "user1Pass")); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
| } | } | ||||||
| @ -0,0 +1,39 @@ | |||||||
|  | package org.baeldung.http.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; | ||||||
|  |     } | ||||||
|  | } | ||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user