use of org.graylog.shaded.elasticsearch7.org.apache.http.client.CredentialsProvider in project dq-easy-cloud by dq-open-cloud.
the class DqHttpRequestTemplateBO method createCredentialsProvider.
/**
* 创建凭据提供程序
*
* @param configStorage
* 请求配置
* @return 凭据提供程序
*/
public CredentialsProvider createCredentialsProvider(DqHttpConfigStorageDTO configStorage) {
if (DqStringUtils.isNotEmpty(configStorage.getHttpProxyHost())) {
// http代理地址设置
httpProxy = new HttpHost(configStorage.getHttpProxyHost(), configStorage.getHttpProxyPort());
}
if (DqStringUtils.isEmpty(configStorage.getAuthUsername())) {
return null;
}
// 需要用户认证的代理服务器
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(configStorage.getAuthUsername(), configStorage.getAuthPassword()));
return credsProvider;
}
use of org.graylog.shaded.elasticsearch7.org.apache.http.client.CredentialsProvider in project SmartApplianceEnabler by camueller.
the class HttpTransactionExecutor method sendHttpRequest.
/**
* Send a HTTP request whose response has to be closed by the caller!
* @param url
* @return
*/
protected CloseableHttpResponse sendHttpRequest(String url, String data, ContentType contentType, String username, String password) {
HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
if (username != null && password != null) {
CredentialsProvider provider = new BasicCredentialsProvider();
UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(username, password);
provider.setCredentials(AuthScope.ANY, credentials);
httpClientBuilder.setDefaultCredentialsProvider(provider);
}
CloseableHttpClient client = httpClientBuilder.build();
logger.debug("Sending HTTP request");
logger.debug("url=" + url);
logger.debug("data=" + data);
logger.debug("contentType=" + contentType);
logger.debug("username=" + username);
logger.debug("password=" + password);
try {
HttpRequestBase request = null;
if (data != null) {
request = new HttpPost(url);
((HttpPost) request).setEntity(new StringEntity(data, contentType));
} else {
request = new HttpGet(url);
}
CloseableHttpResponse response = client.execute(request);
int responseCode = response.getStatusLine().getStatusCode();
logger.debug("Response code is " + responseCode);
return response;
} catch (IOException e) {
logger.error("Error executing HTTP request.", e);
return null;
}
}
use of org.graylog.shaded.elasticsearch7.org.apache.http.client.CredentialsProvider in project sling by apache.
the class PreemptiveAuthInterceptor method process.
public void process(HttpRequest request, HttpContext context) throws HttpException, IOException {
AuthState authState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE);
CredentialsProvider credsProvider = (CredentialsProvider) context.getAttribute(ClientContext.CREDS_PROVIDER);
HttpHost targetHost = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
// If not auth scheme has been initialized yet
if (authState.getAuthScheme() == null) {
AuthScope authScope = new AuthScope(targetHost.getHostName(), targetHost.getPort());
// Obtain credentials matching the target host
Credentials creds = credsProvider.getCredentials(authScope);
// If found, generate BasicScheme preemptively
if (creds != null) {
authState.setAuthScheme(new BasicScheme());
authState.setCredentials(creds);
}
}
}
use of org.graylog.shaded.elasticsearch7.org.apache.http.client.CredentialsProvider in project opennms by OpenNMS.
the class ScvEnabledRestClientImpl method getResponse.
// Setup a client with pre-emptive authentication
private CloseableHttpResponse getResponse(HttpGet httpget) throws Exception {
CloseableHttpResponse response = null;
HttpHost target = new HttpHost(url.getHost(), url.getPort(), url.getProtocol());
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(new AuthScope(target.getHostName(), target.getPort()), getCredentials());
CloseableHttpClient httpclient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();
AuthCache authCache = new BasicAuthCache();
BasicScheme basicAuth = new BasicScheme();
authCache.put(target, basicAuth);
HttpClientContext localContext = HttpClientContext.create();
localContext.setAuthCache(authCache);
response = httpclient.execute(target, httpget, localContext);
return response;
}
use of org.graylog.shaded.elasticsearch7.org.apache.http.client.CredentialsProvider 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);
}
Aggregations