Search in sources :

Example 81 with CredentialsProvider

use of org.apache.http.client.CredentialsProvider in project ddf by codice.

the class TestSecurity method createBasicAuth.

private CredentialsProvider createBasicAuth(String username, String password) {
    CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
    UsernamePasswordCredentials usernamePasswordCredentials = new UsernamePasswordCredentials(username, password);
    credentialsProvider.setCredentials(AuthScope.ANY, usernamePasswordCredentials);
    return credentialsProvider;
}
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 82 with CredentialsProvider

use of org.apache.http.client.CredentialsProvider in project sling by apache.

the class SmokeIT method newClient.

private CloseableHttpClient newClient() {
    CredentialsProvider credsProvider = new BasicCredentialsProvider();
    UsernamePasswordCredentials creds = new UsernamePasswordCredentials("admin", "admin");
    credsProvider.setCredentials(new AuthScope("localhost", LAUNCHPAD_PORT), creds);
    return HttpClientBuilder.create().setDefaultCredentialsProvider(credsProvider).build();
}
Also used : BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) AuthScope(org.apache.http.auth.AuthScope) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) CredentialsProvider(org.apache.http.client.CredentialsProvider) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials)

Example 83 with CredentialsProvider

use of org.apache.http.client.CredentialsProvider in project cas by apereo.

the class HttpUtils method prepareCredentialsIfNeeded.

/**
 * Prepare credentials if needed.
 *
 * @param builder           the builder
 * @param basicAuthUsername username for basic auth
 * @param basicAuthPassword password for basic auth
 * @return the http client builder
 */
private static HttpClientBuilder prepareCredentialsIfNeeded(final HttpClientBuilder builder, final String basicAuthUsername, final String basicAuthPassword) {
    if (StringUtils.isNotBlank(basicAuthUsername) && StringUtils.isNotBlank(basicAuthPassword)) {
        final CredentialsProvider provider = new BasicCredentialsProvider();
        final UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(basicAuthUsername, basicAuthPassword);
        provider.setCredentials(AuthScope.ANY, credentials);
        return builder.setDefaultCredentialsProvider(provider);
    }
    return builder;
}
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 84 with CredentialsProvider

use of org.apache.http.client.CredentialsProvider in project syncany by syncany.

the class ApplicationLink method createHttpClient.

private CloseableHttpClient createHttpClient() {
    RequestConfig.Builder requestConfigBuilder = RequestConfig.custom().setSocketTimeout(2000).setConnectTimeout(2000).setRedirectsEnabled(false);
    HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
    // do we use a https proxy?
    String proxyHost = System.getProperty("https.proxyHost");
    String proxyPortStr = System.getProperty("https.proxyPort");
    String proxyUser = System.getProperty("https.proxyUser");
    String proxyPassword = System.getProperty("https.proxyPassword");
    if (proxyHost != null && proxyPortStr != null) {
        try {
            Integer proxyPort = Integer.parseInt(proxyPortStr);
            requestConfigBuilder.setProxy(new HttpHost(proxyHost, proxyPort));
            logger.log(Level.INFO, "Using proxy: " + proxyHost + ":" + proxyPort);
            if (proxyUser != null && proxyPassword != null) {
                logger.log(Level.INFO, "Proxy required credentials; using '" + proxyUser + "' (username) and *** (hidden password)");
                CredentialsProvider credsProvider = new BasicCredentialsProvider();
                credsProvider.setCredentials(new AuthScope(proxyHost, proxyPort), new UsernamePasswordCredentials(proxyUser, proxyPassword));
                httpClientBuilder.setDefaultCredentialsProvider(credsProvider);
            }
        } catch (NumberFormatException e) {
            logger.log(Level.WARNING, "Invalid proxy settings found. Not using proxy.", e);
        }
    }
    httpClientBuilder.setDefaultRequestConfig(requestConfigBuilder.build());
    return httpClientBuilder.build();
}
Also used : RequestConfig(org.apache.http.client.config.RequestConfig) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) HttpHost(org.apache.http.HttpHost) 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) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials)

Example 85 with CredentialsProvider

use of org.apache.http.client.CredentialsProvider in project SmartApplianceEnabler by camueller.

the class HttpTransactionExecutor method sendHttpRequest.

/**
 * Send a HTTP request whose response has to be closed by the caller!
 * @param url
 * @return
 */
protected CloseableHttpResponse sendHttpRequest(String url, String data, ContentType contentType, String username, String password) {
    HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
    if (username != null && password != null) {
        CredentialsProvider provider = new BasicCredentialsProvider();
        UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(username, password);
        provider.setCredentials(AuthScope.ANY, credentials);
        httpClientBuilder.setDefaultCredentialsProvider(provider);
    }
    CloseableHttpClient client = httpClientBuilder.build();
    logger.debug("{}: Sending HTTP request", applianceId);
    logger.debug("{}: url={}", applianceId, url);
    logger.debug("{}: data={}", applianceId, data);
    logger.debug("{}: contentType={}", applianceId, contentType);
    logger.debug("{}: username={}", applianceId, username);
    logger.debug("{}: password={}", applianceId, password);
    try {
        HttpRequestBase request = null;
        if (data != null) {
            request = new HttpPost(url);
            ((HttpPost) request).setEntity(new StringEntity(data, contentType));
        } else {
            request = new HttpGet(url);
        }
        CloseableHttpResponse response = client.execute(request);
        int responseCode = response.getStatusLine().getStatusCode();
        logger.debug("{}: Response code is {}", applianceId, responseCode);
        return response;
    } catch (IOException e) {
        logger.error("{}: Error executing HTTP request.", applianceId, e);
        return null;
    }
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpPost(org.apache.http.client.methods.HttpPost) StringEntity(org.apache.http.entity.StringEntity) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) HttpRequestBase(org.apache.http.client.methods.HttpRequestBase) HttpGet(org.apache.http.client.methods.HttpGet) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) HttpClientBuilder(org.apache.http.impl.client.HttpClientBuilder) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) CredentialsProvider(org.apache.http.client.CredentialsProvider) IOException(java.io.IOException) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials)

Aggregations

CredentialsProvider (org.apache.http.client.CredentialsProvider)92 BasicCredentialsProvider (org.apache.http.impl.client.BasicCredentialsProvider)65 UsernamePasswordCredentials (org.apache.http.auth.UsernamePasswordCredentials)57 AuthScope (org.apache.http.auth.AuthScope)51 HttpHost (org.apache.http.HttpHost)28 HttpGet (org.apache.http.client.methods.HttpGet)19 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)17 Test (org.junit.Test)17 HttpResponse (org.apache.http.HttpResponse)16 Credentials (org.apache.http.auth.Credentials)16 IOException (java.io.IOException)13 HttpClientContext (org.apache.http.client.protocol.HttpClientContext)12 BasicScheme (org.apache.http.impl.auth.BasicScheme)12 Header (org.apache.http.Header)11 HttpRequest (org.apache.http.HttpRequest)10 AuthCache (org.apache.http.client.AuthCache)10 BasicAuthCache (org.apache.http.impl.client.BasicAuthCache)10 HttpClientBuilder (org.apache.http.impl.client.HttpClientBuilder)10 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)9 HttpEntity (org.apache.http.HttpEntity)8