use of org.apache.http.auth.UsernamePasswordCredentials in project service-proxy by membrane.
the class AssertUtils method getAuthenticatingHttpClient.
private static HttpClient getAuthenticatingHttpClient(String host, int port, String user, String pass) {
Credentials defaultcreds = new UsernamePasswordCredentials(user, pass);
BasicCredentialsProvider bcp = new BasicCredentialsProvider();
bcp.setCredentials(new AuthScope(host, port, AuthScope.ANY_REALM), defaultcreds);
HttpRequestInterceptor preemptiveAuth = new HttpRequestInterceptor() {
public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException {
AuthState authState = (AuthState) context.getAttribute(HttpClientContext.TARGET_AUTH_STATE);
CredentialsProvider credsProvider = (CredentialsProvider) context.getAttribute(HttpClientContext.CREDS_PROVIDER);
HttpHost targetHost = (HttpHost) context.getAttribute(HttpCoreContext.HTTP_TARGET_HOST);
if (authState.getAuthScheme() == null) {
AuthScope authScope = new AuthScope(targetHost.getHostName(), targetHost.getPort());
Credentials creds = credsProvider.getCredentials(authScope);
if (creds != null) {
authState.update(new BasicScheme(), creds);
}
}
}
};
HttpClient hc = HttpClientBuilder.create().setDefaultCookieStore(new BasicCookieStore()).setDefaultCredentialsProvider(bcp).addInterceptorFirst(preemptiveAuth).build();
return hc;
}
use of org.apache.http.auth.UsernamePasswordCredentials in project OpenOLAT by OpenOLAT.
the class WebSMSProvider method setCredentials.
/**
* Method means for unit tests. The changes are not persisted.
*
* @param username
* @param password
*/
protected void setCredentials(String username, String password) {
this.username = username;
this.password = password;
provider.setCredentials(new AuthScope("api.websms.com", 443), new UsernamePasswordCredentials(username, password));
}
use of org.apache.http.auth.UsernamePasswordCredentials in project rdf4j by eclipse.
the class SPARQLProtocolSession method setUsernameAndPasswordForUrl.
protected void setUsernameAndPasswordForUrl(String username, String password, String url) {
if (username != null && password != null) {
logger.debug("Setting username '{}' and password for server at {}.", username, url);
java.net.URI requestURI = java.net.URI.create(url);
String host = requestURI.getHost();
int port = requestURI.getPort();
AuthScope scope = new AuthScope(host, port);
UsernamePasswordCredentials cred = new UsernamePasswordCredentials(username, password);
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(scope, cred);
httpContext.setCredentialsProvider(credsProvider);
AuthCache authCache = new BasicAuthCache();
BasicScheme basicAuth = new BasicScheme();
HttpHost httpHost = new HttpHost(requestURI.getHost(), requestURI.getPort(), requestURI.getScheme());
authCache.put(httpHost, basicAuth);
httpContext.setAuthCache(authCache);
} else {
httpContext.removeAttribute(HttpClientContext.AUTH_CACHE);
httpContext.removeAttribute(HttpClientContext.CREDS_PROVIDER);
}
}
use of org.apache.http.auth.UsernamePasswordCredentials in project activemq-artemis by apache.
the class UriStrategy method initAuthentication.
protected void initAuthentication() {
if (registration.getAuthenticationMechanism() != null) {
if (registration.getAuthenticationMechanism().getType() instanceof BasicAuth) {
BasicAuth basic = (BasicAuth) registration.getAuthenticationMechanism().getType();
UsernamePasswordCredentials creds = new UsernamePasswordCredentials(basic.getUsername(), basic.getPassword());
AuthScope authScope = new AuthScope(AuthScope.ANY);
((DefaultHttpClient) client).getCredentialsProvider().setCredentials(authScope, creds);
localContext = new BasicHttpContext();
// Generate BASIC scheme object and stick it to the local execution context
BasicScheme basicAuth = new BasicScheme();
localContext.setAttribute("preemptive-auth", basicAuth);
// Add as the first request interceptor
((DefaultHttpClient) client).addRequestInterceptor(new PreemptiveAuth(), 0);
executor.setHttpContext(localContext);
}
}
}
use of org.apache.http.auth.UsernamePasswordCredentials in project watchdog by TestRoots.
the class NetworkUtils method createAuthenticatedHttpClient.
/**
* Creates an HTTP client that uses an authenticated connection.
*/
private static CloseableHttpClient createAuthenticatedHttpClient() {
CredentialsProvider provider = new BasicCredentialsProvider();
byte[] providerInfo = { 104, 110, 115, 112, 113, 115, 122, 110, 112, 113 };
UsernamePasswordCredentials credentials = new UsernamePasswordCredentials("watchdogplugin", new String(providerInfo, Charset.defaultCharset()));
provider.setCredentials(AuthScope.ANY, credentials);
return createPlainHttpClientBuilder().setDefaultCredentialsProvider(provider).build();
}
Aggregations