use of org.apache.http.impl.auth.BasicScheme in project SmartAndroidSource by jaychou2012.
the class PreemtiveAuthorizationHttpRequestInterceptor method process.
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.setAuthScheme(new BasicScheme());
authState.setCredentials(creds);
}
}
}
use of org.apache.http.impl.auth.BasicScheme in project elasticsearch by elastic.
the class RestClient method setHosts.
/**
* Replaces the hosts that the client communicates with.
* @see HttpHost
*/
public synchronized void setHosts(HttpHost... hosts) {
if (hosts == null || hosts.length == 0) {
throw new IllegalArgumentException("hosts must not be null nor empty");
}
Set<HttpHost> httpHosts = new HashSet<>();
AuthCache authCache = new BasicAuthCache();
for (HttpHost host : hosts) {
Objects.requireNonNull(host, "host cannot be null");
httpHosts.add(host);
authCache.put(host, new BasicScheme());
}
this.hostTuple = new HostTuple<>(Collections.unmodifiableSet(httpHosts), authCache);
this.blacklist.clear();
}
use of org.apache.http.impl.auth.BasicScheme in project ats-framework by Axway.
the class HttpClient method setupAuthentication.
/**
* Set up authentication for HTTP Basic/HTTP Digest/SPNEGO.
*
* @param httpClientBuilder The client builder
* @return The context
* @throws HttpException
*/
private HttpClientContext setupAuthentication(HttpClientBuilder httpClientBuilder) throws HttpException {
HttpClientContext localContext = null;
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT), new UsernamePasswordCredentials(username, password));
httpClientBuilder = httpClientBuilder.setDefaultCredentialsProvider(credsProvider);
if (authType == AuthType.always) {
AuthCache authCache = new BasicAuthCache();
// Generate BASIC scheme object and add it to the local
// auth cache
BasicScheme basicAuth = new BasicScheme();
URL uri = null;
try {
uri = new URL(url);
} catch (MalformedURLException e) {
throw new HttpException("Exception occurred creating URL from '" + url + "'.", e);
}
HttpHost target = new HttpHost(uri.getHost(), uri.getPort(), uri.getProtocol());
authCache.put(target, basicAuth);
// Add AuthCache to the execution context
localContext = HttpClientContext.create();
localContext.setAuthCache(authCache);
} else {
if (!StringUtils.isNullOrEmpty(kerberosServicePrincipalName)) {
GssClient gssClient = new GssClient(username, password, kerberosClientKeytab, krb5ConfFile);
AuthSchemeProvider nsf = new SPNegoSchemeFactory(gssClient, kerberosServicePrincipalName, kerberosServicePrincipalType);
final Registry<AuthSchemeProvider> authSchemeRegistry = RegistryBuilder.<AuthSchemeProvider>create().register(AuthSchemes.SPNEGO, nsf).build();
httpClientBuilder.setDefaultAuthSchemeRegistry(authSchemeRegistry);
}
}
return localContext;
}
use of org.apache.http.impl.auth.BasicScheme in project opennms by OpenNMS.
the class RestSessionIT method queryUri.
private Header[] queryUri(final String uri, final String header) throws IOException {
final HttpGet httpGet = new HttpGet(getBaseUrl() + uri);
final HttpHost targetHost = new HttpHost(httpGet.getURI().getHost(), httpGet.getURI().getPort(), httpGet.getURI().getScheme());
final CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(new AuthScope(targetHost.getHostName(), targetHost.getPort()), new UsernamePasswordCredentials("admin", "admin"));
final AuthCache authCache = new BasicAuthCache();
final BasicScheme basicAuth = new BasicScheme();
authCache.put(targetHost, basicAuth);
final HttpClientContext context = HttpClientContext.create();
context.setCredentialsProvider(credsProvider);
context.setAuthCache(authCache);
final CloseableHttpResponse closeableHttpResponse = HttpClients.createDefault().execute(targetHost, httpGet, context);
closeableHttpResponse.close();
return closeableHttpResponse.getHeaders(header);
}
use of org.apache.http.impl.auth.BasicScheme in project jmeter by apache.
the class HTTPHC4Impl method executeRequest.
/**
* Execute request either as is or under PrivilegedAction
* if a Subject is available for url
* @param httpClient the {@link CloseableHttpClient} to be used to execute the httpRequest
* @param httpRequest the {@link HttpRequest} to be executed
* @param localContext th {@link HttpContext} to be used for execution
* @param url the target url (will be used to look up a possible subject for the execution)
* @return the result of the execution of the httpRequest
* @throws IOException
* @throws ClientProtocolException
*/
private CloseableHttpResponse executeRequest(final CloseableHttpClient httpClient, final HttpRequestBase httpRequest, final HttpContext localContext, final URL url) throws IOException, ClientProtocolException {
AuthManager authManager = getAuthManager();
if (authManager != null) {
Subject subject = authManager.getSubjectForUrl(url);
if (subject != null) {
try {
return Subject.doAs(subject, (PrivilegedExceptionAction<CloseableHttpResponse>) () -> httpClient.execute(httpRequest, localContext));
} catch (PrivilegedActionException e) {
log.error("Can't execute httpRequest with subject: {}", subject, e);
throw new RuntimeException("Can't execute httpRequest with subject:" + subject, e);
}
}
if (BASIC_AUTH_PREEMPTIVE) {
Authorization authorization = authManager.getAuthForURL(url);
if (authorization != null && Mechanism.BASIC_DIGEST.equals(authorization.getMechanism())) {
HttpHost target = new HttpHost(url.getHost(), url.getPort(), url.getProtocol());
// 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
localContext.setAttribute(HttpClientContext.AUTH_CACHE, authCache);
}
}
}
return httpClient.execute(httpRequest, localContext);
}
Aggregations