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);
}
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;
}
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();
}
}
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;
}
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")));
}
Aggregations