use of org.apache.http.impl.client.BasicCredentialsProvider in project tutorials by eugenp.
the class HttpClientSandboxLiveTest method givenGetRequestExecuted_whenAnalyzingTheResponse_thenCorrectStatusCode.
@Test
public final void givenGetRequestExecuted_whenAnalyzingTheResponse_thenCorrectStatusCode() throws IOException {
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
final AuthScope authscp = new AuthScope("localhost", 8080);
credentialsProvider.setCredentials(authscp, new UsernamePasswordCredentials("user1", "user1Pass"));
final CloseableHttpClient client = HttpClientBuilder.create().setDefaultCredentialsProvider(credentialsProvider).build();
final HttpGet httpGet = new HttpGet("http://localhost:8080/spring-security-rest-basic-auth/api/foos/1");
final CloseableHttpResponse response = client.execute(httpGet);
System.out.println(response.getStatusLine());
ResponseUtil.closeResponse(response);
}
use of org.apache.http.impl.client.BasicCredentialsProvider in project tutorials by eugenp.
the class HttpClientAuthLiveTest method provider.
// UTILS
private CredentialsProvider provider() {
final CredentialsProvider provider = new BasicCredentialsProvider();
final UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(DEFAULT_USER, DEFAULT_PASS);
provider.setCredentials(AuthScope.ANY, credentials);
return provider;
}
use of org.apache.http.impl.client.BasicCredentialsProvider in project connect-sdk-java by Ingenico-ePayments.
the class DefaultConnectionTest method assertProxy.
@SuppressWarnings("resource")
private static void assertProxy(DefaultConnection connection, ProxyConfiguration proxyConfiguration) {
CloseableHttpClient httpClient = ReflectionUtil.getField(connection, "httpClient", CloseableHttpClient.class);
DefaultProxyRoutePlanner routePlanner = ReflectionUtil.getField(httpClient, "routePlanner", DefaultProxyRoutePlanner.class);
HttpHost proxy = ReflectionUtil.getField(routePlanner, "proxy", HttpHost.class);
Assert.assertEquals(proxyConfiguration.getScheme(), proxy.getSchemeName());
Assert.assertEquals(proxyConfiguration.getPort(), proxy.getPort());
BasicCredentialsProvider credentialsProvider = ReflectionUtil.getField(httpClient, "credentialsProvider", BasicCredentialsProvider.class);
AuthScope authScope = new AuthScope(proxy);
Credentials credentials = credentialsProvider.getCredentials(authScope);
if (proxyConfiguration.getUsername() != null) {
Assert.assertTrue(credentials instanceof UsernamePasswordCredentials);
UsernamePasswordCredentials usernamePasswordCredentials = (UsernamePasswordCredentials) credentials;
Assert.assertEquals(proxyConfiguration.getUsername(), usernamePasswordCredentials.getUserName());
Assert.assertEquals(proxyConfiguration.getPassword(), usernamePasswordCredentials.getPassword());
} else {
Assert.assertNull(credentials);
}
}
use of org.apache.http.impl.client.BasicCredentialsProvider in project connect-sdk-java by Ingenico-ePayments.
the class SDKProxyTest method assertProxySet.
@SuppressWarnings("resource")
private void assertProxySet(ApiResource resource, ProxyConfiguration proxyConfiguration) {
Communicator communicator = getField(resource, "communicator", Communicator.class);
Session session = getField(communicator, "session", Session.class);
DefaultConnection connection = getField(session, "connection", DefaultConnection.class);
CloseableHttpClient httpClient = getField(connection, "httpClient", CloseableHttpClient.class);
DefaultProxyRoutePlanner routePlanner = getField(httpClient, "routePlanner", DefaultProxyRoutePlanner.class);
HttpHost proxy = getField(routePlanner, "proxy", HttpHost.class);
Assert.assertEquals(proxyConfiguration.getScheme(), proxy.getSchemeName());
Assert.assertEquals(proxyConfiguration.getPort(), proxy.getPort());
BasicCredentialsProvider credentialsProvider = getField(httpClient, "credentialsProvider", BasicCredentialsProvider.class);
AuthScope authScope = new AuthScope(proxy);
Credentials credentials = credentialsProvider.getCredentials(authScope);
Assert.assertTrue(credentials instanceof UsernamePasswordCredentials);
UsernamePasswordCredentials usernamePasswordCredentials = (UsernamePasswordCredentials) credentials;
Assert.assertEquals(proxyConfiguration.getUsername(), usernamePasswordCredentials.getUserName());
Assert.assertEquals(proxyConfiguration.getPassword(), usernamePasswordCredentials.getPassword());
}
use of org.apache.http.impl.client.BasicCredentialsProvider in project connect-sdk-java by Ingenico-ePayments.
the class DefaultConnection method createHttpClient.
private CloseableHttpClient createHttpClient(ProxyConfiguration proxyConfiguration) {
HttpClientBuilder builder = HttpClients.custom().setConnectionManager(connectionManager);
HttpRoutePlanner routePlanner;
CredentialsProvider credentialsProvider;
if (proxyConfiguration != null) {
HttpHost proxy = new HttpHost(proxyConfiguration.getHost(), proxyConfiguration.getPort(), proxyConfiguration.getScheme());
routePlanner = new DefaultProxyRoutePlanner(proxy, DefaultSchemePortResolver.INSTANCE);
credentialsProvider = new BasicCredentialsProvider();
if (proxyConfiguration.getUsername() != null) {
AuthScope authscope = new AuthScope(proxyConfiguration.getHost(), proxyConfiguration.getPort());
final Credentials credentials = new UsernamePasswordCredentials(proxyConfiguration.getUsername(), proxyConfiguration.getPassword());
credentialsProvider.setCredentials(authscope, credentials);
// enable preemptive authentication
HttpRequestInterceptor proxyAuthenticationInterceptor = new HttpRequestInterceptor() {
@Override
public void process(HttpRequest request, HttpContext context) throws HttpException, IOException {
Header header = request.getFirstHeader(AUTH.PROXY_AUTH_RESP);
if (header == null) {
header = new BasicScheme((Charset) null).authenticate(credentials, request, context);
if (!AUTH.PROXY_AUTH_RESP.equals(header.getName())) {
header = new BasicHeader(AUTH.PROXY_AUTH_RESP, header.getValue());
}
request.setHeader(header);
}
}
};
builder = builder.addInterceptorLast(proxyAuthenticationInterceptor);
}
} else {
// add support for system properties
routePlanner = new SystemDefaultRoutePlanner(DefaultSchemePortResolver.INSTANCE, ProxySelector.getDefault());
credentialsProvider = new SystemDefaultCredentialsProvider();
}
// add logging - last for requests, first for responses
LoggingInterceptor loggingInterceptor = new LoggingInterceptor();
builder = builder.addInterceptorLast((HttpRequestInterceptor) loggingInterceptor);
builder = builder.addInterceptorFirst((HttpResponseInterceptor) loggingInterceptor);
return builder.setRoutePlanner(routePlanner).setDefaultCredentialsProvider(credentialsProvider).build();
}
Aggregations