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; }
/** * Set credentials to field <code>credsProvider</code>. * * @param username the username, part of credentials * @param password the password, part of credentials */ public void setCredentials(String username, String password) { credsProvider.setCredentials( new AuthScope(host.getHostName(), host.getPort(), AuthScope.ANY_REALM), new UsernamePasswordCredentials(username, password)); }
DefaultHttpClient httpclient = new DefaultHttpClient(); httpclient.getCredentialsProvider().setCredentials( new AuthScope("localhost", 443), new UsernamePasswordCredentials("username", "password"));
DefaultHttpClient httpclient = new DefaultHttpClient(); httpclient.getCredentialsProvider().setCredentials( new AuthScope("PROXY HOST", 8080), new UsernamePasswordCredentials("username", "password")); HttpHost targetHost = new HttpHost("TARGET HOST", 443, "https"); HttpHost proxy = new HttpHost("PROXY HOST", 8080); httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
HttpHost proxy = new HttpHost(proxyHost, proxyPort); httpclient.getParams().setParameter( ConnRoutePNames.DEFAULT_PROXY, proxy); && config.getProxyPassword() != null) { httpclient.getCredentialsProvider().setCredentials( new AuthScope(proxyHost, proxyPort), new NTCredentials(config.getProxyUsername(), config.getProxyPassword(),
BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider(); AuthScope authScope = new AuthScope(proxyHost.getHostName(), proxyHost.getPort(), ANY_REALM, NTLM); Credentials creds = new NTCredentials(ntlmUsername, password, credentialsProvider.setCredentials(authScope, creds); authScope = new AuthScope(proxyHost.getHostName(), proxyHost.getPort()); creds = new UsernamePasswordCredentials(username, password); credentialsProvider.setCredentials(authScope, creds);
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(); }
/** * Set NTLM authentication credentials to be used for the given host and port. * * @param host * @param port * @param user * @param pass * @param workstation * @param domain */ public void ntlm(String host, int port, String user, String pass, String workstation, String domain) { builder.getClient().getCredentialsProvider().setCredentials( new AuthScope(host, port), new NTCredentials(user, pass, workstation, domain) ); }
AndroidHttpClient httpClient = AndroidHttpClient.newInstance("User Agent"); URL urlObj = new URL(url); HttpHost host = new HttpHost(urlObj.getHost(), urlObj.getPort(), urlObj.getProtocol()); AuthScope scope = new AuthScope(urlObj.getHost(), urlObj.getPort()); HttpContext credContext = new BasicHttpContext(); HttpPost httpPost = new HttpPost (url); httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairsArrayList)); // Execute post request and get http response HttpResponse httpResponse = httpClient.execute(host, httpPost, credContext); httpClient.close();
@SuppressWarnings("UnusedReturnValue") ResteasyClientBuilder defaultProxy(String hostname, int port, final String scheme, String username, String password) { super.defaultProxy(hostname, port, scheme); if (username != null && password != null) { proxyCredentials = new BasicCredentialsProvider(); proxyCredentials.setCredentials(new AuthScope(hostname, port), new UsernamePasswordCredentials(username, password)); } return this; }
client = getHttpClient(); client.getCredentialsProvider().setCredentials( new AuthScope(null, -1, null), new UsernamePasswordCredentials("Username", "Password")); HttpHost httpproxy = new HttpHost("hostname", "portname", "http"); client.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, httpproxy);
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)); }
HttpHost proxy = new HttpHost(proxyHost, proxyPort); httpclient.getParams().setParameter( ConnRoutePNames.DEFAULT_PROXY, proxy); && config.getProxyPassword() != null) { httpclient.getCredentialsProvider().setCredentials( new AuthScope(proxyHost, proxyPort), new NTCredentials(config.getProxyUsername(), config.getProxyPassword(),
/** * BASIC authentication<br/> * Official Example: https://hc.apache.org/httpcomponents-client-ga/httpclient/examples/org * /apache/http/examples/client/ClientAuthentication.java */ private void addBasicCredentials(BasicAuthInfo authInfo, Map<AuthScope, Credentials> credentialsMap) { logger.info("BASIC authentication for: {}", authInfo.getLoginTarget()); Credentials credentials = new UsernamePasswordCredentials(authInfo.getUsername(), authInfo.getPassword()); credentialsMap.put(new AuthScope(authInfo.getHost(), authInfo.getPort()), credentials); }
@Override public HttpClientBuilder configureHttpClientBuilder(RestConfig config, HttpClientBuilder httpClientBuilder) { String username = config.getUsername(); if (username != null) { BasicCredentialsProvider prov = new BasicCredentialsProvider(); prov.setCredentials(new AuthScope(config.getHost(), config.getPort(), AuthScope.ANY_REALM), new UsernamePasswordCredentials(username, config.getPassword())); return httpClientBuilder.setDefaultCredentialsProvider(prov); } return httpClientBuilder; } }
CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials(new AuthScope(null, -1, null), use_jaas_creds); Registry<AuthSchemeProvider> authSchemeRegistry = RegistryBuilder.<AuthSchemeProvider>create().register(AuthSchemes.SPNEGO, new SPNegoSchemeFactory(true)).build(); CloseableHttpClient httpclient = HttpClients.custom().setDefaultAuthSchemeRegistry(authSchemeRegistry).setDefaultCredentialsProvider(credsProvider).build();
private void initHttpRestClient() { httpClient = new DefaultHttpClient(); restHost = new HttpHost(kaaRestHost, kaaRestPort, "http"); AuthCache authCache = new BasicAuthCache(); BasicScheme basicAuth = new BasicScheme(); authCache.put(restHost, basicAuth); CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials( new AuthScope(kaaRestHost, kaaRestPort, AuthScope.ANY_REALM), new UsernamePasswordCredentials(kaaRestUser, kaaRestPassword)); httpContext = new BasicHttpContext(); httpContext.setAttribute(ClientContext.AUTH_CACHE, authCache); httpContext.setAttribute(ClientContext.CREDS_PROVIDER, credsProvider); }
String ipStr = "the.proxy.ip.string"; int port = 8080; String proxyLogin = "your login name"; String proxyPassword = "your password"; httpClient.getCredentialsProvider().setCredentials( new AuthScope(ipStr, port), new UsernamePasswordCredentials(proxyLogin, proxyPassword)); HttpHost httpHost = new HttpHost(ipStr, port, "http"); httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, httpHost);
/** * Set authentication credentials to be used for the given host and port. * * @param host * @param port * @param user * @param pass */ public void basic(String host, int port, String user, String pass) { builder.getClient().getCredentialsProvider().setCredentials( new AuthScope(host, port), new UsernamePasswordCredentials(user, pass) ); }
AmazonHttpClient.log.info("Configuring Proxy. Proxy Host: " + proxyHost + " " + "Proxy Port: " + proxyPort); final HttpHost proxyHttpHost = new HttpHost(proxyHost, proxyPort); httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxyHttpHost); httpClient.getCredentialsProvider().setCredentials( new AuthScope(proxyHost, proxyPort), new NTCredentials(proxyUsername, proxyPassword, proxyWorkstation, proxyDomain));