当前位置: 首页>>代码示例>>Java>>正文


Java HttpClientBuilder.setDefaultCredentialsProvider方法代码示例

本文整理汇总了Java中org.apache.http.impl.client.HttpClientBuilder.setDefaultCredentialsProvider方法的典型用法代码示例。如果您正苦于以下问题:Java HttpClientBuilder.setDefaultCredentialsProvider方法的具体用法?Java HttpClientBuilder.setDefaultCredentialsProvider怎么用?Java HttpClientBuilder.setDefaultCredentialsProvider使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在org.apache.http.impl.client.HttpClientBuilder的用法示例。


在下文中一共展示了HttpClientBuilder.setDefaultCredentialsProvider方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: setProxy

import org.apache.http.impl.client.HttpClientBuilder; //导入方法依赖的package包/类
public void setProxy(String proxyHost, Integer proxyPort, Credentials credentials) {
      this.proxyHost = proxyHost;
      this.proxyPort = proxyPort;
      
if (this.proxyHost.length() > 0 && !this.proxyPort.equals(0)) {
          HttpClientBuilder clientBuilder = HttpClients.custom()
              .useSystemProperties()
              .setProxy(new HttpHost(proxyHost, proxyPort, "http"));
              
          if (credentials != null) {
              CredentialsProvider credsProvider = new BasicCredentialsProvider();
              credsProvider.setCredentials(new AuthScope(proxyHost, proxyPort), credentials);
              clientBuilder.setDefaultCredentialsProvider(credsProvider);
              Loggers.SERVER.debug("MsTeamsNotification ::using proxy credentials " + credentials.getUserPrincipal().getName());
          }
          
          this.client = clientBuilder.build();
}
  }
 
开发者ID:spyder007,项目名称:teamcity-msteams-notifier,代码行数:20,代码来源:MsTeamsNotificationImpl.java

示例2: getProxyExecutor

import org.apache.http.impl.client.HttpClientBuilder; //导入方法依赖的package包/类
public static HttpCommandExecutor getProxyExecutor(URL url, Properties prop) {

        prop = decrypt(prop);

        String proxyHost = prop.getProperty("proxyHost");
        int proxyPort = Integer.valueOf(prop.getProperty("proxyPort"));
        String proxyUserDomain = prop.getProperty("proxyUserDomain");
        String proxyUser = prop.getProperty("proxyUser");
        String proxyPassword = prop.getProperty("proxyPassword");

        HttpClientBuilder builder = HttpClientBuilder.create();
        HttpHost proxy = new HttpHost(proxyHost, proxyPort);
        CredentialsProvider credsProvider = new BasicCredentialsProvider();

        credsProvider.setCredentials(new AuthScope(proxyHost, proxyPort),
                new NTCredentials(proxyUser, proxyPassword, getWorkstation(), proxyUserDomain));
        if (url.getUserInfo() != null && !url.getUserInfo().isEmpty()) {
            credsProvider.setCredentials(new AuthScope(url.getHost(), (url.getPort() > 0 ? url.getPort() : url.getDefaultPort())),
                    new UsernamePasswordCredentials(proxyUser, proxyPassword));
        }
        builder.setProxy(proxy);
        builder.setDefaultCredentialsProvider(credsProvider);
        HttpClient.Factory factory = new SimpleHttpClientFactory(builder);
        return new HttpCommandExecutor(new HashMap<String, CommandInfo>(), url, factory);

    }
 
开发者ID:CognizantQAHub,项目名称:Cognizant-Intelligent-Test-Scripter,代码行数:27,代码来源:RemoteProxy.java

示例3: configure

import org.apache.http.impl.client.HttpClientBuilder; //导入方法依赖的package包/类
public void configure(HttpClientBuilder builder) {
    SystemDefaultCredentialsProvider credentialsProvider = new SystemDefaultCredentialsProvider();
    configureSslSocketConnectionFactory(builder, httpSettings.getSslContextFactory());
    configureAuthSchemeRegistry(builder);
    configureCredentials(builder, credentialsProvider, httpSettings.getAuthenticationSettings());
    configureProxy(builder, credentialsProvider, httpSettings);
    configureUserAgent(builder);
    builder.setDefaultCredentialsProvider(credentialsProvider);
}
 
开发者ID:lxxlxx888,项目名称:Reer,代码行数:10,代码来源:HttpClientConfigurer.java

示例4: init

import org.apache.http.impl.client.HttpClientBuilder; //导入方法依赖的package包/类
@PostConstruct
public void init() {
    objectMapper = new ObjectMapper();
    final HttpClientBuilder clientBuilder = HttpClientBuilder.create();
    if (StringUtils.hasText(camundaUser)) {
        final BasicCredentialsProvider provider = new BasicCredentialsProvider();
        provider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(camundaUser, camundaPass));
        clientBuilder.setDefaultCredentialsProvider(provider);
    }
    httpClient = clientBuilder.build();
}
 
开发者ID:EsikAntony,项目名称:camunda-task-dispatcher,代码行数:12,代码来源:ExternalTaskRestServiceImpl.java

示例5: createRequestFactory

import org.apache.http.impl.client.HttpClientBuilder; //导入方法依赖的package包/类
public ClientHttpRequestFactory createRequestFactory(HttpProxyConfiguration httpProxyConfiguration, boolean
        trustSelfSignedCerts) {
    HttpClientBuilder httpClientBuilder = HttpClients.custom().useSystemProperties();

    if (trustSelfSignedCerts) {
        httpClientBuilder.setSslcontext(buildSslContext());
        httpClientBuilder.setHostnameVerifier(BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
    }

    if (httpProxyConfiguration != null) {
        HttpHost proxy = new HttpHost(httpProxyConfiguration.getProxyHost(), httpProxyConfiguration.getProxyPort());
        httpClientBuilder.setProxy(proxy);

        if (httpProxyConfiguration.isAuthRequired()) {
            BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
            credentialsProvider.setCredentials(
                    new AuthScope(httpProxyConfiguration.getProxyHost(), httpProxyConfiguration.getProxyPort()),
                    new UsernamePasswordCredentials(httpProxyConfiguration.getUsername(), httpProxyConfiguration
                            .getPassword()));
            httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
        }

        HttpRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxy);
        httpClientBuilder.setRoutePlanner(routePlanner);
    }

    HttpClient httpClient = httpClientBuilder.build();
    HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(httpClient);

    return requestFactory;
}
 
开发者ID:SAP,项目名称:cf-java-client-sap,代码行数:32,代码来源:RestUtil.java

示例6: createHttpClient

import org.apache.http.impl.client.HttpClientBuilder; //导入方法依赖的package包/类
private HttpClient createHttpClient(Authentication auth, String verify, HttpHost target, Boolean postRedirects,
		String password, TrustStrategy keystoreTrustStrategy, HostnameVerifier keystoreHostnameVerifier,
		Proxy proxy) {
	Certificate certificate = new Certificate();
	Auth authHelper = new Auth();
	HttpClientBuilder httpClientBuilder = WinHttpClients.custom();
	Builder requestConfig = RequestConfig.custom();
	requestConfig.setCookieSpec(CookieSpecs.DEFAULT);

	logger.debug("Verify value: " + verify);
	logger.debug((new File(verify).getAbsolutePath()));

	if (new File(verify).exists()) {
		logger.debug("Loading custom keystore");
		httpClientBuilder.setSSLSocketFactory(
				certificate.allowAllCertificates(certificate.createCustomKeyStore(verify.toString(), password),
						password, keystoreTrustStrategy, keystoreHostnameVerifier));
	} else if (!Boolean.parseBoolean(verify.toString())) {
		logger.debug("Allowing all certificates");
		httpClientBuilder.setSSLSocketFactory(certificate.allowAllCertificates(null));
	}

	if (auth.isAuthenticable()) {
		httpClientBuilder.setDefaultCredentialsProvider(authHelper.getCredentialsProvider(auth, target));
	}

	if (proxy != null && proxy.isInUse()) {
		logger.debug("Enabling proxy");
		if (proxy.isAuthenticable()) {
			logger.debug("Setting proxy credentials");
			httpClientBuilder.setDefaultCredentialsProvider(
					authHelper.getCredentialsProvider(proxy.getAuth(), proxy.getHttpHost()));
		}
		requestConfig.setProxy(proxy.getHttpHost());
	}

	if (postRedirects) {
		httpClientBuilder.setRedirectStrategy(new CustomRedirectStrategy());
	}
	httpClientBuilder.setDefaultRequestConfig(requestConfig.build());

	return httpClientBuilder.build();
}
 
开发者ID:Hi-Fi,项目名称:httpclient,代码行数:44,代码来源:RestClient.java

示例7: setupAuthentication

import org.apache.http.impl.client.HttpClientBuilder; //导入方法依赖的package包/类
/**
 * Set up authentication for HTTP Basic/HTTP Digest/SPNEGO.
 *
 * @param httpClientBuilder The client builder
 * @return The context
 * @throws HttpException
 */
private void setupAuthentication( HttpClientBuilder httpClientBuilder ) throws HttpException {

    CredentialsProvider credsProvider = new BasicCredentialsProvider();
    credsProvider.setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT),
                                 new UsernamePasswordCredentials(username, password));
    httpClientBuilder.setDefaultCredentialsProvider(credsProvider);

    if (authType == AuthType.always) {
        AuthCache authCache = new BasicAuthCache();
        // Generate BASIC scheme object and add it to the local auth cache
        BasicScheme basicAuth = new BasicScheme();

        HttpHost target = new HttpHost(host, port, isOverSsl
                                                             ? "https"
                                                             : "http");
        authCache.put(target, basicAuth);

        // Add AuthCache to the execution context
        httpContext.setAuthCache(authCache);
    } else {
        if (!StringUtils.isNullOrEmpty(kerberosServicePrincipalName)) {
            GssClient gssClient = new GssClient(username, password, kerberosClientKeytab, krb5ConfFile);
            AuthSchemeProvider nsf = new SPNegoSchemeFactory(gssClient, kerberosServicePrincipalName,
                                                             kerberosServicePrincipalType);
            final Registry<AuthSchemeProvider> authSchemeRegistry = RegistryBuilder.<AuthSchemeProvider> create()
                                                                                   .register(AuthSchemes.SPNEGO,
                                                                                             nsf)
                                                                                   .build();
            httpClientBuilder.setDefaultAuthSchemeRegistry(authSchemeRegistry);
        }
    }
}
 
开发者ID:Axway,项目名称:ats-framework,代码行数:40,代码来源:HttpClient.java

示例8: addSpnego

import org.apache.http.impl.client.HttpClientBuilder; //导入方法依赖的package包/类
public static void addSpnego(HttpClientBuilder clientBuilder) {
    //Add spnego http header processor
    Lookup<AuthSchemeProvider> authSchemeRegistry = RegistryBuilder.<AuthSchemeProvider> create()
            .register(AuthSchemes.SPNEGO, new SPNegoSchemeFactory(true)).build();
    clientBuilder.setDefaultAuthSchemeRegistry(authSchemeRegistry);
    //There has to be at least this dummy credentials provider or apache http client gives up
    BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
    credentialsProvider.setCredentials(new AuthScope(null, -1, null), new NullCredentials());
    clientBuilder.setDefaultCredentialsProvider(credentialsProvider);
}
 
开发者ID:LIBCAS,项目名称:ARCLib,代码行数:11,代码来源:SpnegoHelper.java

示例9: prepareRestTemplate

import org.apache.http.impl.client.HttpClientBuilder; //导入方法依赖的package包/类
/**
 * Ensures that the passed-in {@link RestTemplate} is using the Apache HTTP Client. If the optional {@code username} AND
 * {@code password} are not empty, then a {@link BasicCredentialsProvider} will be added to the {@link CloseableHttpClient}.
 *
 * Furthermore, you can set the underlying {@link SSLContext} of the {@link HttpClient} allowing you to accept self-signed
 * certificates.
 *
 * @param restTemplate Must not be null
 * @param username Can be null
 * @param password Can be null
 * @param skipSslValidation Use with caution! If true certificate warnings will be ignored.
 */
public static void prepareRestTemplate(
		RestTemplate restTemplate,
		String username,
		String password,
		boolean skipSslValidation) {

	Assert.notNull(restTemplate, "The provided RestTemplate must not be null.");

	final HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();

	if (StringUtils.hasText(username) && StringUtils.hasText(password)) {
		final BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
		credentialsProvider.setCredentials(AuthScope.ANY,
				new UsernamePasswordCredentials(
						username,
						password));
		httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
	}

	if (skipSslValidation) {
		httpClientBuilder.setSSLContext(HttpClientUtils.buildCertificateIgnoringSslContext());
		httpClientBuilder.setSSLHostnameVerifier(new NoopHostnameVerifier());
	}

	final CloseableHttpClient httpClient = httpClientBuilder.build();
	final HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(httpClient);
	restTemplate.setRequestFactory(requestFactory);
}
 
开发者ID:spring-cloud,项目名称:spring-cloud-dashboard,代码行数:41,代码来源:HttpClientUtils.java

示例10: getHttpClient

import org.apache.http.impl.client.HttpClientBuilder; //导入方法依赖的package包/类
/**
 * Construct httpclient with SSL protocol
 *
 * @param clientConfiguration the client configuration
 * @return {@code CloseableHttpClient}
 * @throws Exception
 */
public static CloseableHttpClient getHttpClient(ClientConfiguration clientConfiguration) throws Exception {
    if (clientConfiguration == null) {
        clientConfiguration = new ClientConfiguration();
    }

    SSLConnectionSocketFactory sslSocketFactory = createSslConnectionSocketFactory(clientConfiguration);
    HttpClientBuilder builder = HttpClients.custom();

    // set proxy
    String proxyHost = clientConfiguration.getProxyHost();
    int proxyPort = clientConfiguration.getProxyPort();

    if (!StringUtils.isEmpty(proxyHost) && proxyPort > 0) {
        HttpHost proxy = new HttpHost(proxyHost, proxyPort);
        builder.setProxy(proxy);

        String username = clientConfiguration.getProxyUserName();
        String password = clientConfiguration.getProxyPassword();

        if (!StringUtils.isEmpty(username) && !StringUtils.isEmpty(password)) {
            CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
            AuthScope authscope = new AuthScope(proxy);
            Credentials credentials = new UsernamePasswordCredentials(username,
                    password);
            credentialsProvider.setCredentials(authscope, credentials);
            builder.setDefaultCredentialsProvider(credentialsProvider);
        }
    }
    builder.setUserAgent(VersionUtil.getDefaultUserAgent());
    CloseableHttpClient httpclient = builder.setSSLSocketFactory(sslSocketFactory).build();
    return httpclient;
}
 
开发者ID:SimpleMessageNotification,项目名称:smn-sdk-java,代码行数:40,代码来源:HttpUtil.java

示例11: addProxyConfig

import org.apache.http.impl.client.HttpClientBuilder; //导入方法依赖的package包/类
private void addProxyConfig(HttpClientBuilder builder,
                            ProxyConfiguration proxyConfiguration) {
    if (isProxyEnabled(proxyConfiguration)) {

        LOG.info("Configuring Proxy. Proxy Host: " + proxyConfiguration.endpoint());

        builder.setRoutePlanner(new SdkProxyRoutePlanner(proxyConfiguration.endpoint().getHost(),
                                                         proxyConfiguration.endpoint().getPort(),
                                                         proxyConfiguration.nonProxyHosts()));

        if (isAuthenticatedProxy(proxyConfiguration)) {
            builder.setDefaultCredentialsProvider(ApacheUtils.newProxyCredentialsProvider(proxyConfiguration));
        }
    }
}
 
开发者ID:aws,项目名称:aws-sdk-java-v2,代码行数:16,代码来源:ApacheHttpClientFactory.java

示例12: addProxy

import org.apache.http.impl.client.HttpClientBuilder; //导入方法依赖的package包/类
private void addProxy(RESTPool pool, HttpClientBuilder builder) {
    if (pool.getProxy() == null) return;

    Proxy proxy = pool.getProxy();

    if (proxy.getUsername() != null) {
        CredentialsProvider provider = makeProxyCredentialsProvider(proxy);
        builder.setDefaultCredentialsProvider(provider);
    }

    HttpHost proxyHost = new HttpHost(proxy.getHostname(), proxy.getPort());
    builder.setRoutePlanner(new DefaultProxyRoutePlanner(proxyHost));
}
 
开发者ID:mercadolibre,项目名称:java-restclient,代码行数:14,代码来源:HTTPCBuilder.java

示例13: addProxyConfig

import org.apache.http.impl.client.HttpClientBuilder; //导入方法依赖的package包/类
private void addProxyConfig(HttpClientBuilder builder,
                            HttpClientSettings settings) {
    if (settings.isProxyEnabled()) {

        LOG.info("Configuring Proxy. Proxy Host: " + settings.getProxyHost() + " " +
                "Proxy Port: " + settings.getProxyPort());

        builder.setRoutePlanner(new SdkProxyRoutePlanner(
                settings.getProxyHost(), settings.getProxyPort(), settings.getNonProxyHosts()));

        if (settings.isAuthenticatedProxy()) {
            builder.setDefaultCredentialsProvider(ApacheUtils.newProxyCredentialsProvider(settings));
        }
    }
}
 
开发者ID:IBM,项目名称:ibm-cos-sdk-java,代码行数:16,代码来源:ApacheHttpClientFactory.java

示例14: build

import org.apache.http.impl.client.HttpClientBuilder; //导入方法依赖的package包/类
private CloseableHttpClient build(final Consumer<HttpClientBuilder> customizeBuilder) throws IOException {
	LOGGER.debug("HTTP {} {}", request, url);
	if (httpClient != null) {
		LOGGER.debug("Existing HttpClient re-used");
		return httpClient;
	}
	if (sslsf == null) {
		sslsf = createSSLConnectionSocketFactory(null, null, false);
	}

	HttpClientBuilder builder = HttpClients.custom()
			.setRetryHandler(new StandardHttpRequestRetryHandler(3, true) {
				@Override
				public boolean retryRequest(IOException exception,
						int executionCount, HttpContext context) {
					try {
						TimeUnit.SECONDS.sleep(3);
					} catch (InterruptedException e) {
					}
					return super.retryRequest(exception, executionCount,
							context);
				}
			}).setSSLSocketFactory(sslsf);
	if (username != null) {
		CredentialsProvider credsProvider = new BasicCredentialsProvider();
		credsProvider.setCredentials(
				new AuthScope(url.getHost(), url.getPort() == -1 ? url.getDefaultPort() : url.getPort()),
				new UsernamePasswordCredentials(username, password));

		builder.setDefaultCredentialsProvider(credsProvider);
	}
	if (disableRedirect) {
		builder.disableRedirectHandling();
	}

	addPreemptiveAuthorizationHeaders();

	if (customizeBuilder != null) {
		customizeBuilder.accept(builder);
	}
	if (!cookies.isEmpty()) {
		final CookieStore cookieStore = new BasicCookieStore();
		cookies.entrySet().stream().map(x -> new BasicClientCookie(x.getKey(), x.getValue())).forEach(cookieStore::addCookie);
		builder.setDefaultCookieStore(cookieStore);
	}
	return builder.build();
}
 
开发者ID:xtf-cz,项目名称:xtf,代码行数:48,代码来源:HttpClient.java

示例15: getUrlContents

import org.apache.http.impl.client.HttpClientBuilder; //导入方法依赖的package包/类
protected Response getUrlContents(String theUrl, boolean useAuth, boolean followRedirects) {

        StringBuilder content = new StringBuilder();
        int code;

        try {

            HttpClientBuilder builder = HttpClientBuilder.create();
            if (!followRedirects) {
                builder.disableRedirectHandling();
            }

            if (useAuth) {
                CredentialsProvider provider = new BasicCredentialsProvider();
                UsernamePasswordCredentials credentials = new UsernamePasswordCredentials("admin", "password");
                provider.setCredentials(AuthScope.ANY, credentials);
                builder.setDefaultCredentialsProvider(provider);
            }

            HttpClient client = builder.build();

            HttpResponse response = client.execute(new HttpGet(theUrl));
            code = response.getStatusLine().getStatusCode();

            if (response.getEntity() != null) {

                BufferedReader bufferedReader = new BufferedReader(
                    new InputStreamReader(response.getEntity().getContent())
                );

                String line;

                while ((line = bufferedReader.readLine()) != null) {
                    content.append(line + "\n");
                }
                bufferedReader.close();
            }

        }
        catch (Exception e) {
            throw new RuntimeException(e);
        }

        return new Response(code, content.toString());
    }
 
开发者ID:eclipse,项目名称:microprofile-health,代码行数:46,代码来源:SimpleHttp.java


注:本文中的org.apache.http.impl.client.HttpClientBuilder.setDefaultCredentialsProvider方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。