Search in sources :

Example 26 with CredentialsProvider

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());
            }
        }
    }
}
Also used : BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NameValuePair(org.apache.http.NameValuePair) HttpPost(org.apache.http.client.methods.HttpPost) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpEntity(org.apache.http.HttpEntity) ServerErrorRetryStrategy(org.apache.sling.testing.clients.util.ServerErrorRetryStrategy) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) CredentialsProvider(org.apache.http.client.CredentialsProvider) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity) URI(java.net.URI) LinkedList(java.util.LinkedList) StatusLine(org.apache.http.StatusLine) Header(org.apache.http.Header) HttpHost(org.apache.http.HttpHost) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) AuthScope(org.apache.http.auth.AuthScope) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) BufferedReader(java.io.BufferedReader)

Example 27 with CredentialsProvider

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;
}
Also used : BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) HttpHost(org.apache.http.HttpHost) AuthScope(org.apache.http.auth.AuthScope) ProxyMapping(org.keycloak.connections.httpclient.ProxyMappings.ProxyMapping) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) CredentialsProvider(org.apache.http.client.CredentialsProvider) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials)

Example 28 with CredentialsProvider

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);
        }
    }
}
Also used : BasicScheme(org.apache.http.impl.auth.BasicScheme) AuthState(org.apache.http.auth.AuthState) HttpHost(org.apache.http.HttpHost) AuthScope(org.apache.http.auth.AuthScope) CredentialsProvider(org.apache.http.client.CredentialsProvider) Credentials(org.apache.http.auth.Credentials)

Example 29 with CredentialsProvider

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;
}
Also used : BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) HttpHost(org.apache.http.HttpHost) HttpClient(org.apache.http.client.HttpClient) AuthScope(org.apache.http.auth.AuthScope) RestTemplate(org.springframework.web.client.RestTemplate) GsonHttpMessageConverter(org.springframework.http.converter.json.GsonHttpMessageConverter) HttpClientBuilder(org.apache.http.impl.client.HttpClientBuilder) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) CredentialsProvider(org.apache.http.client.CredentialsProvider) HttpComponentsClientHttpRequestFactory(org.springframework.http.client.HttpComponentsClientHttpRequestFactory) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials)

Example 30 with CredentialsProvider

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;
}
Also used : DefaultApacheHttpClient4Config(com.sun.jersey.client.apache4.config.DefaultApacheHttpClient4Config) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) ThreadSafeClientConnManager(org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) CredentialsProvider(org.apache.http.client.CredentialsProvider) Client(com.sun.jersey.api.client.Client) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials)

Aggregations

CredentialsProvider (org.apache.http.client.CredentialsProvider)273 BasicCredentialsProvider (org.apache.http.impl.client.BasicCredentialsProvider)225 UsernamePasswordCredentials (org.apache.http.auth.UsernamePasswordCredentials)203 AuthScope (org.apache.http.auth.AuthScope)138 HttpHost (org.apache.http.HttpHost)104 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)75 HttpGet (org.apache.http.client.methods.HttpGet)62 BasicScheme (org.apache.http.impl.auth.BasicScheme)49 HttpClientBuilder (org.apache.http.impl.client.HttpClientBuilder)48 HttpResponse (org.apache.http.HttpResponse)47 Test (org.junit.Test)44 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)41 HttpClientContext (org.apache.http.client.protocol.HttpClientContext)40 IOException (java.io.IOException)39 URI (java.net.URI)36 Credentials (org.apache.http.auth.Credentials)36 AuthCache (org.apache.http.client.AuthCache)33 BasicAuthCache (org.apache.http.impl.client.BasicAuthCache)33 HttpClient (org.apache.http.client.HttpClient)31 RequestConfig (org.apache.http.client.config.RequestConfig)29