use of org.graylog.shaded.elasticsearch7.org.apache.http.client.CredentialsProvider in project sling-org-apache-sling-testing-clients by apache.
the class FormBasedAuthInterceptor method doLogin.
private void doLogin(HttpRequest request, HttpContext context) throws IOException {
// get host
final HttpHost host = HttpClientContext.adapt(context).getTargetHost();
// get the username and password from the credentials provider
final CredentialsProvider credsProvider = HttpClientContext.adapt(context).getCredentialsProvider();
final AuthScope scope = new AuthScope(host.getHostName(), host.getPort());
final String username = credsProvider.getCredentials(scope).getUserPrincipal().getName();
final String password = credsProvider.getCredentials(scope).getPassword();
List<NameValuePair> parameters = new LinkedList<>();
parameters.add(new BasicNameValuePair("j_username", username));
parameters.add(new BasicNameValuePair("j_password", password));
HttpEntity httpEntity = new UrlEncodedFormEntity(parameters, "utf-8");
URI loginURI = URI.create(request.getRequestLine().getUri()).resolve(loginPath);
HttpPost loginPost = new HttpPost(loginURI);
loginPost.setEntity(httpEntity);
try (CloseableHttpClient client = HttpClientBuilder.create().setServiceUnavailableRetryStrategy(new ServerErrorRetryStrategy()).disableRedirectHandling().build()) {
try (CloseableHttpResponse response = client.execute(host, loginPost, context)) {
StatusLine sl = response.getStatusLine();
if (sl.getStatusCode() >= 400) {
LOG.error("Got error login response code {} from '{}'", sl.getStatusCode(), loginURI.toString());
LOG.error("Dumping headers: ");
for (Header header : response.getAllHeaders()) {
LOG.error("\t '{}' = '{}'", header.getName(), header.getValue());
}
try (InputStream inputStream = response.getEntity().getContent()) {
String responseText = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8)).lines().collect(Collectors.joining("\n"));
LOG.error("Error response body was : '{}'", responseText);
}
} else if (getLoginCookie(context, loginTokenName) == null) {
LOG.error("Login response {} from '{}' did not include cookie '{}'.", sl.getStatusCode(), loginURI.toString(), loginTokenName);
} else {
LOG.debug("Login response {} from '{}'", sl.getStatusCode(), loginURI.toString());
}
}
}
}
use of org.graylog.shaded.elasticsearch7.org.apache.http.client.CredentialsProvider in project keycloak by keycloak.
the class ProxyMappingsAwareRoutePlanner method determineProxy.
@Override
protected HttpHost determineProxy(HttpHost target, HttpRequest request, HttpContext context) throws HttpException {
String targetHostName = target.getHostName();
ProxyMapping proxyMapping = proxyMappings.getProxyFor(targetHostName);
LOG.debugf("Returning proxyMapping=%s for targetHost=%s", proxyMapping, targetHostName);
UsernamePasswordCredentials proxyCredentials = proxyMapping.getProxyCredentials();
HttpHost proxyHost = proxyMapping.getProxyHost();
if (proxyCredentials != null) {
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(new AuthScope(proxyHost.getHostName(), proxyHost.getPort()), proxyCredentials);
context.setAttribute(HttpClientContext.CREDS_PROVIDER, credsProvider);
}
return proxyHost;
}
use of org.graylog.shaded.elasticsearch7.org.apache.http.client.CredentialsProvider in project crawler4j by yasserg.
the class BasicAuthHttpRequestInterceptor method process.
@Override
public void process(HttpRequest request, HttpContext context) {
AuthState authState = (AuthState) context.getAttribute(HttpClientContext.TARGET_AUTH_STATE);
if (authState.getAuthScheme() == null) {
CredentialsProvider credsProvider = (CredentialsProvider) context.getAttribute(HttpClientContext.CREDS_PROVIDER);
HttpHost targetHost = (HttpHost) context.getAttribute(HttpCoreContext.HTTP_TARGET_HOST);
Credentials credentials = credsProvider.getCredentials(new AuthScope(targetHost.getHostName(), targetHost.getPort()));
if (credentials != null) {
authState.update(new BasicScheme(), credentials);
}
}
}
use of org.graylog.shaded.elasticsearch7.org.apache.http.client.CredentialsProvider in project NoraUi by NoraUi.
the class MailSteps method createRestTemplate.
private static RestTemplate createRestTemplate() {
String proxyUser = "";
String proxyPassword = "";
String proxyUrl = "";
int proxyPort = 0;
HttpClientBuilder clientBuilder = HttpClientBuilder.create();
if (!"".equals(proxyUrl)) {
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(new AuthScope(proxyUrl, proxyPort), new UsernamePasswordCredentials(proxyUser, proxyPassword));
HttpHost myProxy = new HttpHost(proxyUrl, proxyPort);
clientBuilder.setProxy(myProxy).setDefaultCredentialsProvider(credsProvider).disableCookieManagement();
}
HttpClient httpClient = clientBuilder.build();
HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory();
factory.setHttpClient(httpClient);
RestTemplate restTemplate = new RestTemplate(factory);
restTemplate.getMessageConverters().add(new GsonHttpMessageConverter());
return restTemplate;
}
use of org.graylog.shaded.elasticsearch7.org.apache.http.client.CredentialsProvider in project ANNIS by korpling.
the class Helper method createRESTClient.
/**
* Creates an authentificiated REST client
*
* @param userName
* @param password
* @return A newly created client.
*/
public static Client createRESTClient(String userName, String password) {
DefaultApacheHttpClient4Config rc = new DefaultApacheHttpClient4Config();
rc.getClasses().add(SaltProjectProvider.class);
ThreadSafeClientConnManager clientConnMgr = new ThreadSafeClientConnManager();
clientConnMgr.setDefaultMaxPerRoute(10);
rc.getProperties().put(ApacheHttpClient4Config.PROPERTY_CONNECTION_MANAGER, clientConnMgr);
if (userName != null && password != null) {
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(userName, password));
rc.getProperties().put(ApacheHttpClient4Config.PROPERTY_CREDENTIALS_PROVIDER, credentialsProvider);
rc.getProperties().put(ApacheHttpClient4Config.PROPERTY_PREEMPTIVE_BASIC_AUTHENTICATION, true);
}
Client c = ApacheHttpClient4.create(rc);
return c;
}
Aggregations