use of org.graylog.shaded.elasticsearch7.org.apache.http.client.CredentialsProvider in project stdlib by petergeneric.
the class PreemptiveBasicAuthInterceptor method process.
public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException {
final AuthState state = (AuthState) context.getAttribute(HttpClientContext.TARGET_AUTH_STATE);
// Try to initialise an auth scheme if one is not already set
if (state.getAuthScheme() == null) {
CredentialsProvider credentialsProvider = (CredentialsProvider) context.getAttribute(HttpClientContext.CREDS_PROVIDER);
HttpHost host = (HttpHost) context.getAttribute(HttpCoreContext.HTTP_TARGET_HOST);
final Credentials credentials = credentialsProvider.getCredentials(new AuthScope(host));
if (credentials == null)
throw new HttpException("No credentials for preemptive authentication against: " + host);
else
state.update(new BasicScheme(), credentials);
}
}
use of org.graylog.shaded.elasticsearch7.org.apache.http.client.CredentialsProvider in project briefcase by opendatakit.
the class WebUtils method addCredentials.
private static final void addCredentials(HttpClientContext localContext, Credentials c, String host) {
CredentialsProvider credsProvider = localContext.getCredentialsProvider();
List<AuthScope> asList = buildAuthScopes(host);
for (AuthScope a : asList) {
credsProvider.setCredentials(a, c);
}
}
use of org.graylog.shaded.elasticsearch7.org.apache.http.client.CredentialsProvider in project sagacity-sqltoy by chenrenfei.
the class HttpClientUtils method doPost.
/**
* @todo 执行post请求
* @param sqltoyContext
* @param nosqlConfig
* @param url
* @param jsonObject
* @return
* @throws Exception
*/
public static JSONObject doPost(SqlToyContext sqltoyContext, NoSqlConfigModel nosqlConfig, Object postValue) throws Exception {
ElasticEndpoint esConfig = sqltoyContext.getElasticEndpoint(nosqlConfig.getUrl());
if (esConfig.getUrl() == null)
throw new Exception("请正确配置sqltoyContext elasticConfigs 指定es的服务地址!");
String charset = (nosqlConfig.getCharset() == null) ? CHARSET : nosqlConfig.getCharset();
HttpEntity httpEntity = new StringEntity(nosqlConfig.isSqlMode() ? postValue.toString() : JSON.toJSONString(postValue), charset);
((StringEntity) httpEntity).setContentEncoding(charset);
((StringEntity) httpEntity).setContentType(CONTENT_TYPE);
String realUrl;
// 返回结果
HttpEntity reponseEntity = null;
if (esConfig.getRestClient() != null) {
realUrl = wrapUrl(esConfig.getPath(), nosqlConfig);
if (sqltoyContext.isDebug())
logger.debug("esRestClient执行:URL=[{}],Path={},执行的JSON=[{}]", esConfig.getUrl(), realUrl, JSON.toJSONString(postValue));
// 默认采用post请求
RestClient restClient = null;
try {
restClient = esConfig.getRestClient();
Response response = restClient.performRequest("POST", realUrl, Collections.<String, String>emptyMap(), httpEntity);
reponseEntity = response.getEntity();
} catch (Exception e) {
throw e;
} finally {
if (restClient != null)
restClient.close();
}
} else {
realUrl = wrapUrl(esConfig.getUrl(), nosqlConfig);
HttpPost httpPost = new HttpPost(realUrl);
if (sqltoyContext.isDebug())
logger.debug("httpClient执行URL=[{}],执行的JSON=[{}]", realUrl, JSON.toJSONString(postValue));
httpPost.setEntity(httpEntity);
// 设置connection是否自动关闭
httpPost.setHeader("Connection", "close");
// 自定义超时
if (nosqlConfig.getRequestTimeout() != 30000 || nosqlConfig.getConnectTimeout() != 10000 || nosqlConfig.getSocketTimeout() != 180000) {
httpPost.setConfig(RequestConfig.custom().setConnectionRequestTimeout(nosqlConfig.getRequestTimeout()).setConnectTimeout(nosqlConfig.getConnectTimeout()).setSocketTimeout(nosqlConfig.getSocketTimeout()).build());
} else
httpPost.setConfig(requestConfig);
CloseableHttpClient client = null;
try {
if (StringUtil.isNotBlank(esConfig.getUsername()) && StringUtil.isNotBlank(esConfig.getPassword())) {
// 凭据提供器
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(AuthScope.ANY, // 认证用户名和密码
new UsernamePasswordCredentials(esConfig.getUsername(), esConfig.getPassword()));
client = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();
} else
client = HttpClients.createDefault();
HttpResponse response = client.execute(httpPost);
reponseEntity = response.getEntity();
} catch (Exception e) {
throw e;
}
}
String result = null;
if (reponseEntity != null) {
result = EntityUtils.toString(reponseEntity, nosqlConfig.getCharset());
if (sqltoyContext.isDebug())
logger.debug("result={}", result);
}
if (StringUtil.isBlank(result))
return null;
// 将结果转换为JSON对象
JSONObject json = JSON.parseObject(result);
// 存在错误
if (json.containsKey("error")) {
String errorMessage = JSON.toJSONString(json.getJSONObject("error").getJSONArray("root_cause").get(0));
logger.error("elastic查询失败,URL:[{}],错误信息:[{}]", nosqlConfig.getUrl(), errorMessage);
throw new Exception("ElasticSearch查询失败,错误信息:" + errorMessage);
}
return json;
}
use of org.graylog.shaded.elasticsearch7.org.apache.http.client.CredentialsProvider in project carina by qaprosoft.
the class RestTemplateBuilder method build.
public RestTemplate build() {
if (!isUseDefaultJsonMessageConverter) {
HttpMessageConverter<?> httpMessageConverter = Iterables.tryFind(restTemplate.getMessageConverters(), new Predicate<HttpMessageConverter<?>>() {
@Override
public boolean apply(HttpMessageConverter<?> input) {
return input instanceof MappingJackson2HttpMessageConverter;
}
}).orNull();
restTemplate.getMessageConverters().remove(httpMessageConverter);
}
restTemplate.getMessageConverters().addAll(httpMessageConverters);
if (isDisableSslChecking) {
restTemplate.setRequestFactory(new DisabledSslClientHttpRequestFactory());
}
if (isUseBasicAuth) {
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(basicAuthUsername, basicAuthPassword));
HttpClient httpClient = HttpClients.custom().setDefaultCredentialsProvider(credentialsProvider).build();
restTemplate.setRequestFactory(new HttpComponentsClientHttpRequestFactory(httpClient));
}
return restTemplate;
}
use of org.graylog.shaded.elasticsearch7.org.apache.http.client.CredentialsProvider in project service-proxy by membrane.
the class AssertUtils method getAuthenticatingHttpClient.
private static HttpClient getAuthenticatingHttpClient(String host, int port, String user, String pass) {
Credentials defaultcreds = new UsernamePasswordCredentials(user, pass);
BasicCredentialsProvider bcp = new BasicCredentialsProvider();
bcp.setCredentials(new AuthScope(host, port, AuthScope.ANY_REALM), defaultcreds);
HttpRequestInterceptor preemptiveAuth = new HttpRequestInterceptor() {
public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException {
AuthState authState = (AuthState) context.getAttribute(HttpClientContext.TARGET_AUTH_STATE);
CredentialsProvider credsProvider = (CredentialsProvider) context.getAttribute(HttpClientContext.CREDS_PROVIDER);
HttpHost targetHost = (HttpHost) context.getAttribute(HttpCoreContext.HTTP_TARGET_HOST);
if (authState.getAuthScheme() == null) {
AuthScope authScope = new AuthScope(targetHost.getHostName(), targetHost.getPort());
Credentials creds = credsProvider.getCredentials(authScope);
if (creds != null) {
authState.update(new BasicScheme(), creds);
}
}
}
};
HttpClient hc = HttpClientBuilder.create().setDefaultCookieStore(new BasicCookieStore()).setDefaultCredentialsProvider(bcp).addInterceptorFirst(preemptiveAuth).build();
return hc;
}
Aggregations