Search in sources :

Example 11 with AuthScope

use of org.apache.hc.client5.http.auth.AuthScope in project geo-platform by geosdi.

the class DigestPreemptiveSecurityConnector method bindCredentials.

/**
 * @param targetHost
 * @param targetURI
 * @throws Exception
 */
@Override
protected void bindCredentials(@Nonnull(when = NEVER) HttpHost targetHost, @Nonnull(when = NEVER) URI targetURI) throws Exception {
    super.bindCredentials(targetHost, targetURI);
    BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
    credentialsProvider.setCredentials(new AuthScope(targetHost), this.usernamePasswordCredentials);
    localContext.setCredentialsProvider(credentialsProvider);
}
Also used : BasicCredentialsProvider(org.apache.hc.client5.http.impl.auth.BasicCredentialsProvider) AuthScope(org.apache.hc.client5.http.auth.AuthScope)

Example 12 with AuthScope

use of org.apache.hc.client5.http.auth.AuthScope 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 13 with AuthScope

use of org.apache.hc.client5.http.auth.AuthScope 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 14 with AuthScope

use of org.apache.hc.client5.http.auth.AuthScope 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)

Example 15 with AuthScope

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

the class AsyncClientAuthentication 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()));
    final CloseableHttpAsyncClient httpclient = HttpAsyncClients.custom().setDefaultCredentialsProvider(credsProvider).build();
    httpclient.start();
    final SimpleHttpRequest request = SimpleRequestBuilder.get("http://httpbin.org/basic-auth/user/passwd").build();
    System.out.println("Executing request " + request);
    final Future<SimpleHttpResponse> future = httpclient.execute(SimpleRequestProducer.create(request), SimpleResponseConsumer.create(), new FutureCallback<SimpleHttpResponse>() {

        @Override
        public void completed(final SimpleHttpResponse response) {
            System.out.println(request + "->" + new StatusLine(response));
            System.out.println(response.getBody());
        }

        @Override
        public void failed(final Exception ex) {
            System.out.println(request + "->" + ex);
        }

        @Override
        public void cancelled() {
            System.out.println(request + " cancelled");
        }
    });
    future.get();
    System.out.println("Shutting down");
    httpclient.close(CloseMode.GRACEFUL);
}
Also used : StatusLine(org.apache.hc.core5.http.message.StatusLine) BasicCredentialsProvider(org.apache.hc.client5.http.impl.auth.BasicCredentialsProvider) AuthScope(org.apache.hc.client5.http.auth.AuthScope) CloseableHttpAsyncClient(org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient) SimpleHttpRequest(org.apache.hc.client5.http.async.methods.SimpleHttpRequest) SimpleHttpResponse(org.apache.hc.client5.http.async.methods.SimpleHttpResponse) UsernamePasswordCredentials(org.apache.hc.client5.http.auth.UsernamePasswordCredentials)

Aggregations

AuthScope (org.apache.hc.client5.http.auth.AuthScope)16 UsernamePasswordCredentials (org.apache.hc.client5.http.auth.UsernamePasswordCredentials)14 BasicCredentialsProvider (org.apache.hc.client5.http.impl.auth.BasicCredentialsProvider)13 Credentials (org.apache.hc.client5.http.auth.Credentials)6 CloseableHttpClient (org.apache.hc.client5.http.impl.classic.CloseableHttpClient)5 HttpHost (org.apache.hc.core5.http.HttpHost)5 CredentialsStore (org.apache.hc.client5.http.auth.CredentialsStore)3 HttpGet (org.apache.hc.client5.http.classic.methods.HttpGet)3 CloseableHttpResponse (org.apache.hc.client5.http.impl.classic.CloseableHttpResponse)3 AuthCache (org.apache.hc.client5.http.auth.AuthCache)2 NTCredentials (org.apache.hc.client5.http.auth.NTCredentials)2 RequestConfig (org.apache.hc.client5.http.config.RequestConfig)2 BasicAuthCache (org.apache.hc.client5.http.impl.auth.BasicAuthCache)2 Exceptions.throwUnchecked (com.github.tomakehurst.wiremock.common.Exceptions.throwUnchecked)1 LocalNotifier.notifier (com.github.tomakehurst.wiremock.common.LocalNotifier.notifier)1 ProxySettings (com.github.tomakehurst.wiremock.common.ProxySettings)1 NO_PROXY (com.github.tomakehurst.wiremock.common.ProxySettings.NO_PROXY)1 KeyStoreSettings (com.github.tomakehurst.wiremock.common.ssl.KeyStoreSettings)1 NO_STORE (com.github.tomakehurst.wiremock.common.ssl.KeyStoreSettings.NO_STORE)1 RequestMethod (com.github.tomakehurst.wiremock.http.RequestMethod)1