httpclient auth work
This commit is contained in:
@@ -23,6 +23,10 @@ import org.junit.Test;
|
||||
|
||||
public class HttpClientAuthLiveTest {
|
||||
|
||||
private static final String URL_SECURED_BY_BASIC_AUTHENTICATION = "http://localhost:8080/spring-security-rest-basic-auth/api/foos/1";
|
||||
private static final String DEFAULT_USER = "user1";
|
||||
private static final String DEFAULT_PASS = "user1Pass";
|
||||
|
||||
private CloseableHttpClient instance;
|
||||
|
||||
private CloseableHttpResponse response;
|
||||
@@ -51,22 +55,23 @@ public class HttpClientAuthLiveTest {
|
||||
|
||||
// tests
|
||||
|
||||
// simple request - response
|
||||
|
||||
@Test
|
||||
public final void whenExecutingBasicGetRequest_thenNoExceptions() throws ClientProtocolException, IOException {
|
||||
final CredentialsProvider provider = new BasicCredentialsProvider();
|
||||
final AuthScope scope = new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM);
|
||||
final UsernamePasswordCredentials credentials = new UsernamePasswordCredentials("user1", "user1Pass");
|
||||
public final void whenExecutingBasicGetRequestWithBasicAuthenticationEnabled_thenSuccess() throws ClientProtocolException, IOException {
|
||||
instance = HttpClientBuilder.create().setDefaultCredentialsProvider(provider()).build();
|
||||
|
||||
provider.setCredentials(scope, credentials);
|
||||
|
||||
instance = HttpClientBuilder.create().setDefaultCredentialsProvider(provider).build();
|
||||
|
||||
response = instance.execute(new HttpGet("http://localhost:8080/spring-security-mvc-basic-auth/homepage.html"));
|
||||
response = instance.execute(new HttpGet(URL_SECURED_BY_BASIC_AUTHENTICATION));
|
||||
|
||||
final int statusCode = response.getStatusLine().getStatusCode();
|
||||
assertThat(statusCode, equalTo(HttpStatus.SC_OK));
|
||||
}
|
||||
|
||||
// UTILS
|
||||
|
||||
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,67 @@
|
||||
package org.baeldung.httpclient;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
import org.apache.http.HttpEntity;
|
||||
import org.apache.http.auth.AuthScope;
|
||||
import org.apache.http.auth.UsernamePasswordCredentials;
|
||||
import org.apache.http.client.ClientProtocolException;
|
||||
import org.apache.http.client.CredentialsProvider;
|
||||
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
import org.apache.http.client.protocol.HttpClientContext;
|
||||
import org.apache.http.impl.client.BasicCredentialsProvider;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
import org.apache.http.impl.client.HttpClientBuilder;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
public class HttpClientSandboxLiveTest {
|
||||
|
||||
private CloseableHttpClient client;
|
||||
|
||||
private CloseableHttpResponse response;
|
||||
|
||||
@Before
|
||||
public final void before() {
|
||||
client = HttpClientBuilder.create().build();
|
||||
}
|
||||
|
||||
@After
|
||||
public final void after() throws IllegalStateException, IOException {
|
||||
if (response == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
final HttpEntity entity = response.getEntity();
|
||||
if (entity != null) {
|
||||
// EntityUtils.consume(entity);
|
||||
final InputStream instream = entity.getContent();
|
||||
instream.close();
|
||||
}
|
||||
} finally {
|
||||
response.close();
|
||||
}
|
||||
}
|
||||
|
||||
// tests
|
||||
|
||||
// simple request - response
|
||||
|
||||
@Test
|
||||
public final void givenGetRequestExecuted_whenAnalyzingTheResponse_thenCorrectStatusCode() throws ClientProtocolException, IOException {
|
||||
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
|
||||
final AuthScope authscp = new AuthScope("api.calltrackingmetrics.com", 443);
|
||||
credentialsProvider.setCredentials(authscp, new UsernamePasswordCredentials("mykey", "mysecret"));
|
||||
final HttpClientContext localContext = HttpClientContext.create();
|
||||
localContext.setCredentialsProvider(credentialsProvider);
|
||||
final HttpGet httpGet = new HttpGet("https://api.calltrackingmetrics.com/api/v1/accounts/myaccout/calls.json");
|
||||
response = client.execute(httpGet);
|
||||
|
||||
System.out.println(response.getStatusLine());
|
||||
}
|
||||
|
||||
}
|
||||
50
httpclient/src/test/resources/sandbox.txt
Normal file
50
httpclient/src/test/resources/sandbox.txt
Normal file
@@ -0,0 +1,50 @@
|
||||
web - 2014-01-30 20:48:07,161 [main] DEBUG o.a.h.c.protocol.RequestAddCookies - CookieSpec selected: best-match
|
||||
web - 2014-01-30 20:48:07,171 [main] DEBUG o.a.h.c.protocol.RequestAuthCache - Auth cache not set in the context
|
||||
web - 2014-01-30 20:48:07,172 [main] DEBUG o.a.h.i.c.PoolingHttpClientConnectionManager - Connection request: [route: {}->http://localhost:8080][total kept alive: 0; route allocated: 0 of 2; total allocated: 0 of 20]
|
||||
web - 2014-01-30 20:48:07,185 [main] DEBUG o.a.h.i.c.PoolingHttpClientConnectionManager - Connection leased: [id: 0][route: {}->http://localhost:8080][total kept alive: 0; route allocated: 1 of 2; total allocated: 1 of 20]
|
||||
web - 2014-01-30 20:48:07,190 [main] DEBUG o.a.h.impl.execchain.MainClientExec - Opening connection {}->http://localhost:8080
|
||||
web - 2014-01-30 20:48:07,192 [main] DEBUG o.a.h.c.HttpClientConnectionManager - Connecting to localhost/127.0.0.1:8080
|
||||
web - 2014-01-30 20:48:07,193 [main] DEBUG o.a.h.impl.execchain.MainClientExec - Executing request GET /spring-security-rest-basic-auth/api/foos/1 HTTP/1.1
|
||||
web - 2014-01-30 20:48:07,193 [main] DEBUG o.a.h.impl.execchain.MainClientExec - Target auth state: UNCHALLENGED
|
||||
web - 2014-01-30 20:48:07,193 [main] DEBUG o.a.h.impl.execchain.MainClientExec - Proxy auth state: UNCHALLENGED
|
||||
web - 2014-01-30 20:48:07,194 [main] DEBUG org.apache.http.headers - http-outgoing-0 >> GET /spring-security-rest-basic-auth/api/foos/1 HTTP/1.1
|
||||
web - 2014-01-30 20:48:07,194 [main] DEBUG org.apache.http.headers - http-outgoing-0 >> Host: localhost:8080
|
||||
web - 2014-01-30 20:48:07,194 [main] DEBUG org.apache.http.headers - http-outgoing-0 >> Connection: Keep-Alive
|
||||
web - 2014-01-30 20:48:07,194 [main] DEBUG org.apache.http.headers - http-outgoing-0 >> User-Agent: Apache-HttpClient/4.3.1 (java 1.5)
|
||||
web - 2014-01-30 20:48:07,194 [main] DEBUG org.apache.http.headers - http-outgoing-0 >> Accept-Encoding: gzip,deflate
|
||||
web - 2014-01-30 20:48:07,203 [main] DEBUG org.apache.http.headers - http-outgoing-0 << HTTP/1.1 401 Unauthorized
|
||||
web - 2014-01-30 20:48:07,203 [main] DEBUG org.apache.http.headers - http-outgoing-0 << Server: Apache-Coyote/1.1
|
||||
web - 2014-01-30 20:48:07,203 [main] DEBUG org.apache.http.headers - http-outgoing-0 << WWW-Authenticate: Basic realm="Spring Security Application"
|
||||
web - 2014-01-30 20:48:07,203 [main] DEBUG org.apache.http.headers - http-outgoing-0 << Content-Type: text/html;charset=utf-8
|
||||
web - 2014-01-30 20:48:07,203 [main] DEBUG org.apache.http.headers - http-outgoing-0 << Content-Language: en
|
||||
web - 2014-01-30 20:48:07,203 [main] DEBUG org.apache.http.headers - http-outgoing-0 << Content-Length: 1061
|
||||
web - 2014-01-30 20:48:07,203 [main] DEBUG org.apache.http.headers - http-outgoing-0 << Date: Thu, 30 Jan 2014 18:48:07 GMT
|
||||
web - 2014-01-30 20:48:07,206 [main] DEBUG o.a.h.impl.execchain.MainClientExec - Connection can be kept alive indefinitely
|
||||
web - 2014-01-30 20:48:07,206 [main] DEBUG o.a.http.impl.auth.HttpAuthenticator - Authentication required
|
||||
web - 2014-01-30 20:48:07,206 [main] DEBUG o.a.http.impl.auth.HttpAuthenticator - localhost:8080 requested authentication
|
||||
web - 2014-01-30 20:48:07,206 [main] DEBUG o.a.h.i.c.TargetAuthenticationStrategy - Authentication schemes in the order of preference: [negotiate, Kerberos, NTLM, Digest, Basic]
|
||||
web - 2014-01-30 20:48:07,206 [main] DEBUG o.a.h.i.c.TargetAuthenticationStrategy - Challenge for negotiate authentication scheme not available
|
||||
web - 2014-01-30 20:48:07,206 [main] DEBUG o.a.h.i.c.TargetAuthenticationStrategy - Challenge for Kerberos authentication scheme not available
|
||||
web - 2014-01-30 20:48:07,206 [main] DEBUG o.a.h.i.c.TargetAuthenticationStrategy - Challenge for NTLM authentication scheme not available
|
||||
web - 2014-01-30 20:48:07,206 [main] DEBUG o.a.h.i.c.TargetAuthenticationStrategy - Challenge for Digest authentication scheme not available
|
||||
web - 2014-01-30 20:48:07,213 [main] DEBUG o.a.http.impl.auth.HttpAuthenticator - Selected authentication options: [BASIC]
|
||||
web - 2014-01-30 20:48:07,214 [main] DEBUG o.a.h.impl.execchain.MainClientExec - Executing request GET /spring-security-rest-basic-auth/api/foos/1 HTTP/1.1
|
||||
web - 2014-01-30 20:48:07,214 [main] DEBUG o.a.h.impl.execchain.MainClientExec - Target auth state: CHALLENGED
|
||||
web - 2014-01-30 20:48:07,214 [main] DEBUG o.a.http.impl.auth.HttpAuthenticator - Generating response to an authentication challenge using basic scheme
|
||||
web - 2014-01-30 20:48:07,215 [main] DEBUG o.a.h.impl.execchain.MainClientExec - Proxy auth state: UNCHALLENGED
|
||||
web - 2014-01-30 20:48:07,215 [main] DEBUG org.apache.http.headers - http-outgoing-0 >> GET /spring-security-rest-basic-auth/api/foos/1 HTTP/1.1
|
||||
web - 2014-01-30 20:48:07,215 [main] DEBUG org.apache.http.headers - http-outgoing-0 >> Host: localhost:8080
|
||||
web - 2014-01-30 20:48:07,215 [main] DEBUG org.apache.http.headers - http-outgoing-0 >> Connection: Keep-Alive
|
||||
web - 2014-01-30 20:48:07,215 [main] DEBUG org.apache.http.headers - http-outgoing-0 >> User-Agent: Apache-HttpClient/4.3.1 (java 1.5)
|
||||
web - 2014-01-30 20:48:07,215 [main] DEBUG org.apache.http.headers - http-outgoing-0 >> Accept-Encoding: gzip,deflate
|
||||
web - 2014-01-30 20:48:07,215 [main] DEBUG org.apache.http.headers - http-outgoing-0 >> Authorization: Basic dXNlcjE6dXNlcjFQYXNz
|
||||
web - 2014-01-30 20:48:07,217 [main] DEBUG org.apache.http.headers - http-outgoing-0 << HTTP/1.1 200 OK
|
||||
web - 2014-01-30 20:48:07,217 [main] DEBUG org.apache.http.headers - http-outgoing-0 << Server: Apache-Coyote/1.1
|
||||
web - 2014-01-30 20:48:07,217 [main] DEBUG org.apache.http.headers - http-outgoing-0 << Content-Type: application/json;charset=UTF-8
|
||||
web - 2014-01-30 20:48:07,217 [main] DEBUG org.apache.http.headers - http-outgoing-0 << Transfer-Encoding: chunked
|
||||
web - 2014-01-30 20:48:07,217 [main] DEBUG org.apache.http.headers - http-outgoing-0 << Date: Thu, 30 Jan 2014 18:48:07 GMT
|
||||
web - 2014-01-30 20:48:07,218 [main] DEBUG o.a.h.impl.execchain.MainClientExec - Connection can be kept alive indefinitely
|
||||
web - 2014-01-30 20:48:07,218 [main] DEBUG o.a.http.impl.auth.HttpAuthenticator - Authentication succeeded
|
||||
web - 2014-01-30 20:48:07,219 [main] DEBUG o.a.h.i.c.TargetAuthenticationStrategy - Caching 'basic' auth scheme for http://localhost:8080
|
||||
web - 2014-01-30 20:48:07,227 [main] DEBUG o.a.h.i.c.PoolingHttpClientConnectionManager - Connection [id: 0][route: {}->http://localhost:8080] can be kept alive indefinitely
|
||||
web - 2014-01-30 20:48:07,227 [main] DEBUG o.a.h.i.c.PoolingHttpClientConnectionManager - Connection released: [id: 0][route: {}->http://localhost:8080][total kept alive: 1; route allocated: 1 of 2; total allocated: 1 of 20]
|
||||
Reference in New Issue
Block a user