use of org.apache.http.auth.AuthScope in project sling by apache.
the class PreemptiveAuthInterceptor method process.
public void process(HttpRequest request, 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 not auth scheme has been initialized yet
if (authState.getAuthScheme() == null) {
AuthScope authScope = new AuthScope(targetHost.getHostName(), targetHost.getPort());
// Obtain credentials matching the target host
Credentials creds = credsProvider.getCredentials(authScope);
// If found, generate BasicScheme preemptively
if (creds != null) {
authState.setAuthScheme(new BasicScheme());
authState.setCredentials(creds);
}
}
}
use of org.apache.http.auth.AuthScope in project opennms by OpenNMS.
the class ScvEnabledRestClientImpl method getResponse.
// Setup a client with pre-emptive authentication
private CloseableHttpResponse getResponse(HttpGet httpget) throws Exception {
CloseableHttpResponse response = null;
HttpHost target = new HttpHost(url.getHost(), url.getPort(), url.getProtocol());
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(new AuthScope(target.getHostName(), target.getPort()), getCredentials());
CloseableHttpClient httpclient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();
AuthCache authCache = new BasicAuthCache();
BasicScheme basicAuth = new BasicScheme();
authCache.put(target, basicAuth);
HttpClientContext localContext = HttpClientContext.create();
localContext.setAuthCache(authCache);
response = httpclient.execute(target, httpget, localContext);
return response;
}
use of org.apache.http.auth.AuthScope in project geode by apache.
the class GeodeRestClient method doRequest.
public HttpResponse doRequest(HttpRequestBase request, String username, String password) throws Exception {
HttpHost targetHost = new HttpHost(bindAddress, restPort, protocol);
HttpClientBuilder clientBuilder = HttpClients.custom();
HttpClientContext clientContext = HttpClientContext.create();
// configures the clientBuilder and clientContext
if (username != null) {
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(new AuthScope(targetHost.getHostName(), targetHost.getPort()), new UsernamePasswordCredentials(username, password));
clientBuilder.setDefaultCredentialsProvider(credsProvider);
}
if (useHttps) {
SSLContext ctx = SSLContext.getInstance("TLS");
ctx.init(new KeyManager[0], new TrustManager[] { new DefaultTrustManager() }, new SecureRandom());
clientBuilder.setSSLContext(ctx);
clientBuilder.setSSLHostnameVerifier(new NoopHostnameVerifier());
}
return clientBuilder.build().execute(targetHost, request, clientContext);
}
use of org.apache.http.auth.AuthScope in project opennms by OpenNMS.
the class HttpClientWrapper method enablePreemptiveAuth.
protected void enablePreemptiveAuth(final HttpClientBuilder builder) {
/**
* Add an HttpRequestInterceptor that will perform preemptive authentication
* @see http://hc.apache.org/httpcomponents-client-4.0.1/tutorial/html/authentication.html
*/
final HttpRequestInterceptor preemptiveAuth = new HttpRequestInterceptor() {
@Override
public void process(final HttpRequest request, final HttpContext context) throws IOException {
if (context instanceof HttpClientContext) {
final HttpClientContext clientContext = (HttpClientContext) context;
final AuthState authState = clientContext.getTargetAuthState();
final CredentialsProvider credsProvider = clientContext.getCredentialsProvider();
final HttpHost targetHost = clientContext.getTargetHost();
// If not authentication scheme has been initialized yet
if (authState.getAuthScheme() == null) {
final AuthScope authScope = new AuthScope(targetHost.getHostName(), targetHost.getPort());
// Obtain credentials matching the target host
final Credentials creds = credsProvider.getCredentials(authScope);
// If found, generate BasicScheme preemptively
if (creds != null) {
authState.update(new BasicScheme(), creds);
}
}
} else {
throw new IllegalArgumentException("Not sure how to handle a non-HttpClientContext context.");
}
}
};
builder.addInterceptorFirst(preemptiveAuth);
}
use of org.apache.http.auth.AuthScope in project metron by apache.
the class TaxiiHandler method createContext.
private static HttpClientContext createContext(URL endpoint, String username, String password, int port) {
HttpClientContext context = null;
HttpHost target = new HttpHost(endpoint.getHost(), port, endpoint.getProtocol());
if (username != null && password != null) {
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(new AuthScope(target.getHostName(), target.getPort()), new UsernamePasswordCredentials(username, password));
// http://hc.apache.org/httpcomponents-client-ga/tutorial/html/authentication.html
AuthCache authCache = new BasicAuthCache();
authCache.put(target, new BasicScheme());
// Add AuthCache to the execution context
context = HttpClientContext.create();
context.setCredentialsProvider(credsProvider);
context.setAuthCache(authCache);
} else {
context = null;
}
return context;
}
Aggregations