Search in sources :

Example 56 with BasicCredentialsProvider

use of org.apache.http.impl.client.BasicCredentialsProvider in project sidewinder by srotya.

the class TestInMemoryByzantineDefaultsAuthenticated method beforeClass.

@BeforeClass
public static void beforeClass() {
    provider = new BasicCredentialsProvider();
    UsernamePasswordCredentials credentials = new UsernamePasswordCredentials("admin", "admin");
    provider.setCredentials(AuthScope.ANY, credentials);
}
Also used : BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials) BeforeClass(org.junit.BeforeClass)

Example 57 with BasicCredentialsProvider

use of org.apache.http.impl.client.BasicCredentialsProvider in project JFramework by gugumall.

the class JHttp method createClient.

/**
 * @param timeout in milliseconds
 * @return
 */
public HttpClient createClient(int timeout, String host, int port, String scheme, String username, String password) {
    HttpHost proxy = new HttpHost(host, port, scheme == null ? "http" : scheme);
    CredentialsProvider credsProvider = new BasicCredentialsProvider();
    credsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password));
    CloseableHttpClient client = HttpClients.custom().setConnectionManager(poolingmgr).setProxy(proxy).setDefaultCredentialsProvider(credsProvider).build();
    int retries = default_retries;
    if (JUtilMath.isInt(AppConfig.getPara("HTTP", "retries"))) {
        retries = Integer.parseInt(AppConfig.getPara("HTTP", "retries"));
    }
    RequestConfig requestConfig = RequestConfig.custom().setMaxRedirects(retries).setSocketTimeout(timeout).setConnectTimeout(timeout).build();
    configOfClients.put(client.toString(), requestConfig);
    return client;
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) RequestConfig(org.apache.http.client.config.RequestConfig) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) HttpHost(org.apache.http.HttpHost) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) CredentialsProvider(org.apache.http.client.CredentialsProvider) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials)

Example 58 with BasicCredentialsProvider

use of org.apache.http.impl.client.BasicCredentialsProvider in project jenkin-qtest-plugin by QASymphony.

the class HttpClientUtils method setHttpProxy.

private static void setHttpProxy(HttpClientBuilder httpClientBuilder, String hostUrl) {
    ProxyConfiguration proxyConfig = Jenkins.getInstance().proxy;
    if (proxyConfig != null) {
        List<Pattern> proxyHostPatterns = proxyConfig.getNoProxyHostPatterns();
        if (isUrlMatchWithNoProxyHost(hostUrl, proxyHostPatterns) == true) {
            return;
        }
        HttpHost proxy = new HttpHost(proxyConfig.name, proxyConfig.port);
        String username = proxyConfig.getUserName();
        String password = proxyConfig.getPassword();
        Credentials credentials;
        if (username != null && StringUtils.isNotEmpty(username) == true) {
            credentials = new UsernamePasswordCredentials(username, password);
        } else {
            credentials = new UsernamePasswordCredentials("", "");
        }
        AuthScope authScope = new AuthScope(proxyConfig.name, proxyConfig.port);
        CredentialsProvider credsProvider = new BasicCredentialsProvider();
        credsProvider.setCredentials(authScope, credentials);
        httpClientBuilder.setProxy(proxy).setDefaultCredentialsProvider(credsProvider).build();
    }
}
Also used : Pattern(java.util.regex.Pattern) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) ProxyConfiguration(hudson.ProxyConfiguration) HttpHost(org.apache.http.HttpHost) AuthScope(org.apache.http.auth.AuthScope) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) CredentialsProvider(org.apache.http.client.CredentialsProvider) Credentials(org.apache.http.auth.Credentials) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials)

Example 59 with BasicCredentialsProvider

use of org.apache.http.impl.client.BasicCredentialsProvider in project xian by happyyangyuan.

the class BasicAuthApacheHttpClient method getHttpClient.

private static HttpClient getHttpClient(String username, String password) {
    HttpClient httpClient;
    if (username != null && password != null) {
        CredentialsProvider provider = new BasicCredentialsProvider();
        UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(username, password);
        provider.setCredentials(AuthScope.ANY, credentials);
        httpClient = HttpClientBuilder.create().setDefaultCredentialsProvider(provider).build();
    } else {
        httpClient = HttpClientBuilder.create().build();
    }
    return httpClient;
}
Also used : BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) IApacheHttpClient(info.xiancloud.httpclient.apache_http.IApacheHttpClient) ApacheHttpClient(info.xiancloud.httpclient.apache_http.no_auth.ApacheHttpClient) HttpClient(org.apache.http.client.HttpClient) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) CredentialsProvider(org.apache.http.client.CredentialsProvider) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials)

Example 60 with BasicCredentialsProvider

use of org.apache.http.impl.client.BasicCredentialsProvider in project tutorials by eugenp.

the class HttpClientAdvancedConfigurationIntegrationTest method givenServerThatIsBehindAuthorizationProxy_whenClientSendRequest_shouldAuthorizeProperly.

@Test
public void givenServerThatIsBehindAuthorizationProxy_whenClientSendRequest_shouldAuthorizeProperly() throws IOException {
    // given
    proxyMock.stubFor(get(urlMatching("/private")).willReturn(aResponse().proxiedFrom("http://localhost:8089/")));
    serviceMock.stubFor(get(urlEqualTo("/private")).willReturn(aResponse().withStatus(200)));
    HttpHost proxy = new HttpHost("localhost", 8090);
    DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxy);
    // Client credentials
    CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
    credentialsProvider.setCredentials(new AuthScope(proxy), new UsernamePasswordCredentials("username_admin", "secret_password"));
    // Create AuthCache instance
    AuthCache authCache = new BasicAuthCache();
    // Generate BASIC scheme object and add it to the local auth cache
    BasicScheme basicAuth = new BasicScheme();
    authCache.put(proxy, basicAuth);
    HttpClientContext context = HttpClientContext.create();
    context.setCredentialsProvider(credentialsProvider);
    context.setAuthCache(authCache);
    HttpClient httpclient = HttpClients.custom().setRoutePlanner(routePlanner).setDefaultCredentialsProvider(credentialsProvider).build();
    // when
    final HttpGet httpGet = new HttpGet("http://localhost:8089/private");
    HttpResponse response = httpclient.execute(httpGet, context);
    // then
    assertEquals(response.getStatusLine().getStatusCode(), 200);
    proxyMock.verify(getRequestedFor(urlEqualTo("/private")).withHeader("Authorization", containing("Basic")));
    serviceMock.verify(getRequestedFor(urlEqualTo("/private")));
}
Also used : BasicScheme(org.apache.http.impl.auth.BasicScheme) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) HttpGet(org.apache.http.client.methods.HttpGet) AuthCache(org.apache.http.client.AuthCache) BasicAuthCache(org.apache.http.impl.client.BasicAuthCache) DefaultProxyRoutePlanner(org.apache.http.impl.conn.DefaultProxyRoutePlanner) HttpResponse(org.apache.http.HttpResponse) HttpClientContext(org.apache.http.client.protocol.HttpClientContext) 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) HttpHost(org.apache.http.HttpHost) HttpClient(org.apache.http.client.HttpClient) AuthScope(org.apache.http.auth.AuthScope) Test(org.junit.Test)

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