use of org.apache.http.impl.client.BasicCredentialsProvider in project geode by apache.
the class GeodeRestClient method doRequest.
public HttpResponse doRequest(HttpRequestBase request, String username, String password) throws Exception {
HttpHost targetHost = new HttpHost(bindAddress, restPort, protocol);
HttpClientBuilder clientBuilder = HttpClients.custom();
HttpClientContext clientContext = HttpClientContext.create();
// configures the clientBuilder and clientContext
if (username != null) {
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(new AuthScope(targetHost.getHostName(), targetHost.getPort()), new UsernamePasswordCredentials(username, password));
clientBuilder.setDefaultCredentialsProvider(credsProvider);
}
if (useHttps) {
SSLContext ctx = SSLContext.getInstance("TLS");
ctx.init(new KeyManager[0], new TrustManager[] { new DefaultTrustManager() }, new SecureRandom());
clientBuilder.setSSLContext(ctx);
clientBuilder.setSSLHostnameVerifier(new NoopHostnameVerifier());
}
return clientBuilder.build().execute(targetHost, request, clientContext);
}
use of org.apache.http.impl.client.BasicCredentialsProvider in project metron by apache.
the class TaxiiHandler method createContext.
private static HttpClientContext createContext(URL endpoint, String username, String password, int port) {
HttpClientContext context = null;
HttpHost target = new HttpHost(endpoint.getHost(), port, endpoint.getProtocol());
if (username != null && password != null) {
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(new AuthScope(target.getHostName(), target.getPort()), new UsernamePasswordCredentials(username, password));
// http://hc.apache.org/httpcomponents-client-ga/tutorial/html/authentication.html
AuthCache authCache = new BasicAuthCache();
authCache.put(target, new BasicScheme());
// Add AuthCache to the execution context
context = HttpClientContext.create();
context.setCredentialsProvider(credsProvider);
context.setAuthCache(authCache);
} else {
context = null;
}
return context;
}
use of org.apache.http.impl.client.BasicCredentialsProvider in project Lucee by lucee.
the class HTTPEngine4Impl method setCredentials.
public static BasicHttpContext setCredentials(HttpClientBuilder builder, HttpHost httpHost, String username, String password, boolean preAuth) {
// set Username and Password
if (!StringUtil.isEmpty(username, true)) {
if (password == null)
password = "";
CredentialsProvider cp = new BasicCredentialsProvider();
builder.setDefaultCredentialsProvider(cp);
cp.setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT), new UsernamePasswordCredentials(username, password));
BasicHttpContext httpContext = new BasicHttpContext();
if (preAuth) {
AuthCache authCache = new BasicAuthCache();
authCache.put(httpHost, new BasicScheme());
httpContext.setAttribute(ClientContext.AUTH_CACHE, authCache);
}
return httpContext;
}
return null;
}
use of org.apache.http.impl.client.BasicCredentialsProvider in project Lucee by lucee.
the class HTTPEngine4Impl method setNTCredentials.
public static void setNTCredentials(HttpClientBuilder builder, String username, String password, String workStation, String domain) {
// set Username and Password
if (!StringUtil.isEmpty(username, true)) {
if (password == null)
password = "";
CredentialsProvider cp = new BasicCredentialsProvider();
builder.setDefaultCredentialsProvider(cp);
cp.setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT), new NTCredentials(username, password, workStation, domain));
}
}
use of org.apache.http.impl.client.BasicCredentialsProvider in project tomee by apache.
the class AuthBeanTest method get.
private String get(final String user, final String password) {
final BasicCredentialsProvider basicCredentialsProvider = new BasicCredentialsProvider();
basicCredentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(user, password));
final CloseableHttpClient client = HttpClients.custom().setDefaultCredentialsProvider(basicCredentialsProvider).build();
final HttpHost httpHost = new HttpHost(webapp.getHost(), webapp.getPort(), webapp.getProtocol());
final AuthCache authCache = new BasicAuthCache();
final BasicScheme basicAuth = new BasicScheme();
authCache.put(httpHost, basicAuth);
final HttpClientContext context = HttpClientContext.create();
context.setAuthCache(authCache);
final HttpGet get = new HttpGet(webapp.toExternalForm() + "servlet");
CloseableHttpResponse response = null;
try {
response = client.execute(httpHost, get, context);
return response.getStatusLine().getStatusCode() + " " + EntityUtils.toString(response.getEntity());
} catch (final IOException e) {
throw new IllegalStateException(e);
} finally {
try {
IO.close(response);
} catch (final IOException e) {
// no-op
}
}
}
Aggregations