Search in sources :

Example 76 with BasicCredentialsProvider

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);
    }
}
Also used : BasicScheme(org.apache.http.impl.auth.BasicScheme) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) HttpHost(org.apache.http.HttpHost) AuthScope(org.apache.http.auth.AuthScope) AuthCache(org.apache.http.client.AuthCache) BasicAuthCache(org.apache.http.impl.client.BasicAuthCache) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) CredentialsProvider(org.apache.http.client.CredentialsProvider) BasicAuthCache(org.apache.http.impl.client.BasicAuthCache) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials)

Example 77 with BasicCredentialsProvider

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();
}
Also used : BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) CredentialsProvider(org.apache.http.client.CredentialsProvider) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials)

Example 78 with BasicCredentialsProvider

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));
}
Also used : Field(java.lang.reflect.Field) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) HttpClient(io.github.bonigarcia.wdm.HttpClient) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) AuthScope(org.apache.http.auth.AuthScope) NTCredentials(org.apache.http.auth.NTCredentials) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials) Test(org.junit.Test)

Example 79 with BasicCredentialsProvider

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"));
}
Also used : Field(java.lang.reflect.Field) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) HttpClient(io.github.bonigarcia.wdm.HttpClient) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) AuthScope(org.apache.http.auth.AuthScope) NTCredentials(org.apache.http.auth.NTCredentials) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials) Test(org.junit.Test)

Example 80 with BasicCredentialsProvider

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();
}
Also used : BasicCookieStore(org.apache.http.impl.client.BasicCookieStore) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) SocketConfig(org.apache.http.config.SocketConfig) AuthScope(org.apache.http.auth.AuthScope) HttpClientBuilder(org.apache.http.impl.client.HttpClientBuilder) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) CredentialsProvider(org.apache.http.client.CredentialsProvider) LaxRedirectStrategy(org.apache.http.impl.client.LaxRedirectStrategy) PoolingHttpClientConnectionManager(org.apache.http.impl.conn.PoolingHttpClientConnectionManager) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials)

Aggregations

BasicCredentialsProvider (org.apache.http.impl.client.BasicCredentialsProvider)192 UsernamePasswordCredentials (org.apache.http.auth.UsernamePasswordCredentials)162 CredentialsProvider (org.apache.http.client.CredentialsProvider)147 AuthScope (org.apache.http.auth.AuthScope)104 HttpHost (org.apache.http.HttpHost)76 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)53 HttpClientBuilder (org.apache.http.impl.client.HttpClientBuilder)41 HttpResponse (org.apache.http.HttpResponse)38 IOException (java.io.IOException)35 HttpClientContext (org.apache.http.client.protocol.HttpClientContext)31 HttpGet (org.apache.http.client.methods.HttpGet)30 HttpClient (org.apache.http.client.HttpClient)29 BasicAuthCache (org.apache.http.impl.client.BasicAuthCache)29 AuthCache (org.apache.http.client.AuthCache)28 BasicScheme (org.apache.http.impl.auth.BasicScheme)27 RequestConfig (org.apache.http.client.config.RequestConfig)25 HttpPost (org.apache.http.client.methods.HttpPost)20 Credentials (org.apache.http.auth.Credentials)19 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)19 Test (org.junit.Test)16