List of usage examples for org.apache.http.client CredentialsProvider setCredentials
void setCredentials(AuthScope authscope, Credentials credentials);
From source file:org.fcrepo.integration.AbstractResourceIT.java
protected HttpResponse executeWithBasicAuth(final HttpUriRequest request, final String username, final String password) throws IOException { final HttpHost target = new HttpHost(HOSTNAME, SERVER_PORT, PROTOCOL); final CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials(new AuthScope(target.getHostName(), target.getPort()), new UsernamePasswordCredentials(username, password)); try (final CloseableHttpClient httpclient = HttpClients.custom() .setDefaultCredentialsProvider(credsProvider).build()) { final AuthCache authCache = new BasicAuthCache(); final BasicScheme basicAuth = new BasicScheme(); authCache.put(target, basicAuth); final HttpClientContext localContext = HttpClientContext.create(); localContext.setAuthCache(authCache); final CloseableHttpResponse response = httpclient.execute(request, localContext); return response; }//from ww w. jav a 2s. c o m }
From source file:org.sonatype.nexus.client.rest.jersey.NexusClientFactoryImpl.java
protected void applyAuthenticationIfAny(final ConnectionInfo connectionInfo, ApacheHttpClient4Config config) { if (connectionInfo.getAuthenticationInfo() != null) { if (connectionInfo.getAuthenticationInfo() instanceof UsernamePasswordAuthenticationInfo) { final UsernamePasswordAuthenticationInfo upinfo = (UsernamePasswordAuthenticationInfo) connectionInfo .getAuthenticationInfo(); final CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(upinfo.getUsername(), upinfo.getPassword())); config.getProperties().put(PROPERTY_CREDENTIALS_PROVIDER, credentialsProvider); config.getProperties().put(PROPERTY_PREEMPTIVE_BASIC_AUTHENTICATION, true); } else {// www . ja v a2 s. co m throw new IllegalArgumentException(String.format("AuthenticationInfo of type %s is not supported!", connectionInfo.getAuthenticationInfo().getClass().getName())); } } }
From source file:com.seyren.core.util.graphite.GraphiteHttpClient.java
private HttpClient createHttpClient() { HttpClientBuilder clientBuilder = HttpClientBuilder.create().useSystemProperties() .setConnectionManager(createConnectionManager()) .setDefaultRequestConfig(RequestConfig.custom() .setConnectionRequestTimeout(graphiteConnectionRequestTimeout) .setConnectTimeout(graphiteConnectTimeout).setSocketTimeout(graphiteSocketTimeout).build()); // Set auth header for graphite if username and password are provided if (!StringUtils.isEmpty(graphiteUsername) && !StringUtils.isEmpty(graphitePassword)) { CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); credentialsProvider.setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT), new UsernamePasswordCredentials(graphiteUsername, graphitePassword)); clientBuilder.setDefaultCredentialsProvider(credentialsProvider); context.setAttribute("preemptive-auth", new BasicScheme()); clientBuilder.addInterceptorFirst(new PreemptiveAuth()); }//from w ww . ja v a 2 s .c o m return clientBuilder.build(); }
From source file:com.jaeksoft.searchlib.crawler.web.spider.ProxyHandler.java
public void check(RequestConfig.Builder configBuilder, URI uri, CredentialsProvider credentialsProvider) { if (proxy == null || uri == null) return;/*from w ww. ja va2 s . c o m*/ if (exclusionSet.contains(uri.getHost())) return; configBuilder.setProxy(proxy); if (!StringUtils.isEmpty(username)) credentialsProvider.setCredentials(new AuthScope(proxy), new UsernamePasswordCredentials(username, password)); }
From source file:eu.fthevenet.binjr.data.adapters.HttpDataAdapterBase.java
protected CloseableHttpClient httpClientFactory() throws CannotInitializeDataAdapterException { try {//from w w w. ja va2 s . c o m SSLConnectionSocketFactory csf = new SSLConnectionSocketFactory(createSslCustomContext(), null, null, SSLConnectionSocketFactory.getDefaultHostnameVerifier()); RegistryBuilder<AuthSchemeProvider> schemeProviderBuilder = RegistryBuilder.create(); schemeProviderBuilder.register(AuthSchemes.SPNEGO, new SPNegoSchemeFactory()); CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials(new AuthScope(null, -1, null), new Credentials() { @Override public Principal getUserPrincipal() { return null; } @Override public String getPassword() { return null; } }); return HttpClients.custom().setDefaultAuthSchemeRegistry(schemeProviderBuilder.build()) .setDefaultCredentialsProvider(credsProvider).setSSLSocketFactory(csf).build(); } catch (Exception e) { throw new CannotInitializeDataAdapterException( "Could not initialize adapter to source '" + this.getSourceName() + "': " + e.getMessage(), e); } }
From source file:io.wcm.caravan.commons.httpclient.impl.HttpClientItem.java
/** * @param config Http client configuration *//*from ww w.ja v a2 s . com*/ HttpClientItem(HttpClientConfig config) { this.config = config; // optional SSL client certificate support SSLContext sslContext; if (CertificateLoader.isSslKeyManagerEnabled(config) || CertificateLoader.isSslTrustStoreEnbaled(config)) { try { sslContext = CertificateLoader.buildSSLContext(config); } catch (IOException | GeneralSecurityException ex) { throw new IllegalArgumentException("Invalid SSL client certificate configuration.", ex); } } else { sslContext = CertificateLoader.createDefaultSSlContext(); } CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); // optional proxy authentication if (StringUtils.isNotEmpty(config.getProxyUser())) { credentialsProvider.setCredentials(new AuthScope(config.getProxyHost(), config.getProxyPort()), new UsernamePasswordCredentials(config.getProxyUser(), config.getProxyPassword())); } // optional http basic authentication support if (StringUtils.isNotEmpty(config.getHttpUser())) { credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(config.getHttpUser(), config.getHttpPassword())); } // build http clients connectionManager = buildConnectionManager(config, sslContext); httpClient = buildHttpClient(config, connectionManager, credentialsProvider); }
From source file:org.piwigo.remotesync.api.client.WSClient.java
protected CloseableHttpClient getHttpClient() throws Exception { if (httpClient == null) { HttpClientBuilder httpClientBuilder = HttpClientBuilder.create(); if (clientConfiguration.getUsesProxy()) { String proxyUrl = clientConfiguration.getProxyUrl(); int proxyPort = clientConfiguration.getProxyPort(); String proxyUsername = clientConfiguration.getProxyUsername(); String proxyPassword = clientConfiguration.getProxyPassword(); if (proxyUsername != null && proxyUsername.length() > 0 && proxyPassword != null && proxyPassword.length() > 0) { CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); credentialsProvider.setCredentials(new AuthScope(proxyUrl, proxyPort), new UsernamePasswordCredentials(proxyUsername, proxyPassword)); httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider); }//ww w.j a v a2 s . c o m HttpHost proxy = new HttpHost(proxyUrl, proxyPort); requestConfig = RequestConfig.custom().setProxy(proxy).build(); } if (clientConfiguration.getTrustSSLCertificates()) { SSLContextBuilder sslContextBuilder = new SSLContextBuilder(); sslContextBuilder.loadTrustMaterial(null, new TrustSSLCertificatesStrategy()); httpClientBuilder.setSSLSocketFactory(new SSLConnectionSocketFactory(sslContextBuilder.build())); } httpClient = httpClientBuilder.build(); } return httpClient; }
From source file:org.eclipse.skalli.core.destination.DestinationComponent.java
private void setCredentials(DefaultHttpClient client, URL url) { if (configService == null) { return;/*from w w w. j av a2 s . c o m*/ } DestinationsConfig config = configService.readConfiguration(DestinationsConfig.class); if (config == null) { return; } for (DestinationConfig destination : config.getDestinations()) { Pattern regex = Pattern.compile(destination.getPattern()); Matcher matcher = regex.matcher(url.toExternalForm()); if (matcher.matches()) { if (LOG.isDebugEnabled()) { LOG.debug(MessageFormat.format("matched URL {0} with destination ''{1}''", url.toExternalForm(), destination.getId())); } if (StringUtils.isNotBlank(destination.getUser()) && StringUtils.isNotBlank(destination.getPattern())) { String authenticationMethod = destination.getAuthenticationMethod(); if ("basic".equalsIgnoreCase(authenticationMethod)) { //$NON-NLS-1$ CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(destination.getUser(), destination.getPassword())); client.setCredentialsProvider(credsProvider); } else if (StringUtils.isNotBlank(authenticationMethod)) { LOG.warn(MessageFormat.format("Authentication method ''{1}'' is not supported", authenticationMethod)); } } break; } } }
From source file:com.microsoft.alm.provider.JaxrsClientProvider.java
private ClientConfig getClientConfig(final String username, final String password) { Debug.Assert(username != null, "username cannot be null"); Debug.Assert(password != null, "password cannot be null"); final Credentials credentials = new UsernamePasswordCredentials(username, password); final CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); credentialsProvider.setCredentials(AuthScope.ANY, credentials); final ConnectorProvider connectorProvider = new ApacheConnectorProvider(); final ClientConfig clientConfig = new ClientConfig().connectorProvider(connectorProvider); clientConfig.property(ApacheClientProperties.CREDENTIALS_PROVIDER, credentialsProvider); clientConfig.property(ApacheClientProperties.PREEMPTIVE_BASIC_AUTHENTICATION, true); clientConfig.property(ClientProperties.REQUEST_ENTITY_PROCESSING, RequestEntityProcessing.BUFFERED); addProxySettings(clientConfig);//from www.j ava 2 s. c o m return clientConfig; }
From source file:org.apache.aurora.scheduler.http.api.security.HttpSecurityIT.java
private HttpResponse callH2Console(Credentials credentials) throws Exception { DefaultHttpClient defaultHttpClient = new DefaultHttpClient(); CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); credentialsProvider.setCredentials(AuthScope.ANY, credentials); defaultHttpClient.setCredentialsProvider(credentialsProvider); return defaultHttpClient.execute(new HttpPost(formatUrl(H2_PATH + "/"))); }