Search in sources :

Example 11 with BasicCredentialsProvider

use of org.apache.hc.client5.http.impl.auth.BasicCredentialsProvider in project gradle-download-task by michel-kraemer.

the class DownloadAction method addAuthentication.

/**
 * Add authentication information for the given host
 * @param host the host
 * @param credentials the credentials
 * @param context the context in which the authentication information
 * should be saved
 */
private void addAuthentication(HttpHost host, Credentials credentials, HttpClientContext context) {
    AuthCache authCache = context.getAuthCache();
    if (authCache == null) {
        authCache = new BasicAuthCache();
        context.setAuthCache(authCache);
    }
    CredentialsProvider credsProvider = context.getCredentialsProvider();
    if (credsProvider == null) {
        credsProvider = new BasicCredentialsProvider();
        context.setCredentialsProvider(credsProvider);
    }
    ((CredentialsStore) credsProvider).setCredentials(new AuthScope(host), credentials);
}
Also used : BasicCredentialsProvider(org.apache.hc.client5.http.impl.auth.BasicCredentialsProvider) AuthCache(org.apache.hc.client5.http.auth.AuthCache) BasicAuthCache(org.apache.hc.client5.http.impl.auth.BasicAuthCache) CredentialsStore(org.apache.hc.client5.http.auth.CredentialsStore) AuthScope(org.apache.hc.client5.http.auth.AuthScope) BasicAuthCache(org.apache.hc.client5.http.impl.auth.BasicAuthCache) BasicCredentialsProvider(org.apache.hc.client5.http.impl.auth.BasicCredentialsProvider) CredentialsProvider(org.apache.hc.client5.http.auth.CredentialsProvider)

Example 12 with BasicCredentialsProvider

use of org.apache.hc.client5.http.impl.auth.BasicCredentialsProvider in project wildfly-clustering-spring-session by wildfly-clustering.

the class AuthSmokeITCase method createClient.

private static CloseableHttpClient createClient(URL url1, URL url2) {
    CredentialsStore provider = new BasicCredentialsProvider();
    Credentials credentials = new UsernamePasswordCredentials("admin", "password".toCharArray());
    provider.setCredentials(new AuthScope(url1.getHost(), url1.getPort()), credentials);
    provider.setCredentials(new AuthScope(url2.getHost(), url2.getPort()), credentials);
    return HttpClients.custom().setDefaultCredentialsProvider(provider).build();
}
Also used : BasicCredentialsProvider(org.apache.hc.client5.http.impl.auth.BasicCredentialsProvider) CredentialsStore(org.apache.hc.client5.http.auth.CredentialsStore) AuthScope(org.apache.hc.client5.http.auth.AuthScope) Credentials(org.apache.hc.client5.http.auth.Credentials) UsernamePasswordCredentials(org.apache.hc.client5.http.auth.UsernamePasswordCredentials) UsernamePasswordCredentials(org.apache.hc.client5.http.auth.UsernamePasswordCredentials)

Example 13 with BasicCredentialsProvider

use of org.apache.hc.client5.http.impl.auth.BasicCredentialsProvider in project wildfly-clustering-spring-session by wildfly-clustering.

the class AuthSmokeITCase method createClient.

private static CloseableHttpClient createClient(URL url1, URL url2) {
    CredentialsStore provider = new BasicCredentialsProvider();
    Credentials credentials = new UsernamePasswordCredentials("admin", "password".toCharArray());
    provider.setCredentials(new AuthScope(url1.getHost(), url1.getPort()), credentials);
    provider.setCredentials(new AuthScope(url2.getHost(), url2.getPort()), credentials);
    return HttpClients.custom().setDefaultCredentialsProvider(provider).build();
}
Also used : BasicCredentialsProvider(org.apache.hc.client5.http.impl.auth.BasicCredentialsProvider) CredentialsStore(org.apache.hc.client5.http.auth.CredentialsStore) AuthScope(org.apache.hc.client5.http.auth.AuthScope) Credentials(org.apache.hc.client5.http.auth.Credentials) UsernamePasswordCredentials(org.apache.hc.client5.http.auth.UsernamePasswordCredentials) UsernamePasswordCredentials(org.apache.hc.client5.http.auth.UsernamePasswordCredentials)

Example 14 with BasicCredentialsProvider

use of org.apache.hc.client5.http.impl.auth.BasicCredentialsProvider in project mercury by yellow013.

the class ClientProxyAuthentication method main.

public static void main(final String[] args) throws Exception {
    final BasicCredentialsProvider credsProvider = new BasicCredentialsProvider();
    credsProvider.setCredentials(new AuthScope("localhost", 8888), new UsernamePasswordCredentials("squid", "squid".toCharArray()));
    credsProvider.setCredentials(new AuthScope("httpbin.org", 80), new UsernamePasswordCredentials("user", "passwd".toCharArray()));
    try (final CloseableHttpClient httpclient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build()) {
        final HttpHost target = new HttpHost("http", "httpbin.org", 80);
        final HttpHost proxy = new HttpHost("localhost", 8888);
        final RequestConfig config = RequestConfig.custom().setProxy(proxy).build();
        final HttpGet httpget = new HttpGet("/basic-auth/user/passwd");
        httpget.setConfig(config);
        System.out.println("Executing request " + httpget.getMethod() + " " + httpget.getUri() + " via " + proxy);
        try (final CloseableHttpResponse response = httpclient.execute(target, httpget)) {
            System.out.println("----------------------------------------");
            System.out.println(response.getCode() + " " + response.getReasonPhrase());
            System.out.println(EntityUtils.toString(response.getEntity()));
        }
    }
}
Also used : CloseableHttpClient(org.apache.hc.client5.http.impl.classic.CloseableHttpClient) RequestConfig(org.apache.hc.client5.http.config.RequestConfig) BasicCredentialsProvider(org.apache.hc.client5.http.impl.auth.BasicCredentialsProvider) HttpHost(org.apache.hc.core5.http.HttpHost) HttpGet(org.apache.hc.client5.http.classic.methods.HttpGet) AuthScope(org.apache.hc.client5.http.auth.AuthScope) CloseableHttpResponse(org.apache.hc.client5.http.impl.classic.CloseableHttpResponse) UsernamePasswordCredentials(org.apache.hc.client5.http.auth.UsernamePasswordCredentials)

Example 15 with BasicCredentialsProvider

use of org.apache.hc.client5.http.impl.auth.BasicCredentialsProvider in project mercury by yellow013.

the class ClientAuthentication method main.

public static void main(final String[] args) throws Exception {
    final BasicCredentialsProvider credsProvider = new BasicCredentialsProvider();
    credsProvider.setCredentials(new AuthScope("httpbin.org", 80), new UsernamePasswordCredentials("user", "passwd".toCharArray()));
    try (final CloseableHttpClient httpclient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build()) {
        final HttpGet httpget = new HttpGet("http://httpbin.org/basic-auth/user/passwd");
        System.out.println("Executing request " + httpget.getMethod() + " " + httpget.getUri());
        try (final CloseableHttpResponse response = httpclient.execute(httpget)) {
            System.out.println("----------------------------------------");
            System.out.println(response.getCode() + " " + response.getReasonPhrase());
            System.out.println(EntityUtils.toString(response.getEntity()));
        }
    }
}
Also used : CloseableHttpClient(org.apache.hc.client5.http.impl.classic.CloseableHttpClient) BasicCredentialsProvider(org.apache.hc.client5.http.impl.auth.BasicCredentialsProvider) HttpGet(org.apache.hc.client5.http.classic.methods.HttpGet) AuthScope(org.apache.hc.client5.http.auth.AuthScope) CloseableHttpResponse(org.apache.hc.client5.http.impl.classic.CloseableHttpResponse) UsernamePasswordCredentials(org.apache.hc.client5.http.auth.UsernamePasswordCredentials)

Aggregations

BasicCredentialsProvider (org.apache.hc.client5.http.impl.auth.BasicCredentialsProvider)16 AuthScope (org.apache.hc.client5.http.auth.AuthScope)13 UsernamePasswordCredentials (org.apache.hc.client5.http.auth.UsernamePasswordCredentials)11 CloseableHttpClient (org.apache.hc.client5.http.impl.classic.CloseableHttpClient)8 CloseableHttpResponse (org.apache.hc.client5.http.impl.classic.CloseableHttpResponse)6 HttpHost (org.apache.hc.core5.http.HttpHost)6 HttpGet (org.apache.hc.client5.http.classic.methods.HttpGet)5 URI (java.net.URI)3 Credentials (org.apache.hc.client5.http.auth.Credentials)3 CredentialsStore (org.apache.hc.client5.http.auth.CredentialsStore)3 RequestConfig (org.apache.hc.client5.http.config.RequestConfig)3 SSLContext (javax.net.ssl.SSLContext)2 ServletException (javax.servlet.ServletException)2 AuthCache (org.apache.hc.client5.http.auth.AuthCache)2 CredentialsProvider (org.apache.hc.client5.http.auth.CredentialsProvider)2 BasicAuthCache (org.apache.hc.client5.http.impl.auth.BasicAuthCache)2 HttpClientContext (org.apache.hc.client5.http.protocol.HttpClientContext)2 DefaultConnectionReuseStrategy (org.apache.hc.core5.http.impl.DefaultConnectionReuseStrategy)2 URIBuilder (org.apache.hc.core5.net.URIBuilder)2 Exceptions.throwUnchecked (com.github.tomakehurst.wiremock.common.Exceptions.throwUnchecked)1