List of usage examples for org.apache.http.auth UsernamePasswordCredentials UsernamePasswordCredentials
public UsernamePasswordCredentials(final String userName, final String password)
From source file:es.tsb.ltba.nomhad.httpclient.NomhadHttpClient.java
/** * Set the credentials, if the client was created without them. * //from www . ja v a 2 s . co m * @param usr * user * @param pwd * password */ public void setCredentials(String usr, String pwd) { httpClient.getCredentialsProvider().setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT), new UsernamePasswordCredentials(usr, pwd)); }
From source file:net.seedboxer.seedroid.utils.RestClient.java
public void AddBasicAuthentication(String username, String password) { credProvider = new BasicCredentialsProvider(); credProvider.setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT), new UsernamePasswordCredentials(username, password)); }
From source file:bad.robot.http.apache.ApacheHttpClientBuilder.java
@Override public ApacheHttpClientBuilder withBasicAuthCredentials(String username, String password, URL url) { this.authentications.add(new AuthenticatedHost(url, new UsernamePasswordCredentials(username, password))); return this; }
From source file:com.ibm.connectors.splunklog.SplunkHttpConnection.java
public Properties sendLogEvent(SplunkConnectionData connData, byte[] thePayload, Properties properties) throws ConnectorException { HttpClient myhClient = HttpClients.custom().setConnectionManager(this.cm).build(); HttpClientContext myhContext = HttpClientContext.create(); CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT), new UsernamePasswordCredentials(connData.getUser(), connData.getPass())); AuthCache authCache = new BasicAuthCache(); authCache.put(new HttpHost(connData.getHost(), Integer.parseInt(connData.getPort()), connData.getScheme()), new BasicScheme()); myhContext.setCredentialsProvider(credsProvider); myhContext.setAuthCache(authCache);/*from w w w . j a va 2s . c o m*/ HttpPost myhPost = new HttpPost(connData.getSplunkURI()); ByteArrayEntity payload = new ByteArrayEntity(thePayload); try { myhPost.setEntity(payload); HttpResponse response = myhClient.execute(myhPost, myhContext); Integer statusCode = response.getStatusLine().getStatusCode(); if (statusCode != 200 && !connData.getIgnoreSplunkErrors()) { throw new ConnectorException( "Error posting log event to Splunk: " + response.getStatusLine().toString()); } System.out.println(response.getStatusLine().toString()); properties.setProperty("status", String.valueOf(statusCode)); Integer leasedConns = Integer .valueOf(((PoolingHttpClientConnectionManager) this.cm).getTotalStats().getLeased()); properties.setProperty("conns_leased", leasedConns.toString()); Integer availConns = Integer .valueOf(((PoolingHttpClientConnectionManager) this.cm).getTotalStats().getAvailable()); properties.setProperty("conns_available", availConns.toString()); } catch (IOException e) { e.fillInStackTrace(); throw new ConnectorException(e.toString()); } return properties; }
From source file:org.superbiz.AuthBeanTest.java
private String get(final String user, final String password) { final BasicCredentialsProvider basicCredentialsProvider = new BasicCredentialsProvider(); basicCredentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(user, password)); final CloseableHttpClient client = HttpClients.custom() .setDefaultCredentialsProvider(basicCredentialsProvider).build(); final HttpHost httpHost = new HttpHost(webapp.getHost(), webapp.getPort(), webapp.getProtocol()); final AuthCache authCache = new BasicAuthCache(); final BasicScheme basicAuth = new BasicScheme(); authCache.put(httpHost, basicAuth);/*from ww w. j a v a 2 s . c o m*/ final HttpClientContext context = HttpClientContext.create(); context.setAuthCache(authCache); final HttpGet get = new HttpGet(webapp.toExternalForm() + "servlet"); CloseableHttpResponse response = null; try { response = client.execute(httpHost, get, context); return response.getStatusLine().getStatusCode() + " " + EntityUtils.toString(response.getEntity()); } catch (final IOException e) { throw new IllegalStateException(e); } finally { try { IO.close(response); } catch (final IOException e) { // no-op } } }
From source file:test.integ.be.fedict.hsm.rest.security.SecurityTest.java
@Test public void testInvocation() throws Exception { DefaultHttpClient httpClient = new DefaultHttpClient(); httpClient.getCredentialsProvider().setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("username", "password")); HttpGet httpGet = new HttpGet(this.baseURL + "rest/security/test"); HttpResponse httpResponse = httpClient.execute(httpGet); String result = EntityUtils.toString(httpResponse.getEntity()); LOG.debug("result: " + result); StatusLine statusLine = httpResponse.getStatusLine(); int statusCode = statusLine.getStatusCode(); assertEquals(200, statusCode);/* ww w . ja va2 s . com*/ }
From source file:org.jasig.portlet.emailpreview.service.auth.CachedPasswordAuthenticationService.java
public Credentials getCredentials(PortletRequest req, MailStoreConfiguration config) { String ntlmDomain = config.getAdditionalProperties().get(MailPreferences.EXCHANGE_DOMAIN.getKey()); // If the domain is specified, we are authenticating to a domain so we need to return NT credentials if (StringUtils.isNotBlank(ntlmDomain)) { String username = getMailAccountName(req, config); return createNTCredentials(ntlmDomain, username, getPassword(req)); }/* www. j a va 2 s. co m*/ return new UsernamePasswordCredentials(getMailAccountName(req, config), getPassword(req)); }
From source file:RestApiHttpClient.java
/** * Create an new {@link RestApiHttpClient} instance with Endpoint, username and API-Key * /* w ww .jav a 2 s.c om*/ * @param apiEndpoint The Hostname and Api-Endpoint (http://www.example.com/api) * @param username Shopware Username * @param password Api-Key from User-Administration */ public RestApiHttpClient(URL apiEndpoint, String username, String password) { this.apiEndpoint = apiEndpoint; BasicHttpContext context = new BasicHttpContext(); this.localContext = HttpClientContext.adapt(context); HttpHost target = new HttpHost(this.apiEndpoint.getHost(), -1, this.apiEndpoint.getProtocol()); this.localContext.setTargetHost(target); TargetAuthenticationStrategy authStrat = new TargetAuthenticationStrategy(); UsernamePasswordCredentials creds = new UsernamePasswordCredentials(username, password); BasicCredentialsProvider credsProvider = new BasicCredentialsProvider(); AuthScope aScope = new AuthScope(target.getHostName(), target.getPort()); credsProvider.setCredentials(aScope, creds); BasicAuthCache authCache = new BasicAuthCache(); // Digest Authentication DigestScheme digestAuth = new DigestScheme(Charset.forName("UTF-8")); authCache.put(target, digestAuth); this.localContext.setAuthCache(authCache); this.localContext.setCredentialsProvider(credsProvider); ArrayList<Header> defHeaders = new ArrayList<>(); defHeaders.add(new BasicHeader(HttpHeaders.ACCEPT, ContentType.APPLICATION_JSON.getMimeType())); this.httpclient = HttpClients.custom().useSystemProperties().setTargetAuthenticationStrategy(authStrat) .disableRedirectHandling() // make Rest-API Endpoint GZIP-Compression enable comment this out // Response-Compression is also possible .disableContentCompression().setDefaultHeaders(defHeaders) .setDefaultCredentialsProvider(credsProvider).build(); }