List of usage examples for org.apache.http.impl.client BasicCredentialsProvider setCredentials
public void setCredentials(final AuthScope authscope, final Credentials credentials)
From source file:org.springframework.cloud.dataflow.shell.command.support.HttpClientUtils.java
/** * 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./*from ww w .j a v a 2 s .c om*/ * * @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, URI host, 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 HttpHost targetHost = new HttpHost(host.getHost(), host.getPort(), host.getScheme()); final HttpComponentsClientHttpRequestFactory requestFactory = new PreemptiveBasicAuthHttpComponentsClientHttpRequestFactory( httpClient, targetHost); restTemplate.setRequestFactory(requestFactory); }
From source file:org.eclipse.cft.server.core.internal.client.RestUtils.java
public static ClientHttpRequestFactory createRequestFactory(HttpProxyConfiguration httpProxyConfiguration, boolean trustSelfSignedCerts, boolean disableRedirectHandling) { HttpClientBuilder httpClientBuilder = HttpClients.custom().useSystemProperties(); if (trustSelfSignedCerts) { httpClientBuilder.setSslcontext(buildSslContext()); httpClientBuilder.setHostnameVerifier(BROWSER_COMPATIBLE_HOSTNAME_VERIFIER); }/*w w w . j a v a2 s.com*/ if (disableRedirectHandling) { httpClientBuilder.disableRedirectHandling(); } 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; }
From source file:org.lorislab.armonitor.client.MonitorClient.java
/** * Creates the monitor service./*from w w w. jav a 2 s . c o m*/ * * @param url the URL. * @param username the user name. * @param password the password. * @return the monitor service. */ private static MonitorService createService(URL url, String username, String password, boolean auth) { ResteasyProviderFactory rpf = ResteasyProviderFactory.getInstance(); RegisterBuiltin.register(rpf); String tmp = url.toString(); if (!tmp.endsWith("/")) { tmp = tmp + "/"; } tmp = tmp + APP_URL; if (auth) { Credentials credentials = new UsernamePasswordCredentials(username, password); DefaultHttpClient httpClient = new DefaultHttpClient(); BasicCredentialsProvider provider = new BasicCredentialsProvider(); provider.setCredentials(AuthScope.ANY, credentials); httpClient.setCredentialsProvider(provider); return ProxyFactory.create(MonitorService.class, tmp, new ApacheHttpClient4Executor(httpClient)); } return ProxyFactory.create(MonitorService.class, tmp); }
From source file:com.ibm.ecod.watson.RetrieveAndRankSolrJExample.java
private static HttpClient createHttpClient(String uri, String username, String password) { final URI scopeUri = URI.create(uri); final BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider(); credentialsProvider.setCredentials(new AuthScope(scopeUri.getHost(), scopeUri.getPort()), new UsernamePasswordCredentials(username, password)); final HttpClientBuilder builder = HttpClientBuilder.create().setMaxConnTotal(128).setMaxConnPerRoute(32) .setDefaultRequestConfig(// w ww .j a v a2s. c o m RequestConfig.copy(RequestConfig.DEFAULT).setRedirectsEnabled(true).build()); builder.setDefaultCredentialsProvider(credentialsProvider); return builder.build(); }
From source file:org.lorislab.armonitor.util.RestClient.java
/** * Gets the rest-service client./* ww w .j a va2s . co m*/ * * @param <T> the rest-service client implementation. * @param clazz the rest-service class. * @param url the server URL. * @param username the username. * @param password the password. * @param auth the authentication flag. * @exception Exception if the method fails. * * @return the the rest-service client instance. */ public static <T> T getClient(final Class<T> clazz, String url, boolean auth, String username, char[] password) throws Exception { DefaultHttpClient httpClient = new DefaultHttpClient(); if (url.startsWith(HTTPS)) { SSLSocketFactory sslSocketFactory = new SSLSocketFactory(new TrustSelfSignedStrategy(), SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); httpClient.getConnectionManager().getSchemeRegistry() .register(new Scheme(HTTPS, 443, sslSocketFactory)); } if (auth) { BasicCredentialsProvider provider = new BasicCredentialsProvider(); Credentials credentials = new UsernamePasswordCredentials(username, new String(password)); provider.setCredentials(AuthScope.ANY, credentials); httpClient.setCredentialsProvider(provider); } return ProxyFactory.create(clazz, url, new ApacheHttpClient4Executor(httpClient)); }
From source file:com.ibm.watson.developer_cloud.retrieve_and_rank.RetrieveAndRankSolrJExample.java
private static HttpClient createHttpClient(String uri, String username, String password) { final URI scopeUri = URI.create(uri); final BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider(); credentialsProvider.setCredentials(new AuthScope(scopeUri.getHost(), scopeUri.getPort()), new UsernamePasswordCredentials(username, password)); final HttpClientBuilder builder = HttpClientBuilder.create().setMaxConnTotal(128).setMaxConnPerRoute(32) .setDefaultRequestConfig(//from w w w . j a v a 2 s. co m RequestConfig.copy(RequestConfig.DEFAULT).setRedirectsEnabled(true).build()) .setDefaultCredentialsProvider(credentialsProvider) .addInterceptorFirst(new PreemptiveAuthInterceptor()); return builder.build(); }
From source file:com.predic8.membrane.test.AssertUtils.java
private static DefaultHttpClient getAuthenticatingHttpClient(String host, int port, String user, String pass) { DefaultHttpClient hc = new DefaultHttpClient(); HttpRequestInterceptor preemptiveAuth = new HttpRequestInterceptor() { public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException { AuthState authState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE); CredentialsProvider credsProvider = (CredentialsProvider) context .getAttribute(ClientContext.CREDS_PROVIDER); HttpHost targetHost = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST); if (authState.getAuthScheme() == null) { AuthScope authScope = new AuthScope(targetHost.getHostName(), targetHost.getPort()); Credentials creds = credsProvider.getCredentials(authScope); if (creds != null) { authState.update(new BasicScheme(), creds); }//from www . j av a 2s.c om } } }; hc.addRequestInterceptor(preemptiveAuth, 0); Credentials defaultcreds = new UsernamePasswordCredentials(user, pass); BasicCredentialsProvider bcp = new BasicCredentialsProvider(); bcp.setCredentials(new AuthScope(host, port, AuthScope.ANY_REALM), defaultcreds); hc.setCredentialsProvider(bcp); hc.setCookieStore(new BasicCookieStore()); return hc; }
From source file:nl.knaw.dans.easy.sword2examples.Common.java
public static CloseableHttpClient createHttpClient(URI uri, String uid, String pw) { BasicCredentialsProvider credsProv = new BasicCredentialsProvider(); credsProv.setCredentials(new AuthScope(uri.getHost(), uri.getPort()), new UsernamePasswordCredentials(uid, pw)); return HttpClients.custom().setDefaultCredentialsProvider(credsProv).build(); }
From source file:edu.si.services.camel.fcrepo.FcrepoIntegrationTest.java
@BeforeClass public static void loadObjectsIntoFedora() throws IOException, InterruptedException { BasicCredentialsProvider provider = new BasicCredentialsProvider(); provider.setCredentials(ANY, new UsernamePasswordCredentials(System.getProperty("si.fedora.user"), System.getProperty("si.fedora.password"))); httpClient = HttpClientBuilder.create().setDefaultCredentialsProvider(provider).build(); ingest(PID, Paths.get(BUILD_DIR, TEST_FOXML)); }
From source file:org.elasticsearch.client.RestClientSingleHostIntegTests.java
private static RestClient createRestClient(final boolean useAuth, final boolean usePreemptiveAuth) { // provide the username/password for every request final BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider(); credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("user", "pass")); final RestClientBuilder restClientBuilder = RestClient .builder(new HttpHost(httpServer.getAddress().getHostString(), httpServer.getAddress().getPort())) .setDefaultHeaders(defaultHeaders); if (pathPrefix.length() > 0) { // sometimes cut off the leading slash restClientBuilder.setPathPrefix(randomBoolean() ? pathPrefix.substring(1) : pathPrefix); }/*ww w. jav a 2 s . c o m*/ if (useAuth) { restClientBuilder.setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() { @Override public HttpAsyncClientBuilder customizeHttpClient(final HttpAsyncClientBuilder httpClientBuilder) { if (usePreemptiveAuth == false) { // disable preemptive auth by ignoring any authcache httpClientBuilder.disableAuthCaching(); } return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider); } }); } return restClientBuilder.build(); }