rest template work for digest auth and early work on gson
This commit is contained in:
@@ -89,7 +89,7 @@
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.core</groupId>
|
||||
<artifactId>jackson-databind</artifactId>
|
||||
<version>2.2.2</version>
|
||||
<version>${jackson.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- http -->
|
||||
@@ -227,13 +227,13 @@
|
||||
<debuglevel>source</debuglevel>
|
||||
</configuration>
|
||||
</plugin>
|
||||
|
||||
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-war-plugin</artifactId>
|
||||
<version>${maven-war-plugin.version}</version>
|
||||
</plugin>
|
||||
|
||||
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
@@ -290,6 +290,9 @@
|
||||
<org.slf4j.version>1.7.7</org.slf4j.version>
|
||||
<logback.version>1.1.2</logback.version> <!-- do not upgrade - see http://jira.qos.ch/browse/LOGBACK-851 -->
|
||||
|
||||
<!-- marshalling -->
|
||||
<jackson.version>2.4.2</jackson.version>
|
||||
|
||||
<!-- various -->
|
||||
<hibernate-validator.version>5.1.2.Final</hibernate-validator.version>
|
||||
|
||||
@@ -308,7 +311,7 @@
|
||||
<maven-compiler-plugin.version>3.1</maven-compiler-plugin.version>
|
||||
<maven-war-plugin.version>2.4</maven-war-plugin.version>
|
||||
<maven-surefire-plugin.version>2.17</maven-surefire-plugin.version>
|
||||
<cargo-maven2-plugin.version>1.4.8</cargo-maven2-plugin.version>
|
||||
<cargo-maven2-plugin.version>1.4.9</cargo-maven2-plugin.version>
|
||||
|
||||
</properties>
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@ import java.net.URI;
|
||||
|
||||
import org.apache.http.HttpHost;
|
||||
import org.apache.http.client.AuthCache;
|
||||
import org.apache.http.client.HttpClient;
|
||||
import org.apache.http.client.protocol.ClientContext;
|
||||
import org.apache.http.impl.auth.DigestScheme;
|
||||
import org.apache.http.impl.client.BasicAuthCache;
|
||||
@@ -15,8 +16,8 @@ import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
|
||||
public class HttpComponentsClientHttpRequestFactoryDigestAuth extends HttpComponentsClientHttpRequestFactory {
|
||||
HttpHost host;
|
||||
|
||||
public HttpComponentsClientHttpRequestFactoryDigestAuth(final HttpHost host) {
|
||||
super();
|
||||
public HttpComponentsClientHttpRequestFactoryDigestAuth(final HttpHost host, final HttpClient httpClient) {
|
||||
super(httpClient);
|
||||
this.host = host;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,9 +1,13 @@
|
||||
package org.baeldung.client.spring;
|
||||
|
||||
import org.apache.http.HttpHost;
|
||||
import org.apache.http.auth.AuthScope;
|
||||
import org.apache.http.auth.UsernamePasswordCredentials;
|
||||
import org.apache.http.client.CredentialsProvider;
|
||||
import org.apache.http.client.HttpClient;
|
||||
import org.apache.http.params.HttpConnectionParams;
|
||||
import org.apache.http.params.HttpParams;
|
||||
import org.apache.http.impl.client.BasicCredentialsProvider;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
import org.apache.http.impl.client.HttpClientBuilder;
|
||||
import org.baeldung.client.HttpComponentsClientHttpRequestFactoryDigestAuth;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
@@ -12,6 +16,8 @@ import org.springframework.web.client.RestTemplate;
|
||||
|
||||
@Configuration
|
||||
public class ClientConfig {
|
||||
private static final String DEFAULT_USER = "user1";
|
||||
private static final String DEFAULT_PASS = "user1Pass";
|
||||
|
||||
public ClientConfig() {
|
||||
super();
|
||||
@@ -22,7 +28,9 @@ public class ClientConfig {
|
||||
@Bean
|
||||
public RestTemplate restTemplate() {
|
||||
final HttpHost host = new HttpHost("localhost", 8080, "http");
|
||||
final HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactoryDigestAuth(host);
|
||||
final CloseableHttpClient client = HttpClientBuilder.create().setDefaultCredentialsProvider(provider()).useSystemProperties().build();
|
||||
|
||||
final HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactoryDigestAuth(host, client);
|
||||
final RestTemplate restTemplate = new RestTemplate(requestFactory);
|
||||
|
||||
final int timeout = 5;
|
||||
@@ -43,9 +51,16 @@ public class ClientConfig {
|
||||
// httpClient.getParams().setParameter("http.protocol.head-body-timeout", timeout * 1000);
|
||||
|
||||
// - note: timeout via the API
|
||||
final HttpParams httpParams = httpClient.getParams();
|
||||
HttpConnectionParams.setConnectionTimeout(httpParams, timeout * 1000); // http.connection.timeout
|
||||
HttpConnectionParams.setSoTimeout(httpParams, timeout * 1000); // http.socket.timeout
|
||||
// final HttpParams httpParams = httpClient.getParams();
|
||||
// HttpConnectionParams.setConnectionTimeout(httpParams, timeout * 1000); // http.connection.timeout
|
||||
// HttpConnectionParams.setSoTimeout(httpParams, timeout * 1000); // http.socket.timeout
|
||||
}
|
||||
|
||||
private final CredentialsProvider provider() {
|
||||
final CredentialsProvider provider = new BasicCredentialsProvider();
|
||||
final UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(DEFAULT_USER, DEFAULT_PASS);
|
||||
provider.setCredentials(AuthScope.ANY, credentials);
|
||||
return provider;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
package org.baeldung.client;
|
||||
|
||||
import org.apache.http.HttpHost;
|
||||
import org.apache.http.auth.AuthScope;
|
||||
import org.apache.http.auth.UsernamePasswordCredentials;
|
||||
import org.apache.http.client.CredentialsProvider;
|
||||
import org.apache.http.impl.client.BasicCredentialsProvider;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
import org.apache.http.impl.client.HttpClientBuilder;
|
||||
import org.baeldung.web.dto.Foo;
|
||||
import org.junit.Test;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
public class ClientNoSpringLiveTest {
|
||||
private static final String DEFAULT_USER = "user1";
|
||||
private static final String DEFAULT_PASS = "user1Pass";
|
||||
|
||||
// tests - no Spring
|
||||
|
||||
@Test
|
||||
public final void givenUsingCustomHttpRequestFactory_whenSecuredRestApiIsConsumed_then200OK() {
|
||||
final HttpHost host = new HttpHost("localhost", 8080, "http");
|
||||
|
||||
final CredentialsProvider credentialsProvider = provider();
|
||||
final CloseableHttpClient client = HttpClientBuilder.create().setDefaultCredentialsProvider(credentialsProvider).useSystemProperties().build();
|
||||
final HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactoryDigestAuth(host, client);
|
||||
final RestTemplate restTemplate = new RestTemplate(requestFactory);
|
||||
|
||||
// credentialsProvider.setCredentials(new AuthScope("localhost", 8080, AuthScope.ANY_REALM), new UsernamePasswordCredentials("user1", "user1Pass"));
|
||||
|
||||
final String uri = "http://localhost:8080/spring-security-rest-digest-auth/api/foos/1";
|
||||
final ResponseEntity<Foo> responseEntity = restTemplate.exchange(uri, HttpMethod.GET, null, Foo.class);
|
||||
|
||||
System.out.println(responseEntity.getStatusCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void givenUsingStandardRequestFactory_whenSecuredRestApiIsConsumed_then200OK() {
|
||||
final CredentialsProvider credentialsProvider = provider();
|
||||
final CloseableHttpClient client = HttpClientBuilder.create().setDefaultCredentialsProvider(credentialsProvider).useSystemProperties().build();
|
||||
final HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(client);
|
||||
final RestTemplate restTemplate = new RestTemplate(requestFactory);
|
||||
|
||||
// credentialsProvider.setCredentials(new AuthScope("localhost", 8080, AuthScope.ANY_REALM), new UsernamePasswordCredentials("user1", "user1Pass"));
|
||||
|
||||
final String uri = "http://localhost:8080/spring-security-rest-digest-auth/api/foos/1";
|
||||
final ResponseEntity<Foo> responseEntity = restTemplate.exchange(uri, HttpMethod.GET, null, Foo.class);
|
||||
|
||||
System.out.println(responseEntity.getStatusCode());
|
||||
}
|
||||
|
||||
// UTIL
|
||||
|
||||
private final CredentialsProvider provider() {
|
||||
final CredentialsProvider provider = new BasicCredentialsProvider();
|
||||
final UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(DEFAULT_USER, DEFAULT_PASS);
|
||||
provider.setCredentials(AuthScope.ANY, credentials);
|
||||
return provider;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,8 +1,5 @@
|
||||
package org.baeldung.client;
|
||||
|
||||
import org.apache.http.auth.AuthScope;
|
||||
import org.apache.http.auth.UsernamePasswordCredentials;
|
||||
import org.apache.http.impl.client.DefaultHttpClient;
|
||||
import org.baeldung.client.spring.ClientConfig;
|
||||
import org.baeldung.web.dto.Foo;
|
||||
import org.junit.Test;
|
||||
@@ -10,7 +7,6 @@ import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.support.AnnotationConfigContextLoader;
|
||||
@@ -18,21 +14,18 @@ import org.springframework.web.client.RestTemplate;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = { ClientConfig.class }, loader = AnnotationConfigContextLoader.class)
|
||||
public class ClientLiveTest {
|
||||
public class ClientWithSpringLiveTest {
|
||||
|
||||
@Autowired
|
||||
private RestTemplate restTemplate;
|
||||
|
||||
// tests
|
||||
// tests - no Spring
|
||||
|
||||
@Test
|
||||
public final void whenSecuredRestApiIsConsumed_then200OK() {
|
||||
final HttpComponentsClientHttpRequestFactory requestFactory = (HttpComponentsClientHttpRequestFactory) restTemplate.getRequestFactory();
|
||||
final DefaultHttpClient httpClient = (DefaultHttpClient) requestFactory.getHttpClient();
|
||||
httpClient.getCredentialsProvider().setCredentials(new AuthScope("localhost", 8080, AuthScope.ANY_REALM), new UsernamePasswordCredentials("user1", "user1Pass"));
|
||||
|
||||
final String uri = "http://localhost:8080/spring-security-rest-digest-auth/api/foos/1";
|
||||
final ResponseEntity<Foo> responseEntity = restTemplate.exchange(uri, HttpMethod.GET, null, Foo.class);
|
||||
|
||||
System.out.println(responseEntity.getStatusCode());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user