List of usage examples for org.apache.http.client CredentialsProvider setCredentials
void setCredentials(AuthScope authscope, Credentials credentials);
From source file:com.emc.storageos.driver.dellsc.scapi.rest.RestClient.java
/** * Instantiates a new Rest client./* w w w. j a v a2 s .c om*/ * * @param host Host name or IP address of the Dell Storage Manager server. * @param port Port the DSM data collector is listening on. * @param user The DSM user name to use. * @param password The DSM password. */ public RestClient(String host, int port, String user, String password) { this.baseUrl = String.format("https://%s:%d/api/rest", host, port); try { // Set up auth handling CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials(new AuthScope(host, port), new UsernamePasswordCredentials(user, password)); AuthCache authCache = new BasicAuthCache(); BasicScheme basicAuth = new BasicScheme(); HttpHost target = new HttpHost(host, port, "https"); authCache.put(target, basicAuth); // Set up our context httpContext = HttpClientContext.create(); httpContext.setCookieStore(new BasicCookieStore()); httpContext.setAuthCache(authCache); // Create our HTTPS client SSLContext sslContext = SSLContexts.custom().loadTrustMaterial(null, new TrustStrategy() { @Override public boolean isTrusted(X509Certificate[] arg0, String arg1) throws CertificateException { return true; } }).build(); SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(sslContext, SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); this.httpClient = HttpClients.custom().setHostnameVerifier(new AllowAllHostnameVerifier()) .setDefaultCredentialsProvider(credsProvider).setSSLSocketFactory(sslSocketFactory).build(); } catch (NoSuchAlgorithmException | KeyStoreException | KeyManagementException e) { // Hopefully default SSL handling is set up LOG.warn("Failed to configure HTTP handling, falling back to default handler."); LOG.debug("Config error: {}", e); this.httpClient = HttpClients.createDefault(); } }
From source file:org.apache.brooklyn.security.StockSecurityProviderTest.java
private void checkRestSecurity(String username, String password, final int code) throws IOException { CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); if (username != null && password != null) { credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password)); }// w w w .ja v a 2 s. c om try (CloseableHttpClient client = HttpClientBuilder.create() .setDefaultCredentialsProvider(credentialsProvider).build()) { Asserts.succeedsEventually(new Callable<Void>() { @Override public Void call() throws Exception { assertResponseEquals(client, code); return null; } }); } }
From source file:de.vanita5.twittnuker.util.net.TwidereHttpClientImpl.java
public TwidereHttpClientImpl(final Context context, final HttpClientConfiguration conf) { this.conf = conf; final HttpClientBuilder clientBuilder = HttpClients.custom(); final LayeredConnectionSocketFactory factory = TwidereSSLSocketFactory.getSocketFactory(context, conf.isSSLErrorIgnored());// w w w . j a v a 2 s . c o m clientBuilder.setSSLSocketFactory(factory); final RequestConfig.Builder requestConfigBuilder = RequestConfig.custom(); requestConfigBuilder.setConnectionRequestTimeout(conf.getHttpConnectionTimeout()); requestConfigBuilder.setConnectTimeout(conf.getHttpConnectionTimeout()); requestConfigBuilder.setSocketTimeout(conf.getHttpReadTimeout()); requestConfigBuilder.setRedirectsEnabled(false); clientBuilder.setDefaultRequestConfig(requestConfigBuilder.build()); if (conf.isProxyConfigured()) { final HttpHost proxy = new HttpHost(conf.getHttpProxyHost(), conf.getHttpProxyPort()); clientBuilder.setProxy(proxy); if (!TextUtils.isEmpty(conf.getHttpProxyUser())) { if (logger.isDebugEnabled()) { logger.debug("Proxy AuthUser: " + conf.getHttpProxyUser()); logger.debug( "Proxy AuthPassword: " + InternalStringUtil.maskString(conf.getHttpProxyPassword())); } final CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); credentialsProvider.setCredentials(new AuthScope(conf.getHttpProxyHost(), conf.getHttpProxyPort()), new UsernamePasswordCredentials(conf.getHttpProxyUser(), conf.getHttpProxyPassword())); clientBuilder.setDefaultCredentialsProvider(credentialsProvider); } } client = clientBuilder.build(); }
From source file:org.jboss.as.test.integration.management.console.XFrameOptionsHeaderTestCase.java
private CredentialsProvider createCredentialsProvider(URL url) { CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(Authentication.USERNAME, Authentication.PASSWORD);/*from w ww . j a v a 2 s. co m*/ credentialsProvider.setCredentials(new AuthScope(url.getHost(), url.getPort(), "ManagementRealm"), credentials); return credentialsProvider; }
From source file:org.kitodo.data.elasticsearch.KitodoRestClient.java
/** * Create REST client with basic authentication. * // w w w. j a v a2s.c o m * @param host * default host is localhost * @param port * default port ist 9200 * @param protocol * default protocol is http */ private void initiateClientWithAuth(String host, Integer port, String protocol) { String user = ConfigMain.getParameter("elasticsearch.user", "elastic"); String password = ConfigMain.getParameter("elasticsearch.password", "changeme"); final CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(user, password)); client = RestClient.builder(new HttpHost(host, port, protocol)) .setHttpClientConfigCallback( httpClientBuilder -> httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider)) .build(); highLevelClient = new RestHighLevelClient(client); }
From source file:org.whispersystems.mmsmonster.mms.MmsConnection.java
protected CloseableHttpClient constructHttpClient() throws IOException { RequestConfig config = RequestConfig.custom().setConnectTimeout(20 * 1000) .setConnectionRequestTimeout(20 * 1000).setSocketTimeout(20 * 1000).setMaxRedirects(20).build(); URL mmsc = new URL(apn.getMmsc()); CredentialsProvider credsProvider = new BasicCredentialsProvider(); if (apn.hasAuthentication()) { credsProvider.setCredentials( new AuthScope(mmsc.getHost(), mmsc.getPort() > -1 ? mmsc.getPort() : mmsc.getDefaultPort()), new UsernamePasswordCredentials(apn.getUsername(), apn.getPassword())); }//from w ww. ja v a 2 s .com return HttpClients.custom().setConnectionReuseStrategy(new NoConnectionReuseStrategyHC4()) .setRedirectStrategy(new LaxRedirectStrategy()).setUserAgent("Android-Mms/2.0") .setConnectionManager(new BasicHttpClientConnectionManager()).setDefaultRequestConfig(config) .setDefaultCredentialsProvider(credsProvider).build(); }
From source file:org.apache.sling.launchpad.SmokeIT.java
private CloseableHttpClient newClient() { CredentialsProvider credsProvider = new BasicCredentialsProvider(); UsernamePasswordCredentials creds = new UsernamePasswordCredentials("admin", "admin"); credsProvider.setCredentials(new AuthScope("localhost", LAUNCHPAD_PORT), creds); return HttpClientBuilder.create().setDefaultCredentialsProvider(credsProvider).build(); }
From source file:org.springframework.boot.autoconfigure.elasticsearch.rest.RestClientAutoConfiguration.java
@Bean @ConditionalOnMissingBean/* w ww . j a va 2 s.co m*/ public RestClientBuilder restClientBuilder() { HttpHost[] hosts = this.properties.getUris().stream().map(HttpHost::create).toArray(HttpHost[]::new); RestClientBuilder builder = RestClient.builder(hosts); PropertyMapper map = PropertyMapper.get(); map.from(this.properties::getUsername).whenHasText().to((username) -> { CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); Credentials credentials = new UsernamePasswordCredentials(this.properties.getUsername(), this.properties.getPassword()); credentialsProvider.setCredentials(AuthScope.ANY, credentials); builder.setHttpClientConfigCallback( (httpClientBuilder) -> httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider)); }); this.builderCustomizers.orderedStream().forEach((customizer) -> customizer.customize(builder)); return builder; }
From source file:com.socrata.ApiBase.java
/** * Sets up http authentication (BASIC) for default requests *///from w w w . jav a 2s . c o m private void setupBasicAuthentication() { Credentials defaultcreds = new UsernamePasswordCredentials(username, password); CredentialsProvider credProvider = new BasicCredentialsProvider(); credProvider.setCredentials(AuthScope.ANY, defaultcreds); AuthCache authCache = new BasicAuthCache(); BasicScheme basicAuth = new BasicScheme(); httpContext = new BasicHttpContext(); httpContext.setAttribute(ClientContext.AUTH_CACHE, basicAuth); httpClient.setCredentialsProvider(credProvider); }
From source file:org.apache.reef.runtime.hdinsight.client.yarnrest.HDInsightInstance.java
private HttpClientContext getClientContext(final String hostname, final String username, final String password) throws IOException { final HttpHost targetHost = new HttpHost(hostname, 443, "https"); final HttpClientContext result = HttpClientContext.create(); // Setup credentials provider final CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password)); result.setCredentialsProvider(credentialsProvider); // Setup preemptive authentication final AuthCache authCache = new BasicAuthCache(); final BasicScheme basicAuth = new BasicScheme(); authCache.put(targetHost, basicAuth); result.setAuthCache(authCache);//from w w w.j a v a2 s .co m final HttpGet httpget = new HttpGet("/"); // Prime the cache try (final CloseableHttpResponse response = this.httpClient.execute(targetHost, httpget, result)) { // empty try block used to automatically close resources } return result; }