Example usage for org.apache.http.auth AuthScope AuthScope

List of usage examples for org.apache.http.auth AuthScope AuthScope

Introduction

In this page you can find the example usage for org.apache.http.auth AuthScope AuthScope.

Prototype

public AuthScope(final AuthScope authscope) 

Source Link

Document

Creates a copy of the given credentials scope.

Usage

From source file:ua.pp.msk.gradle.http.Client.java

private void init(URL targetURL, String user, String password) throws ClientSslException {
    this.targetUrl = targetURL;
    logger.debug("Initializing " + this.getClass().getName() + " with target URL " + targetURL.toString());
    HttpHost htHost = new HttpHost(targetUrl.getHost(), targetUrl.getPort(), targetUrl.getProtocol());

    AuthCache aCache = new BasicAuthCache();
    BasicScheme basicAuth = new BasicScheme();
    aCache.put(htHost, basicAuth);/*  www. j  a  va2s.co  m*/

    UsernamePasswordCredentials creds = new UsernamePasswordCredentials(user, password);
    BasicCredentialsProvider cProvider = new BasicCredentialsProvider();
    cProvider.setCredentials(new AuthScope(htHost), creds);
    logger.debug("Credential provider: " + cProvider.toString());

    context = new BasicHttpContext();
    ClientContextConfigurer cliCon = new ClientContextConfigurer(context);
    cliCon.setCredentialsProvider(cProvider);
    context.setAttribute(ClientContext.AUTH_CACHE, aCache);
    SSLSocketFactory sslConnectionSocketFactory = null;
    try {
        sslConnectionSocketFactory = new SSLSocketFactory(new TrustSelfSignedStrategy(),
                new NexusHostnameVerifier());
    } catch (KeyManagementException ex) {
        logger.error("Cannot manage secure keys", ex);
        throw new ClientSslException("Cannot manage secure keys", ex);
    } catch (KeyStoreException ex) {
        logger.error("Cannot build SSL context due to KeyStore error", ex);
        throw new ClientSslException("Cannot build SSL context due to KeyStore error", ex);
    } catch (NoSuchAlgorithmException ex) {
        logger.error("Unsupported security algorithm", ex);
        throw new ClientSslException("Unsupported security algorithm", ex);
    } catch (UnrecoverableKeyException ex) {
        logger.error("Unrecoverable key", ex);
        throw new ClientSslException("Unrecoverrable key", ex);
    }

    DefaultHttpClient defClient = new DefaultHttpClient();
    defClient.setRedirectStrategy(new NexusRedirectStrategy());
    defClient.setCredentialsProvider(cProvider);
    Scheme https = new Scheme("https", 443, sslConnectionSocketFactory);
    defClient.getConnectionManager().getSchemeRegistry().register(https);
    defClient.setTargetAuthenticationStrategy(new TargetAuthenticationStrategy());
    client = defClient;
}

From source file:org.apache.activemq.artemis.rest.queue.push.UriStrategy.java

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);
        }//from  w  w w.  ja  v  a 2  s.c  o m
    }
}

From source file:org.modeshape.web.jcr.rest.AbstractRestTest.java

protected void setAuthCredentials(String authUsername, String authPassword) {
    BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
    credentialsProvider.setCredentials(new AuthScope(getHost()),
            new UsernamePasswordCredentials(authUsername, authPassword));
    httpContext.setCredentialsProvider(credentialsProvider);
}

From source file:com.garyclayburg.scimclient.authn.AuthHttpComponentsClientHttpRequestFactory.java

@Override
protected HttpContext createHttpContext(HttpMethod httpMethod, URI uri) {
    // 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(host, basicAuth);/*from  w w  w  .  jav a  2 s .co m*/

    // Add AuthCache to the execution context
    HttpClientContext localcontext = HttpClientContext.create();
    localcontext.setAuthCache(authCache);

    if (userName != null) {
        BasicCredentialsProvider credsProvider = new BasicCredentialsProvider();
        credsProvider.setCredentials(new AuthScope(host), new UsernamePasswordCredentials(userName, password));
        localcontext.setCredentialsProvider(credsProvider);
    }
    return localcontext;
}

From source file:org.jenkinsci.plugins.relution_publisher.net.RequestManager.java

private CloseableHttpAsyncClient createHttpClient() {

    final RequestConfig.Builder requestConfigBuilder = RequestConfig.custom();
    requestConfigBuilder.setConnectionRequestTimeout(TIMEOUT_CONNECTION_REQUEST);
    requestConfigBuilder.setConnectTimeout(TIMEOUT_CONNECT);
    requestConfigBuilder.setSocketTimeout(TIMEOUT_SOCKET);

    if (this.mProxyHost != null) {
        requestConfigBuilder.setProxy(this.mProxyHost);
    }//  w  w w.  j a va2 s. c  o  m

    final HttpAsyncClientBuilder clientBuilder = HttpAsyncClients.custom();

    final RequestConfig requestConfig = requestConfigBuilder.build();
    clientBuilder.setDefaultRequestConfig(requestConfig);

    if (this.mProxyHost != null && this.mCredentials != null) {
        final AuthScope authScope = new AuthScope(this.mProxyHost);
        final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
        credentialsProvider.setCredentials(authScope, this.mCredentials);
        clientBuilder.setDefaultCredentialsProvider(credentialsProvider);
    }

    return clientBuilder.build();
}

From source file:eu.itesla_project.histodb.client.impl.HistoDbHttpClientImpl.java

private synchronized CloseableHttpClient getHttpclient(HistoDbConfig config) {
    if (httpClient == null) {
        try {// w  ww  .jav a  2 s .c o m
            ConnectionSocketFactory plainsf = PlainConnectionSocketFactory.getSocketFactory();
            TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {

                @Override
                public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                    return null;
                }

                @Override
                public void checkClientTrusted(X509Certificate[] certs, String authType) {
                }

                @Override
                public void checkServerTrusted(X509Certificate[] certs, String authType) {
                }
            } };
            SSLContext sslContext = SSLContext.getInstance("SSL");
            sslContext.init(null, trustAllCerts, new java.security.SecureRandom());
            LayeredConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext);
            Registry<ConnectionSocketFactory> r = RegistryBuilder.<ConnectionSocketFactory>create()
                    .register("http", plainsf).register("https", sslsf).build();
            PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(r);
            cm.setDefaultMaxPerRoute(10);
            cm.setMaxTotal(20);
            HttpClientBuilder httpClientBuilder = HttpClients.custom().setConnectionManager(cm);
            CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
            credentialsProvider.setCredentials(
                    new AuthScope(new HttpHost(config.getConnectionParameters().getHost(),
                            config.getConnectionParameters().getPort())),
                    new UsernamePasswordCredentials(config.getConnectionParameters().getUserName(),
                            config.getConnectionParameters().getPassword()));
            if (config.getProxyParameters() != null) {
                HttpHost proxy = new HttpHost(config.getProxyParameters().getHost(),
                        config.getProxyParameters().getPort());
                credentialsProvider.setCredentials(new AuthScope(proxy), new UsernamePasswordCredentials(
                        config.getProxyParameters().getUserName(), config.getProxyParameters().getPassword()));
                httpClientBuilder.setProxy(proxy);
            }
            httpClient = httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider).build();
        } catch (KeyManagementException | NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        }
    }
    return httpClient;
}

From source file:com.helger.httpclient.HttpClientHelper.java

@Nonnull
public static HttpContext createHttpContext(@Nullable final HttpHost aProxy,
        @Nullable final Credentials aProxyCredentials) {
    final HttpClientContext ret = HttpClientContext.create();
    if (aProxy != null) {
        ret.setRequestConfig(RequestConfig.custom().setProxy(aProxy).build());
        if (aProxyCredentials != null) {
            final CredentialsProvider aCredentialsProvider = new BasicCredentialsProvider();
            aCredentialsProvider.setCredentials(new AuthScope(aProxy), aProxyCredentials);
            ret.setCredentialsProvider(aCredentialsProvider);
        }/*from   w w  w.  j  av  a 2  s. com*/
    }
    return ret;
}

From source file:org.sonatype.nexus.plugins.crowd.client.rest.RestClient.java

RestClient(CrowdPluginConfiguration config) throws URISyntaxException {
    crowdServer = new URI(config.getCrowdServerUrl()).resolve("rest/usermanagement/1/");

    crowdCreds = new UsernamePasswordCredentials(config.getApplicationName(), config.getApplicationPassword());

    // configure the http client
    RequestConfig.Builder reqConfigBuilder = RequestConfig.custom().setAuthenticationEnabled(true)
            .setConnectTimeout(config.getHttpTimeout()).setSocketTimeout(config.getHttpTimeout());

    cm = new PoolingHttpClientConnectionManager();
    cm.setMaxTotal(config.getHttpMaxConnections());
    cm.setDefaultMaxPerRoute(config.getHttpMaxConnections());

    // proxy settings
    CredentialsProvider credsProvider = new BasicCredentialsProvider();
    if (StringUtils.isNotBlank(config.getHttpProxyHost()) && config.getHttpProxyPort() > 0) {
        HttpHost proxy = new HttpHost(config.getHttpProxyHost(), config.getHttpProxyPort());
        reqConfigBuilder.setProxy(proxy);

        if (config.getHttpProxyUsername() != null && config.getHttpProxyPassword() != null) {
            credsProvider.setCredentials(new AuthScope(proxy), new UsernamePasswordCredentials(
                    config.getHttpProxyUsername(), config.getHttpProxyPassword()));
        }/*  w w w . j  a  v  a 2 s.c  om*/
    }

    RequestConfig reqConfig = reqConfigBuilder.build();
    HttpClientBuilder hcBuilder = HttpClients.custom().setMaxConnPerRoute(config.getHttpMaxConnections())
            .setMaxConnTotal(config.getHttpMaxConnections()).setConnectionManager(cm)
            .setDefaultCredentialsProvider(credsProvider).setDefaultRequestConfig(reqConfig);

    // handling of compressed responses
    hcBuilder.addInterceptorLast(new CompressedHttpResponseInterceptor());

    client = hcBuilder.build();

    if (LOG.isDebugEnabled()) {
        LOG.debug("HTTP Client config");
        LOG.debug(config.getCrowdServerUrl());
        LOG.debug("PROPERTY_THREADPOOL_SIZE:" + cm.getMaxTotal());
        LOG.debug("PROPERTY_READ_TIMEOUT:" + reqConfig.getSocketTimeout());
        LOG.debug("PROPERTY_CONNECT_TIMEOUT:" + reqConfig.getConnectTimeout());
        if (reqConfig.getProxy() != null) {
            LOG.debug("PROPERTY_PROXY_URI:" + reqConfig.getProxy().toString());
        }
        LOG.debug("Crowd application name:" + config.getApplicationName());
    }
}

From source file:ua.pp.msk.cliqr.GetProcessorImpl.java

private void init(URL targetURL, String user, String password) throws ClientSslException {
    this.targetUrl = targetURL;
    logger.debug("Initializing " + this.getClass().getName() + " with target URL " + targetURL.toString());
    HttpHost htHost = new HttpHost(targetUrl.getHost(), targetUrl.getPort(), targetUrl.getProtocol());

    AuthCache aCache = new BasicAuthCache();
    BasicScheme basicAuth = new BasicScheme();
    aCache.put(htHost, basicAuth);//w  ww  . j  a v a  2s .  c om

    UsernamePasswordCredentials creds = new UsernamePasswordCredentials(user, password);
    BasicCredentialsProvider cProvider = new BasicCredentialsProvider();
    cProvider.setCredentials(new AuthScope(htHost), creds);
    logger.debug("Credential provider: " + cProvider.toString());

    context = new BasicHttpContext();
    ClientContextConfigurer cliCon = new ClientContextConfigurer(context);
    cliCon.setCredentialsProvider(cProvider);
    context.setAttribute(ClientContext.AUTH_CACHE, aCache);
    SSLSocketFactory sslConnectionSocketFactory = null;
    try {
        sslConnectionSocketFactory = new SSLSocketFactory(new TrustSelfSignedStrategy(),
                new CliQrHostnameVerifier());
    } catch (KeyManagementException ex) {
        logger.error("Cannot manage secure keys", ex);
        throw new ClientSslException("Cannot manage secure keys", ex);
    } catch (KeyStoreException ex) {
        logger.error("Cannot build SSL context due to KeyStore error", ex);
        throw new ClientSslException("Cannot build SSL context due to KeyStore error", ex);
    } catch (NoSuchAlgorithmException ex) {
        logger.error("Unsupported security algorithm", ex);
        throw new ClientSslException("Unsupported security algorithm", ex);
    } catch (UnrecoverableKeyException ex) {
        logger.error("Unrecoverable key", ex);
        throw new ClientSslException("Unrecoverrable key", ex);
    }

    DefaultHttpClient defClient = new DefaultHttpClient();
    defClient.setRedirectStrategy(new CliQrRedirectStrategy());
    defClient.setCredentialsProvider(cProvider);
    Scheme https = new Scheme("https", 443, sslConnectionSocketFactory);
    defClient.getConnectionManager().getSchemeRegistry().register(https);
    defClient.setTargetAuthenticationStrategy(new TargetAuthenticationStrategy());
    client = defClient;
}

From source file:ua.pp.msk.cliqr.PostProcessorImpl.java

private void init(URL url, String user, String password) throws ClientSslException {
    this.targetUrl = url;
    HttpHost htHost = new HttpHost(targetUrl.getHost(), targetUrl.getPort(), targetUrl.getProtocol());

    BasicAuthCache aCache = new BasicAuthCache();
    BasicScheme basicAuth = new BasicScheme(ChallengeState.TARGET);
    aCache.put(htHost, basicAuth);/*from w  w w  .ja  v a2s .  c  o m*/

    UsernamePasswordCredentials creds = new UsernamePasswordCredentials(user, password);
    BasicCredentialsProvider cProvider = new BasicCredentialsProvider();
    cProvider.setCredentials(new AuthScope(htHost), creds);
    logger.debug("Credential provider: " + cProvider.toString());

    context = new BasicHttpContext();
    ClientContextConfigurer cliCon = new ClientContextConfigurer(context);
    context.setAttribute(ClientContext.AUTH_CACHE, aCache);
    //context.setAuthCache(aCache);
    cliCon.setCredentialsProvider(cProvider);
    //context.setCredentialsProvider(cProvider);
    SSLSocketFactory sslSocketFactory = null;
    try {
        //SSLContext trustySslContext = SSLContextBuilder.create().loadTrustMaterial( new TrustSelfSignedStrategy()).build();
        //sslConnectionSocketFactory = new SSLConnectionSocketFactory(trustySslContext, new CliQrHostnameVerifier());
        sslSocketFactory = new SSLSocketFactory(new TrustSelfSignedStrategy(), new CliQrHostnameVerifier());
    } catch (KeyManagementException ex) {
        logger.error("Cannot manage secure keys", ex);
        throw new ClientSslException("Cannot manage secure keys", ex);
    } catch (KeyStoreException ex) {
        logger.error("Cannot build SSL context due to KeyStore error", ex);
        throw new ClientSslException("Cannot build SSL context due to KeyStore error", ex);
    } catch (NoSuchAlgorithmException ex) {
        logger.error("Unsupported security algorithm", ex);
        throw new ClientSslException("Unsupported security algorithm", ex);
    } catch (UnrecoverableKeyException ex) {
        logger.error("Unrecoverable key", ex);
        throw new ClientSslException("Unrecoverrable key", ex);
    }

    DefaultHttpClient defClient = new DefaultHttpClient();
    defClient.setRedirectStrategy(new CliQrRedirectStrategy());
    defClient.setCredentialsProvider(cProvider);
    Scheme https = new Scheme("https", 443, sslSocketFactory);
    defClient.getConnectionManager().getSchemeRegistry().register(https);
    defClient.setTargetAuthenticationStrategy(new TargetAuthenticationStrategy());
    client = defClient;
}