本文整理汇总了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();
}
}
示例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);
}
示例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);
}
示例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();
}
示例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;
}
示例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();
}
示例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);
}
}
}
示例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);
}
示例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);
}
示例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;
}
示例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));
}
}
}
示例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));
}
示例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));
}
}
}
示例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();
}
示例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());
}