use of org.apache.http.client.AuthCache 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.client.AuthCache 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.client.AuthCache 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