use of org.apache.http.impl.client.BasicCredentialsProvider in project rdf4j by eclipse.
the class SPARQLProtocolSession method setUsernameAndPasswordForUrl.
protected void setUsernameAndPasswordForUrl(String username, String password, String url) {
if (username != null && password != null) {
logger.debug("Setting username '{}' and password for server at {}.", username, url);
java.net.URI requestURI = java.net.URI.create(url);
String host = requestURI.getHost();
int port = requestURI.getPort();
AuthScope scope = new AuthScope(host, port);
UsernamePasswordCredentials cred = new UsernamePasswordCredentials(username, password);
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(scope, cred);
httpContext.setCredentialsProvider(credsProvider);
AuthCache authCache = new BasicAuthCache();
BasicScheme basicAuth = new BasicScheme();
HttpHost httpHost = new HttpHost(requestURI.getHost(), requestURI.getPort(), requestURI.getScheme());
authCache.put(httpHost, basicAuth);
httpContext.setAuthCache(authCache);
} else {
httpContext.removeAttribute(HttpClientContext.AUTH_CACHE);
httpContext.removeAttribute(HttpClientContext.CREDS_PROVIDER);
}
}
use of org.apache.http.impl.client.BasicCredentialsProvider in project watchdog by TestRoots.
the class NetworkUtils method createAuthenticatedHttpClient.
/**
* Creates an HTTP client that uses an authenticated connection.
*/
private static CloseableHttpClient createAuthenticatedHttpClient() {
CredentialsProvider provider = new BasicCredentialsProvider();
byte[] providerInfo = { 104, 110, 115, 112, 113, 115, 122, 110, 112, 113 };
UsernamePasswordCredentials credentials = new UsernamePasswordCredentials("watchdogplugin", new String(providerInfo, Charset.defaultCharset()));
provider.setCredentials(AuthScope.ANY, credentials);
return createPlainHttpClientBuilder().setDefaultCredentialsProvider(provider).build();
}
use of org.apache.http.impl.client.BasicCredentialsProvider in project webdrivermanager by bonigarcia.
the class ProxyTest method testProxyCredentialsScope.
@Test
public void testProxyCredentialsScope() throws Exception {
WebDriverManager.config().setProxy("myproxy:8081").setProxyUser("domain\\me").setProxyPass("pass");
HttpClient wdmClient = new HttpClient();
Field field = HttpClient.class.getDeclaredField("closeableHttpClient");
field.setAccessible(true);
CloseableHttpClient client = (CloseableHttpClient) field.get(wdmClient);
field = client.getClass().getDeclaredField("credentialsProvider");
field.setAccessible(true);
BasicCredentialsProvider provider = (BasicCredentialsProvider) field.get(client);
assertThat(provider.getCredentials(new AuthScope(ANY_HOST, ANY_PORT, ANY_REALM, NTLM)), instanceOf(NTCredentials.class));
assertThat(provider.getCredentials(new AuthScope(ANY_HOST, ANY_PORT, ANY_REALM, BASIC)), instanceOf(UsernamePasswordCredentials.class));
assertThat(provider.getCredentials(new AuthScope(ANY_HOST, ANY_PORT)), instanceOf(UsernamePasswordCredentials.class));
assertThat(provider.getCredentials(new AuthScope(ANY_HOST, ANY_PORT, ANY_REALM, KERBEROS)), instanceOf(UsernamePasswordCredentials.class));
}
use of org.apache.http.impl.client.BasicCredentialsProvider in project webdrivermanager by bonigarcia.
the class ProxyTest method testProxyCredentials.
@Test
public void testProxyCredentials() throws Exception {
WebDriverManager.config().setProxy("myproxy:8081").setProxyUser("domain\\me").setProxyPass("pass");
HttpClient wdmClient = new HttpClient();
Field field = HttpClient.class.getDeclaredField("closeableHttpClient");
field.setAccessible(true);
CloseableHttpClient client = (CloseableHttpClient) field.get(wdmClient);
field = client.getClass().getDeclaredField("credentialsProvider");
field.setAccessible(true);
BasicCredentialsProvider provider = (BasicCredentialsProvider) field.get(client);
NTCredentials ntcreds = (NTCredentials) provider.getCredentials(new AuthScope(ANY_HOST, ANY_PORT, ANY_REALM, NTLM));
assertThat(ntcreds.getDomain(), equalTo("DOMAIN"));
assertThat(ntcreds.getUserName(), equalTo("me"));
assertThat(ntcreds.getPassword(), equalTo("pass"));
UsernamePasswordCredentials creds = (UsernamePasswordCredentials) provider.getCredentials(new AuthScope(ANY_HOST, ANY_PORT));
assertThat(creds.getUserName(), equalTo("domain\\me"));
assertThat(creds.getPassword(), equalTo("pass"));
}
use of org.apache.http.impl.client.BasicCredentialsProvider in project openolat by klemens.
the class HttpClientFactory method getHttpClientInstance.
/**
* @param host For basic authentication
* @param port For basic authentication
* @param user For basic authentication
* @param password For basic authentication
* @param redirect If redirect is allowed
* @return CloseableHttpClient
*/
public static CloseableHttpClient getHttpClientInstance(String host, int port, String user, String password, boolean redirect) {
PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(socketRegistry);
SocketConfig.Builder socketConfigBuilder = SocketConfig.copy(SocketConfig.DEFAULT);
socketConfigBuilder.setSoTimeout(10000);
cm.setDefaultSocketConfig(socketConfigBuilder.build());
HttpClientBuilder clientBuilder = HttpClientBuilder.create().setConnectionManager(cm).setMaxConnTotal(10).setDefaultCookieStore(new BasicCookieStore());
if (redirect) {
clientBuilder.setRedirectStrategy(new LaxRedirectStrategy());
} else {
clientBuilder.setRedirectStrategy(new NoRedirectStrategy());
}
if (user != null && user.length() > 0) {
CredentialsProvider provider = new BasicCredentialsProvider();
provider.setCredentials(new AuthScope(host, port), new UsernamePasswordCredentials(user, password));
clientBuilder.setDefaultCredentialsProvider(provider);
}
return clientBuilder.build();
}
Aggregations