use of org.apache.http.auth.UsernamePasswordCredentials 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.auth.UsernamePasswordCredentials 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.auth.UsernamePasswordCredentials 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")));
}
use of org.apache.http.auth.UsernamePasswordCredentials in project tutorials by eugenp.
the class HttpClientBasicPostLiveTest method givenAuth_whenExecutingPostRequestWithBody_thenNoExceptions.
@Test
public final void givenAuth_whenExecutingPostRequestWithBody_thenNoExceptions() throws IOException, AuthenticationException {
final HttpPost request = new HttpPost(SAMPLE_URL);
request.setEntity(new StringEntity("in the body of the POST"));
final UsernamePasswordCredentials creds = new UsernamePasswordCredentials("username", "password");
request.addHeader(new BasicScheme().authenticate(creds, request, null));
instance.execute(request);
}
use of org.apache.http.auth.UsernamePasswordCredentials in project tutorials by eugenp.
the class HttpClientSandboxLiveTest method givenGetRequestExecuted_whenAnalyzingTheResponse_thenCorrectStatusCode.
@Test
public final void givenGetRequestExecuted_whenAnalyzingTheResponse_thenCorrectStatusCode() throws IOException {
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
final AuthScope authscp = new AuthScope("localhost", 8080);
credentialsProvider.setCredentials(authscp, new UsernamePasswordCredentials("user1", "user1Pass"));
final CloseableHttpClient client = HttpClientBuilder.create().setDefaultCredentialsProvider(credentialsProvider).build();
final HttpGet httpGet = new HttpGet("http://localhost:8080/spring-security-rest-basic-auth/api/foos/1");
final CloseableHttpResponse response = client.execute(httpGet);
System.out.println(response.getStatusLine());
ResponseUtil.closeResponse(response);
}
Aggregations