private CredentialsProvider makeCredentialsProvider(String host, int port, String username, String password) { CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); credentialsProvider.setCredentials( new AuthScope(host, port), new UsernamePasswordCredentials(username, password) ); return credentialsProvider; }
/** * Creates a copy of the given credentials scope. */ public AuthScope(final AuthScope authscope) { super(); if (authscope == null) { throw new IllegalArgumentException("Scope may not be null"); } this.host = authscope.getHost(); this.port = authscope.getPort(); this.realm = authscope.getRealm(); this.scheme = authscope.getScheme(); }
connectionManager.setDefaultMaxPerRoute(config.getMaxConnectionsPerHost()); HttpClientBuilder clientBuilder = HttpClientBuilder.create(); if (config.getCookieStore() != null) { clientBuilder.setDefaultCookieStore(config.getCookieStore()); clientBuilder.setDefaultRequestConfig(requestConfig); clientBuilder.setConnectionManager(connectionManager); clientBuilder.setUserAgent(config.getUserAgentString()); if (config.getProxyHost() != null) { if (config.getProxyUsername() != null) { AuthScope authScope = new AuthScope(config.getProxyHost(), config.getProxyPort()); Credentials credentials = new UsernamePasswordCredentials(config.getProxyUsername(), config.getProxyPassword()); credentialsMap.put(authScope, credentials); HttpHost proxy = new HttpHost(config.getProxyHost(), config.getProxyPort()); clientBuilder.setProxy(proxy); logger.debug("Working through Proxy: {}", proxy.getHostName()); CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); credentialsMap.forEach((AuthScope authscope, Credentials credentials) -> { credentialsProvider.setCredentials(authscope, credentials); }); clientBuilder.setDefaultCredentialsProvider(credentialsProvider);
final HttpClientBuilder clientBuilder = HttpClientBuilder.create(); clientBuilder.setConnectionManager(getConnectionManager(client, config, sslContext)); clientBuilder.setConnectionManagerShared( PropertiesHelper.getValue(config.getProperties(), ApacheClientProperties.CONNECTION_MANAGER_SHARED, false, null)); clientBuilder.setSslcontext(sslContext); if (proxyUri != null) { final URI u = getProxyUri(proxyUri); final HttpHost proxy = new HttpHost(u.getHost(), u.getPort(), u.getScheme()); final String userName; userName = ClientProperties.getValue(config.getProperties(), ClientProperties.PROXY_USERNAME, String.class); final CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials( new AuthScope(u.getHost(), u.getPort()), new UsernamePasswordCredentials(userName, password) ); clientBuilder.setDefaultCredentialsProvider(credsProvider);
.setRedirectsEnabled(false); HttpClientBuilder httpClientBuilder = HttpClientBuilder.create(); Integer proxyPort = Integer.parseInt(proxyPortStr); requestConfigBuilder.setProxy(new HttpHost(proxyHost, proxyPort)); logger.log(Level.INFO, "Using proxy: " + proxyHost + ":" + proxyPort); logger.log(Level.INFO, "Proxy required credentials; using '" + proxyUser + "' (username) and *** (hidden password)"); CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials(new AuthScope(proxyHost, proxyPort), new UsernamePasswordCredentials(proxyUser, proxyPassword)); httpClientBuilder.setDefaultCredentialsProvider(credsProvider); httpClientBuilder.setDefaultRequestConfig(requestConfigBuilder.build()); return httpClientBuilder.build();
final HttpHost target = HttpHost.create(crowdServer.toString()); final PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(); connectionManager.setDefaultMaxPerRoute(crowdProps.getHttpMaxConnections()); final CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); credentialsProvider.setCredentials( new AuthScope(target.getHostName(), target.getPort()), new UsernamePasswordCredentials(crowdProps.getApplicationName(), crowdProps.getApplicationPassword())); final AuthCache authCache = new BasicAuthCache(); authCache.put(target, new BasicScheme()); proxyString = proxyHost.toString(); if (StringUtils.isNotBlank(crowdProps.getHttpProxyUsername()) || StringUtils.isNotBlank(crowdProps.getHttpProxyPassword())) { credentialsProvider.setCredentials(new AuthScope(crowdProps.getHttpProxyHost(), crowdProps.getHttpProxyPort()), new UsernamePasswordCredentials(crowdProps.getHttpProxyUsername(), crowdProps.getHttpProxyPassword())); client = HttpClientBuilder.create() .setConnectionManager(connectionManager) .setDefaultCredentialsProvider(credentialsProvider) .setRoutePlanner(httpRoutePlanner) .build();
this.configuration = configuration; this.executor = Executors.newFixedThreadPool(configuration.getConnectionPoolSize()); target = new HttpHost(configuration.getHost(), configuration.getPort(), configuration.getSsl() ? "https" : "http"); HttpClientBuilder builder = HttpClients.custom(); if (configuration.getUsername() != null && configuration.getPassword() != null) { LOG.info("Adding basic auth credentials provider"); CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials(new AuthScope(target.getHostName(), target.getPort()), new UsernamePasswordCredentials(configuration.getUsername(), configuration.getPassword()) ); builder.setDefaultCredentialsProvider(credsProvider); sslBuilder.loadTrustMaterial(null, new TrustSelfSignedStrategy()); SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslBuilder.build()); builder.setSSLSocketFactory(sslsf); } catch (NoSuchAlgorithmException | KeyStoreException | KeyManagementException ex) { LOG.error("Failed to init socket factory {}", ex.getMessage(), ex); cm.setDefaultMaxPerRoute(configuration.getConnectionPoolSize()); cm.setMaxTotal(configuration.getConnectionPoolSize()); builder.setConnectionManager(cm); this.client = builder.build();
.setRequestExecutor(new InstrumentedHttpRequestExecutor(metricRegistry, metricNameStrategy, name)) .setConnectionManager(manager) .setDefaultRequestConfig(requestConfig) .setDefaultSocketConfig(socketConfig) .setConnectionReuseStrategy(reuseStrategy) final HttpHost httpHost = new HttpHost(proxy.getHost(), proxy.getPort(), proxy.getScheme()); builder.setRoutePlanner(new NonProxyListProxyRoutePlanner(httpHost, proxy.getNonProxyHosts())); if (auth != null) { if (credentialsProvider == null) { credentialsProvider = new BasicCredentialsProvider(); AuthScope authScope = new AuthScope(httpHost, auth.getRealm(), auth.getAuthScheme()); credentialsProvider.setCredentials(authScope, credentials);
final HttpClientBuilder hcBuilder = HttpClientBuilder.create(); final RequestConfig.Builder rcBuilder = RequestConfig.custom(); final RequestBuilder reqBuilder = RequestBuilder.create( hcBuilder.setRetryHandler(new DefaultHttpRequestRetryHandler(0, false)); proxy.acquire(); if (proxy.isEnabled()) { final HttpHost proxyHost = new HttpHost(proxy.getHost(), proxy.getPort(), "http"); if (proxy.isAuthEnabled()) { CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials( new AuthScope(proxy.getHost(), proxy.getPort()), new UsernamePasswordCredentials(proxy.getUsername(), new String(proxy.getPassword()))); hcBuilder.setDefaultCredentialsProvider(credsProvider); hcBuilder.setProxy(proxyHost); String realm = StringUtil.isEmpty(a.getRealm()) ? AuthScope.ANY_REALM : a.getRealm(); CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials( new AuthScope(host, urlPort, realm), new UsernamePasswordCredentials(uid, pwd)); hcBuilder.setDefaultCredentialsProvider(credsProvider); String pwd = new String(a.getPassword());
schemeRegistry.register(new Scheme("http", Integer.parseInt(port), ssf)); PoolingClientConnectionManager ccm = new PoolingClientConnectionManager(schemeRegistry); httpClient = new DefaultHttpClient(ccm); httpHost = new HttpHost(hosts[0], Integer.parseInt(port), "http"); ((AbstractHttpClient) httpClient).getCredentialsProvider().setCredentials( new AuthScope(hosts[0], Integer.parseInt(port)), new UsernamePasswordCredentials(userName, password)); ((DefaultHttpClient) httpClient).addRequestInterceptor(new HttpRequestInterceptor() ((DefaultHttpClient) httpClient).addResponseInterceptor(new HttpResponseInterceptor()
public static void main(String[] args) { /* * This is out ot date - Just keeping * it in case it's helpful... */ final String authUser = "username"; final String authPassword = "password"; CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials(new AuthScope("10.10.10.10", 8080), new UsernamePasswordCredentials(authUser, authPassword)); HttpHost myProxy = new HttpHost("10.10.10.10", 8080); HttpClientBuilder clientBuilder = HttpClientBuilder.create(); clientBuilder .setProxy(myProxy) .setProxyAuthenticationStrategy(new ProxyAuthenticationStrategy()) .setDefaultCredentialsProvider(credsProvider) .disableCookieManagement(); CloseableHttpClient httpClient = clientBuilder.build(); FhirContext ctx = FhirContext.forDstu2(); String serverBase = "http://spark.furore.com/fhir/"; ctx.getRestfulClientFactory().setHttpClient(httpClient); IGenericClient client = ctx.newRestfulGenericClient(serverBase); IdDt id = new IdDt("Patient", "123"); client.read(Patient.class, id); }
private HttpClient openConnection() throws IOException { HttpClientBuilder httpClientBuilder = HttpClientBuilder.create(); if (sslContextService != null) { try { SSLContext sslContext = getSslSocketFactory(sslContextService); httpClientBuilder.setSSLContext(sslContext); } catch (KeyStoreException | CertificateException | NoSuchAlgorithmException | UnrecoverableKeyException | KeyManagementException e) { throw new IOException(e); } } if (credentialsService != null) { CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); credentialsProvider.setCredentials(new AuthScope(null, -1, null), new KerberosKeytabCredentials(credentialsService.getPrincipal(), credentialsService.getKeytab())); httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider); Lookup<AuthSchemeProvider> authSchemeRegistry = RegistryBuilder.<AuthSchemeProvider> create() .register(AuthSchemes.SPNEGO, new KerberosKeytabSPNegoAuthSchemeProvider()).build(); httpClientBuilder.setDefaultAuthSchemeRegistry(authSchemeRegistry); } RequestConfig.Builder requestConfigBuilder = RequestConfig.custom(); requestConfigBuilder.setConnectTimeout(connectTimeout); requestConfigBuilder.setConnectionRequestTimeout(connectTimeout); requestConfigBuilder.setSocketTimeout(connectTimeout); httpClientBuilder.setDefaultRequestConfig(requestConfigBuilder.build()); return httpClientBuilder.build(); }
private DefaultHttpClient getHttpClient() { HttpHost target = new HttpHost(hostname, port, "http"); DefaultHttpClient httpClient = new DefaultHttpClient(); httpClient.getCredentialsProvider().setCredentials (new AuthScope(target.getHostName(), target.getPort()), new UsernamePasswordCredentials(USERNAME, PASSWORD)); return httpClient; } }
public synchronized HttpClient getNativeHttpClient() { if (myHttpClient == null) { //FIXME potential resoource leak PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS); connectionManager.setMaxTotal(getPoolMaxTotal()); connectionManager.setDefaultMaxPerRoute(getPoolMaxPerRoute()); // @formatter:off //TODO: Use of a deprecated method should be resolved. RequestConfig defaultRequestConfig = RequestConfig.custom().setSocketTimeout(getSocketTimeout()) .setConnectTimeout(getConnectTimeout()).setConnectionRequestTimeout(getConnectionRequestTimeout()) .setStaleConnectionCheckEnabled(true).setProxy(myProxy).build(); HttpClientBuilder builder = HttpClients.custom().setConnectionManager(connectionManager) .setDefaultRequestConfig(defaultRequestConfig).disableCookieManagement(); if (myProxy != null && StringUtils.isNotBlank(getProxyUsername()) && StringUtils.isNotBlank(getProxyPassword())) { CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials(new AuthScope(myProxy.getHostName(), myProxy.getPort()), new UsernamePasswordCredentials(getProxyUsername(), getProxyPassword())); builder.setProxyAuthenticationStrategy(new ProxyAuthenticationStrategy()); builder.setDefaultCredentialsProvider(credsProvider); } myHttpClient = builder.build(); // @formatter:on } return myHttpClient; }
private CloseableHttpClient getCloseableHttpClient(HttpProxy proxy) { if (null != proxy && proxy.isProxy()) { HttpHost proxyHost = new HttpHost(proxy.getHost(), proxy.getPort()); CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); if (null != proxy.getUser() && null != proxy.getPassword()) { credentialsProvider.setCredentials(new AuthScope(proxy.getHost(), proxy.getPort()), new UsernamePasswordCredentials(proxy.getUser(), proxy.getPassword())); } return HttpClients.custom().setProxy(proxyHost).setDefaultCredentialsProvider(credentialsProvider).build(); } return HttpClients.custom().setConnectionManager(connManager).build(); }
/** * this handles rest calls. each request creates a different httpClient object */ public HttpResponse doRequest(HttpRequestBase request, String username, String password) { HttpClientBuilder clientBuilder = HttpClients.custom(); HttpClientContext clientContext = HttpClientContext.create(); // configures the clientBuilder and clientContext if (username != null) { CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials(new AuthScope(bindAddress, restPort), new UsernamePasswordCredentials(username, password)); clientBuilder.setDefaultCredentialsProvider(credsProvider); } try { if (useSsl) { SSLContext ctx = SSLContext.getInstance("TLS"); ctx.init(new KeyManager[0], new TrustManager[] {new DefaultTrustManager()}, new SecureRandom()); clientBuilder.setSSLContext(ctx); clientBuilder.setSSLHostnameVerifier(new NoopHostnameVerifier()); } return clientBuilder.build().execute(host, request, clientContext); } catch (Exception e) { throw new RuntimeException(e.getMessage(), e); } }
protected HttpClient createHttpClient(GoogleAnalyticsConfig config) { ThreadSafeClientConnManager connManager = new ThreadSafeClientConnManager(); connManager.setDefaultMaxPerRoute(getDefaultMaxPerRoute(config)); BasicHttpParams params = new BasicHttpParams(); if (isNotEmpty(config.getUserAgent())) { params.setParameter(CoreProtocolPNames.USER_AGENT, config.getUserAgent()); } if (isNotEmpty(config.getProxyHost())) { params.setParameter(ConnRoutePNames.DEFAULT_PROXY, new HttpHost(config.getProxyHost(), config.getProxyPort())); } DefaultHttpClient client = new DefaultHttpClient(connManager, params); if (isNotEmpty(config.getProxyUserName())) { BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider(); credentialsProvider.setCredentials(new AuthScope(config.getProxyHost(), config.getProxyPort()), new UsernamePasswordCredentials(config.getProxyUserName(), config.getProxyPassword())); client.setCredentialsProvider(credentialsProvider); } return client; }
RestTemplate restTemplate = new RestTemplate(); CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials( new AuthScope("proxyHost", "proxyPort"), new UsernamePasswordCredentials("proxyUser", "proxyPass") ); HttpClientBuilder clientBuilder = HttpClientBuilder.create(); clientBuilder.useSystemProperties(); clientBuilder.setProxy(new HttpHost("proxyHost", "proxyPort")); clientBuilder.setDefaultCredentialsProvider(credsProvider); clientBuilder.setProxyAuthenticationStrategy(new ProxyAuthenticationStrategy()); CloseableHttpClient client = clientBuilder.build(); HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory(); factory.setHttpClient(client); restTemplate.setRequestFactory(factory);
HttpClientContext localContext = null; try { HttpHost target = new HttpHost( host, port, schema ); CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials( new AuthScope( target.getHostName(), target.getPort() ), new UsernamePasswordCredentials( user, password ) );
private static void addProxyCredentials(ApacheHttpTransport transport, String protocol) { Preconditions.checkArgument(protocol.equals("http") || protocol.equals("https")); String proxyHost = System.getProperty(protocol + ".proxyHost"); String proxyUser = System.getProperty(protocol + ".proxyUser"); String proxyPassword = System.getProperty(protocol + ".proxyPassword"); if (proxyHost == null || proxyUser == null || proxyPassword == null) { return; } String defaultProxyPort = protocol.equals("http") ? "80" : "443"; int proxyPort = Integer.parseInt(System.getProperty(protocol + ".proxyPort", defaultProxyPort)); DefaultHttpClient httpClient = (DefaultHttpClient) transport.getHttpClient(); httpClient .getCredentialsProvider() .setCredentials( new AuthScope(proxyHost, proxyPort), new UsernamePasswordCredentials(proxyUser, proxyPassword)); }