use of org.graylog.shaded.elasticsearch7.org.apache.http.client.CredentialsProvider in project opennms by OpenNMS.
the class HttpClientWrapper method enablePreemptiveAuth.
protected void enablePreemptiveAuth(final HttpClientBuilder builder) {
/**
* Add an HttpRequestInterceptor that will perform preemptive authentication
* @see http://hc.apache.org/httpcomponents-client-4.0.1/tutorial/html/authentication.html
*/
final HttpRequestInterceptor preemptiveAuth = new HttpRequestInterceptor() {
@Override
public void process(final HttpRequest request, final HttpContext context) throws IOException {
if (context instanceof HttpClientContext) {
final HttpClientContext clientContext = (HttpClientContext) context;
final AuthState authState = clientContext.getTargetAuthState();
final CredentialsProvider credsProvider = clientContext.getCredentialsProvider();
final HttpHost targetHost = clientContext.getTargetHost();
// If not authentication scheme has been initialized yet
if (authState.getAuthScheme() == null) {
final AuthScope authScope = new AuthScope(targetHost.getHostName(), targetHost.getPort());
// Obtain credentials matching the target host
final Credentials creds = credsProvider.getCredentials(authScope);
// If found, generate BasicScheme preemptively
if (creds != null) {
authState.update(new BasicScheme(), creds);
}
}
} else {
throw new IllegalArgumentException("Not sure how to handle a non-HttpClientContext context.");
}
}
};
builder.addInterceptorFirst(preemptiveAuth);
}
use of org.graylog.shaded.elasticsearch7.org.apache.http.client.CredentialsProvider 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.graylog.shaded.elasticsearch7.org.apache.http.client.CredentialsProvider 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.graylog.shaded.elasticsearch7.org.apache.http.client.CredentialsProvider in project vorto by eclipse.
the class RestClient method getProxyConfiguration.
private ProxyConfiguration getProxyConfiguration() {
IProxyService proxyService = getProxyService();
IProxyData[] proxyDataForHost = proxyService.select(java.net.URI.create(connectionInfo.getUrl()));
CredentialsProvider credsProvider = new BasicCredentialsProvider();
RequestConfig.Builder configBuilder = RequestConfig.custom();
for (IProxyData data : proxyDataForHost) {
if (!Strings.isNullOrEmpty(data.getHost())) {
HttpHost proxyConfig = new HttpHost(data.getHost(), data.getPort(), data.getType());
configBuilder.setProxy(proxyConfig);
if (!Strings.isNullOrEmpty(data.getUserId())) {
credsProvider.setCredentials(new AuthScope(data.getHost(), data.getPort()), new UsernamePasswordCredentials(data.getUserId(), data.getPassword()));
}
}
}
return new ProxyConfiguration(configBuilder.build(), credsProvider);
}
use of org.graylog.shaded.elasticsearch7.org.apache.http.client.CredentialsProvider in project eap-additional-testsuite by jboss-set.
the class IdentityPropagationAuthenticationTestCase method testIdentityPropagationAuthentication.
@Test
public void testIdentityPropagationAuthentication(@ArquillianResource URL url) throws Exception {
HttpGet httpGet = new HttpGet(url.toExternalForm() + "IdentityPropagationServlet/");
HttpResponse response = null;
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(new AuthScope(url.getHost(), url.getPort()), new UsernamePasswordCredentials(USER, PASSWORD));
try (CloseableHttpClient httpclient = HttpClients.custom().setDefaultCredentialsProvider(credentialsProvider).build()) {
response = httpclient.execute(httpGet);
}
assertNotNull("Response is 'null', we expected non-null response!", response);
assertEquals(200, response.getStatusLine().getStatusCode());
}
Aggregations