Example usage for org.apache.http.impl.client BasicCredentialsProvider BasicCredentialsProvider

List of usage examples for org.apache.http.impl.client BasicCredentialsProvider BasicCredentialsProvider

Introduction

In this page you can find the example usage for org.apache.http.impl.client BasicCredentialsProvider BasicCredentialsProvider.

Prototype

public BasicCredentialsProvider() 

Source Link

Document

Default constructor.

Usage

From source file:org.wildfly.test.integration.elytron.http.PasswordMechTestBase.java

@Test
public void testInvalidPrincipal() throws Exception {
    HttpGet request = new HttpGet(new URI(url.toExternalForm() + "role1"));
    UsernamePasswordCredentials credentials = new UsernamePasswordCredentials("user1wrong", "password1");

    CredentialsProvider credsProvider = new BasicCredentialsProvider();
    credsProvider.setCredentials(AuthScope.ANY, credentials);

    try (CloseableHttpClient httpClient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider)
            .build()) {/*ww  w. j  a va  2  s . com*/
        try (CloseableHttpResponse response = httpClient.execute(request)) {
            int statusCode = response.getStatusLine().getStatusCode();
            assertEquals("Unexpected status code in HTTP response.", SC_UNAUTHORIZED, statusCode);
        }
    }
}

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);/*w w w. j  a  v a 2s .c  o 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.modeshape.test.kit.JBossASKitTest.java

@Test
public void webApplicationsShouldBeAccessible() throws Exception {
    DefaultHttpClient httpClient = new DefaultHttpClient();

    httpClient.setCredentialsProvider(new BasicCredentialsProvider() {
        @Override// w ww  .j a v  a 2s .c  o  m
        public Credentials getCredentials(AuthScope authscope) {
            //defined via modeshape-user and modeshape-roles
            return new UsernamePasswordCredentials("admin", "admin");
        }
    });
    assertURIisAccessible("http://localhost:8080/modeshape-webdav", httpClient);
    assertURIisAccessible("http://localhost:8080/modeshape-rest", httpClient);
    assertURIisAccessible("http://localhost:8080/modeshape-cmis", httpClient);
}

From source file:com.logsniffer.settings.http.HttpSettings.java

/**
 * Creates a {@link HttpClientBuilder} considering current settings.
 * /* w w w.  j a  va 2 s.com*/
 * @return a {@link HttpClientBuilder} considering current settings.
 */
public HttpClientBuilder createHttpClientBuilder() {
    HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
    if (getHttpProxy() != null && StringUtils.isNotBlank(getHttpProxy().getHost())) {
        httpClientBuilder = httpClientBuilder.setProxy(
                new HttpHost(getHttpProxy().getHost(), getHttpProxy().getPort(), getHttpProxy().getSchema()));
        if (StringUtils.isNotBlank(getHttpProxy().getUser())) {
            Credentials credentials = new UsernamePasswordCredentials(getHttpProxy().getUser(),
                    getHttpProxy().getPassword());
            AuthScope authScope = new AuthScope(getHttpProxy().getHost(), getHttpProxy().getPort());
            CredentialsProvider credsProvider = new BasicCredentialsProvider();
            credsProvider.setCredentials(authScope, credentials);
            httpClientBuilder = httpClientBuilder.setDefaultCredentialsProvider(credsProvider);
            httpClientBuilder.useSystemProperties();
        }
    }
    return httpClientBuilder;
}

From source file:org.modeshape.test.kit.JBossASKitIT.java

@Test
public void webApplicationsShouldBeAccessible() throws Exception {
    DefaultHttpClient httpClient = new DefaultHttpClient();

    httpClient.setCredentialsProvider(new BasicCredentialsProvider() {
        @Override/*www . j ava  2s.c  om*/
        public Credentials getCredentials(AuthScope authscope) {
            //defined via modeshape-user and modeshape-roles
            return new UsernamePasswordCredentials("admin", "admin");
        }
    });
    assertURIisAccessible("http://localhost:8080/modeshape-webdav", httpClient);
    assertURIisAccessible("http://localhost:8080/modeshape-rest", httpClient);
    assertURIisAccessible("http://localhost:8080/modeshape-cmis", httpClient);
    assertURIisAccessible("http://localhost:8080/modeshape-explorer", httpClient);
}

From source file:io.openvidu.java.client.OpenVidu.java

/**
 * @param urlOpenViduServer Public accessible IP where your instance of OpenVidu
 *                          Server is up an running
 * @param secret            Secret used on OpenVidu Server initialization
 *//*from w ww  .j av a  2s . c  o m*/
public OpenVidu(String urlOpenViduServer, String secret) {

    OpenVidu.urlOpenViduServer = urlOpenViduServer;

    if (!OpenVidu.urlOpenViduServer.endsWith("/")) {
        OpenVidu.urlOpenViduServer += "/";
    }

    this.secret = secret;

    TrustStrategy trustStrategy = new TrustStrategy() {
        @Override
        public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
            return true;
        }
    };

    CredentialsProvider provider = new BasicCredentialsProvider();
    UsernamePasswordCredentials credentials = new UsernamePasswordCredentials("OPENVIDUAPP", this.secret);
    provider.setCredentials(AuthScope.ANY, credentials);

    SSLContext sslContext;

    try {
        sslContext = new SSLContextBuilder().loadTrustMaterial(null, trustStrategy).build();
    } catch (KeyManagementException | NoSuchAlgorithmException | KeyStoreException e) {
        throw new RuntimeException(e);
    }

    RequestConfig.Builder requestBuilder = RequestConfig.custom();
    requestBuilder = requestBuilder.setConnectTimeout(30000);
    requestBuilder = requestBuilder.setConnectionRequestTimeout(30000);

    OpenVidu.httpClient = HttpClientBuilder.create().setDefaultRequestConfig(requestBuilder.build())
            .setConnectionTimeToLive(30, TimeUnit.SECONDS).setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE)
            .setSSLContext(sslContext).setDefaultCredentialsProvider(provider).build();
}

From source file:org.muhia.app.psi.integ.config.ke.crba.CreditReferenceBureauAuthorityClientConfiguration.java

@Bean(name = "transunionHttpClient")
public CloseableHttpClient httpClient() {

    RequestConfig config = RequestConfig.custom()
            .setConnectTimeout(properties.getCrbaTransportConnectionTimeout())
            .setConnectionRequestTimeout(properties.getCrbaTransportConnectionRequestTimeout())
            .setSocketTimeout(properties.getCrbaTransportReadTimeout()).build();
    CredentialsProvider provider = new BasicCredentialsProvider();
    UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(
            properties.getCrbaTransunionTransportUsername(), properties.getCrbaTransunionTransportPassword());
    provider.setCredentials(AuthScope.ANY, credentials);

    PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager();
    connManager.setMaxTotal(properties.getCrbaPoolMaxHost());
    connManager.setDefaultMaxPerRoute(properties.getCrbaPoolDefaultmaxPerhost());
    connManager.setValidateAfterInactivity(properties.getCrbaPoolValidateAfterInactivity());

    return HttpClientBuilder.create().setDefaultRequestConfig(config).setDefaultCredentialsProvider(provider)
            .setConnectionManager(connManager).evictExpiredConnections()
            .addInterceptorFirst(new RemoveHttpHeadersInterceptor()).build();

}

From source file:org.valkyriercp.security.remoting.BasicAuthCommonsHttpInvokerProxyFactoryBean.java

/**
 * Handle a change in the current authentication token.
 * This method will fail fast if the executor isn't a CommonsHttpInvokerRequestExecutor.
 * @see org.valkyriercp.security.AuthenticationAware#setAuthenticationToken(org.springframework.security.core.Authentication)
 *//*ww w  .j av a 2s .c o m*/
public void setAuthenticationToken(Authentication authentication) {
    if (logger.isDebugEnabled()) {
        logger.debug("New authentication token: " + authentication);
    }

    HttpComponentsHttpInvokerRequestExecutor executor = (HttpComponentsHttpInvokerRequestExecutor) getHttpInvokerRequestExecutor();
    DefaultHttpClient httpClient = (DefaultHttpClient) executor.getHttpClient();
    BasicCredentialsProvider provider = new BasicCredentialsProvider();
    httpClient.setCredentialsProvider(provider);
    httpClient.addRequestInterceptor(new PreemptiveAuthInterceptor());
    UsernamePasswordCredentials usernamePasswordCredentials;
    if (authentication != null) {
        usernamePasswordCredentials = new UsernamePasswordCredentials(authentication.getName(),
                authentication.getCredentials().toString());
    } else {
        usernamePasswordCredentials = null;
    }
    provider.setCredentials(AuthScope.ANY, usernamePasswordCredentials);
}

From source file:io.uploader.drive.config.proxy.Proxy.java

@Override
public CredentialsProvider getCredentialsProvider() {
    if (isActive()) {
        CredentialsProvider credsProvider = new BasicCredentialsProvider();
        credsProvider.setCredentials(new AuthScope(getHost(), getPort()),
                new UsernamePasswordCredentials(getUsername(), getPassword()));
        return credsProvider;
    } else {/*ww w. j  a v a  2 s .  c  o  m*/
        return null;
    }
}