Search in sources :

Example 81 with UsernamePasswordCredentials

use of org.apache.http.auth.UsernamePasswordCredentials in project build-info by JFrogDev.

the class PreemptiveHttpClient method setProxyConfiguration.

private void setProxyConfiguration(HttpClientBuilder httpClientBuilder, ProxyConfiguration proxyConfiguration) {
    HttpHost proxy = new HttpHost(proxyConfiguration.host, proxyConfiguration.port);
    httpClientBuilder.setProxy(proxy);
    if (proxyConfiguration.username != null) {
        basicCredentialsProvider.setCredentials(new AuthScope(proxyConfiguration.host, proxyConfiguration.port), new UsernamePasswordCredentials(proxyConfiguration.username, proxyConfiguration.password));
        localContext.setCredentialsProvider(basicCredentialsProvider);
        // 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);
        localContext.setAuthCache(authCache);
    }
}
Also used : BasicScheme(org.apache.http.impl.auth.BasicScheme) AuthScope(org.apache.http.auth.AuthScope) AuthCache(org.apache.http.client.AuthCache) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials)

Example 82 with UsernamePasswordCredentials

use of org.apache.http.auth.UsernamePasswordCredentials in project epp.mpc by eclipse.

the class HttpUtil method configureProxy.

public static void configureProxy(HttpClient client, String url) {
    final IProxyData proxyData = ProxyHelper.getProxyData(url);
    if (proxyData != null && !IProxyData.SOCKS_PROXY_TYPE.equals(proxyData.getType())) {
        HttpHost proxy = new HttpHost(proxyData.getHost(), proxyData.getPort());
        client.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
        if (proxyData.isRequiresAuthentication()) {
            ((AbstractHttpClient) client).getCredentialsProvider().setCredentials(new AuthScope(proxyData.getHost(), proxyData.getPort()), new UsernamePasswordCredentials(proxyData.getUserId(), proxyData.getPassword()));
        }
    }
}
Also used : IProxyData(org.eclipse.core.net.proxy.IProxyData) HttpHost(org.apache.http.HttpHost) AuthScope(org.apache.http.auth.AuthScope) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials)

Example 83 with UsernamePasswordCredentials

use of org.apache.http.auth.UsernamePasswordCredentials in project briefcase by opendatakit.

the class WebUtils method addCredentials.

public static final void addCredentials(HttpClientContext localContext, String userEmail, char[] password, String host) {
    Credentials c = new UsernamePasswordCredentials(userEmail, new String(password));
    addCredentials(localContext, c, host);
}
Also used : Credentials(org.apache.http.auth.Credentials) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials)

Example 84 with UsernamePasswordCredentials

use of org.apache.http.auth.UsernamePasswordCredentials 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;
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) HttpEntity(org.apache.http.HttpEntity) RestClient(org.elasticsearch.client.RestClient) HttpResponse(org.apache.http.HttpResponse) ElasticEndpoint(org.sagacity.sqltoy.config.model.ElasticEndpoint) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) CredentialsProvider(org.apache.http.client.CredentialsProvider) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials) Response(org.elasticsearch.client.Response) HttpResponse(org.apache.http.HttpResponse) StringEntity(org.apache.http.entity.StringEntity) JSONObject(com.alibaba.fastjson.JSONObject)

Example 85 with UsernamePasswordCredentials

use of org.apache.http.auth.UsernamePasswordCredentials 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;
}
Also used : MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) HttpClient(org.apache.http.client.HttpClient) AbstractHttpMessageConverter(org.springframework.http.converter.AbstractHttpMessageConverter) StringHttpMessageConverter(org.springframework.http.converter.StringHttpMessageConverter) MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) HttpMessageConverter(org.springframework.http.converter.HttpMessageConverter) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) CredentialsProvider(org.apache.http.client.CredentialsProvider) HttpComponentsClientHttpRequestFactory(org.springframework.http.client.HttpComponentsClientHttpRequestFactory) DisabledSslClientHttpRequestFactory(com.qaprosoft.hockeyapp.http.resttemplate.ssl.DisabledSslClientHttpRequestFactory) Predicate(com.google.common.base.Predicate) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials)

Aggregations

UsernamePasswordCredentials (org.apache.http.auth.UsernamePasswordCredentials)337 BasicCredentialsProvider (org.apache.http.impl.client.BasicCredentialsProvider)212 CredentialsProvider (org.apache.http.client.CredentialsProvider)189 AuthScope (org.apache.http.auth.AuthScope)163 HttpHost (org.apache.http.HttpHost)95 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)78 HttpGet (org.apache.http.client.methods.HttpGet)73 IOException (java.io.IOException)54 BasicScheme (org.apache.http.impl.auth.BasicScheme)53 Test (org.junit.Test)53 HttpResponse (org.apache.http.HttpResponse)52 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)46 Credentials (org.apache.http.auth.Credentials)44 HttpClientBuilder (org.apache.http.impl.client.HttpClientBuilder)43 HttpClientContext (org.apache.http.client.protocol.HttpClientContext)37 URI (java.net.URI)34 AuthCache (org.apache.http.client.AuthCache)32 HttpClient (org.apache.http.client.HttpClient)31 BasicAuthCache (org.apache.http.impl.client.BasicAuthCache)31 HttpPost (org.apache.http.client.methods.HttpPost)29