List of usage examples for org.apache.http.client AuthCache put
void put(HttpHost host, AuthScheme authScheme);
From source file:com.github.tomakehurst.wiremock.testsupport.WireMockTestClient.java
public WireMockResponse getWithPreemptiveCredentials(String url, int port, String username, String password) { HttpHost target = new HttpHost("localhost", port); HttpClient httpClient = httpClientWithPreemptiveAuth(target, username, password); AuthCache authCache = new BasicAuthCache(); BasicScheme basicAuth = new BasicScheme(); authCache.put(target, basicAuth); HttpClientContext localContext = HttpClientContext.create(); localContext.setAuthCache(authCache); try {/* w w w . j a v a 2 s . c o m*/ HttpGet httpget = new HttpGet(url); HttpResponse response = httpClient.execute(target, httpget, localContext); return new WireMockResponse(response); } catch (IOException e) { return throwUnchecked(e, WireMockResponse.class); } }
From source file:org.sonatype.nexus.testsuite.security.nexus4257.Nexus4257CookieVerificationIT.java
@Test public void testCookieForStateFullClient() throws Exception { setAnonymousAccess(false);/*from www.j a v a 2 s.c om*/ TestContext context = TestContainer.getInstance().getTestContext(); String username = context.getAdminUsername(); String password = context.getPassword(); String url = this.getBaseNexusUrl() + "content/"; URI nexusBaseURI = new URI(url); DefaultHttpClient httpClient = new DefaultHttpClient(); httpClient.getParams().setParameter(CoreProtocolPNames.USER_AGENT, "SomeUAThatWillMakeMeLookStateful/1.0"); final BasicHttpContext localcontext = new BasicHttpContext(); final HttpHost targetHost = new HttpHost(nexusBaseURI.getHost(), nexusBaseURI.getPort(), nexusBaseURI.getScheme()); httpClient.getCredentialsProvider().setCredentials( new AuthScope(targetHost.getHostName(), targetHost.getPort()), new UsernamePasswordCredentials(username, password)); AuthCache authCache = new BasicAuthCache(); BasicScheme basicAuth = new BasicScheme(); authCache.put(targetHost, basicAuth); localcontext.setAttribute(ClientContext.AUTH_CACHE, authCache); // stateful clients must login first, since other rest urls create no sessions String loginUrl = this.getBaseNexusUrl() + "service/local/authentication/login"; assertThat(executeAndRelease(httpClient, new HttpGet(loginUrl), localcontext), equalTo(200)); // after login check content but make sure only cookie is used httpClient.getCredentialsProvider().clear(); HttpGet getMethod = new HttpGet(url); assertThat(executeAndRelease(httpClient, getMethod, null), equalTo(200)); Cookie sessionCookie = this.getSessionCookie(httpClient.getCookieStore().getCookies()); assertThat("Session Cookie not set", sessionCookie, notNullValue()); httpClient.getCookieStore().clear(); // remove cookies // do not set the cookie, expect failure HttpGet failedGetMethod = new HttpGet(url); assertThat(executeAndRelease(httpClient, failedGetMethod, null), equalTo(401)); // set the cookie expect a 200, If a cookie is set, and cannot be found on the server, the response will fail // with a 401 httpClient.getCookieStore().addCookie(sessionCookie); getMethod = new HttpGet(url); assertThat(executeAndRelease(httpClient, getMethod, null), equalTo(200)); }
From source file:com.elastic.support.util.RestExec.java
public HttpClientContext getLocalContext(HttpHost httpHost) throws Exception { AuthCache authCache = new BasicAuthCache(); // Generate BASIC scheme object and add it to the local // auth cache BasicScheme basicAuth = new BasicScheme(); authCache.put(httpHost, basicAuth); HttpClientContext localContext = HttpClientContext.create(); localContext.setAuthCache(authCache); return localContext; }
From source file:pl.wavesoftware.wfirma.api.simple.mapper.SimpleGateway.java
private String get(@Nonnull HttpRequest httpRequest) throws WFirmaException { httpRequest.setHeader("Accept", "text/xml"); HttpHost target = getTargetHost();//from ww w .j a v a 2s . c o m CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials(new AuthScope(target.getHostName(), target.getPort()), new UsernamePasswordCredentials(credentials.getConsumerKey(), credentials.getConsumerSecret())); try (CloseableHttpClient httpclient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider) .build()) { // Create AuthCache instance AuthCache authCache = new BasicAuthCache(); // Generate BASIC scheme object and add it to the local // auth cache BasicScheme basicAuth = new BasicScheme(); authCache.put(target, basicAuth); // Add AuthCache to the execution context HttpClientContext localContext = HttpClientContext.create(); localContext.setAuthCache(authCache); try (CloseableHttpResponse response = httpclient.execute(target, httpRequest, localContext)) { return getContent(response); } } catch (IOException ioe) { throw new RemoteGatewayException(ioe); } }
From source file:org.sonatype.nexus.testsuite.NexusHttpsITSupport.java
/** * @return Cache with preemptive auth enabled for Nexus *//*from w ww .j a va2 s .co m*/ protected AuthCache basicAuthCache() { String hostname = nexusUrl.getHost(); AuthCache authCache = new BasicAuthCache(); HttpHost hostHttp = new HttpHost(hostname, nexusUrl.getPort(), "http"); HttpHost hostHttps = new HttpHost(hostname, nexusSecureUrl.getPort(), "https"); authCache.put(hostHttp, new BasicScheme()); authCache.put(hostHttps, new BasicScheme()); return authCache; }
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 w ww . ja v a 2s .c o m }
From source file:org.apache.cloudstack.cloudian.client.CloudianClient.java
public CloudianClient(final String host, final Integer port, final String scheme, final String username, final String password, final boolean validateSSlCertificate, final int timeout) throws KeyStoreException, NoSuchAlgorithmException, KeyManagementException { final CredentialsProvider provider = new BasicCredentialsProvider(); provider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password)); final HttpHost adminHost = new HttpHost(host, port, scheme); final AuthCache authCache = new BasicAuthCache(); authCache.put(adminHost, new BasicScheme()); this.adminApiUrl = adminHost.toURI(); this.httpContext = HttpClientContext.create(); this.httpContext.setCredentialsProvider(provider); this.httpContext.setAuthCache(authCache); final RequestConfig config = RequestConfig.custom().setConnectTimeout(timeout * 1000) .setConnectionRequestTimeout(timeout * 1000).setSocketTimeout(timeout * 1000).build(); if (!validateSSlCertificate) { final SSLContext sslcontext = SSLUtils.getSSLContext(); sslcontext.init(null, new X509TrustManager[] { new TrustAllManager() }, new SecureRandom()); final SSLConnectionSocketFactory factory = new SSLConnectionSocketFactory(sslcontext, NoopHostnameVerifier.INSTANCE); this.httpClient = HttpClientBuilder.create().setDefaultCredentialsProvider(provider) .setDefaultRequestConfig(config).setSSLSocketFactory(factory).build(); } else {/*from w w w .java2 s. c om*/ this.httpClient = HttpClientBuilder.create().setDefaultCredentialsProvider(provider) .setDefaultRequestConfig(config).build(); } }
From source file:com.emc.storageos.driver.dellsc.scapi.rest.RestClient.java
/** * Instantiates a new Rest client.// ww w . ja v a 2s. 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(); } }