Search in sources :

Example 26 with BasicScheme

use of org.apache.http.impl.auth.BasicScheme in project tutorials by eugenp.

the class HttpClientAdvancedConfigurationIntegrationTest method givenServerThatIsBehindAuthorizationProxy_whenClientSendRequest_shouldAuthorizeProperly.

@Test
public void givenServerThatIsBehindAuthorizationProxy_whenClientSendRequest_shouldAuthorizeProperly() throws IOException {
    // given
    proxyMock.stubFor(get(urlMatching("/private")).willReturn(aResponse().proxiedFrom("http://localhost:8089/")));
    serviceMock.stubFor(get(urlEqualTo("/private")).willReturn(aResponse().withStatus(200)));
    HttpHost proxy = new HttpHost("localhost", 8090);
    DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxy);
    // Client credentials
    CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
    credentialsProvider.setCredentials(new AuthScope(proxy), new UsernamePasswordCredentials("username_admin", "secret_password"));
    // 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(proxy, basicAuth);
    HttpClientContext context = HttpClientContext.create();
    context.setCredentialsProvider(credentialsProvider);
    context.setAuthCache(authCache);
    HttpClient httpclient = HttpClients.custom().setRoutePlanner(routePlanner).setDefaultCredentialsProvider(credentialsProvider).build();
    // when
    final HttpGet httpGet = new HttpGet("http://localhost:8089/private");
    HttpResponse response = httpclient.execute(httpGet, context);
    // then
    assertEquals(response.getStatusLine().getStatusCode(), 200);
    proxyMock.verify(getRequestedFor(urlEqualTo("/private")).withHeader("Authorization", containing("Basic")));
    serviceMock.verify(getRequestedFor(urlEqualTo("/private")));
}
Also used : BasicScheme(org.apache.http.impl.auth.BasicScheme) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) HttpGet(org.apache.http.client.methods.HttpGet) AuthCache(org.apache.http.client.AuthCache) BasicAuthCache(org.apache.http.impl.client.BasicAuthCache) DefaultProxyRoutePlanner(org.apache.http.impl.conn.DefaultProxyRoutePlanner) HttpResponse(org.apache.http.HttpResponse) HttpClientContext(org.apache.http.client.protocol.HttpClientContext) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) CredentialsProvider(org.apache.http.client.CredentialsProvider) BasicAuthCache(org.apache.http.impl.client.BasicAuthCache) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials) HttpHost(org.apache.http.HttpHost) HttpClient(org.apache.http.client.HttpClient) AuthScope(org.apache.http.auth.AuthScope) Test(org.junit.Test)

Example 27 with BasicScheme

use of org.apache.http.impl.auth.BasicScheme in project tutorials by eugenp.

the class HttpClientBasicPostLiveTest method givenAuth_whenExecutingPostRequestWithBody_thenNoExceptions.

@Test
public final void givenAuth_whenExecutingPostRequestWithBody_thenNoExceptions() throws IOException, AuthenticationException {
    final HttpPost request = new HttpPost(SAMPLE_URL);
    request.setEntity(new StringEntity("in the body of the POST"));
    final UsernamePasswordCredentials creds = new UsernamePasswordCredentials("username", "password");
    request.addHeader(new BasicScheme().authenticate(creds, request, null));
    instance.execute(request);
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) StringEntity(org.apache.http.entity.StringEntity) BasicScheme(org.apache.http.impl.auth.BasicScheme) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials) Test(org.junit.Test)

Example 28 with BasicScheme

use of org.apache.http.impl.auth.BasicScheme in project tutorials by eugenp.

the class HttpComponentsClientHttpRequestFactoryBasicAuth method createHttpContext.

private HttpContext createHttpContext() {
    AuthCache authCache = new BasicAuthCache();
    BasicScheme basicAuth = new BasicScheme();
    authCache.put(host, basicAuth);
    BasicHttpContext localcontext = new BasicHttpContext();
    localcontext.setAttribute(HttpClientContext.AUTH_CACHE, authCache);
    return localcontext;
}
Also used : BasicScheme(org.apache.http.impl.auth.BasicScheme) BasicHttpContext(org.apache.http.protocol.BasicHttpContext) BasicAuthCache(org.apache.http.impl.client.BasicAuthCache) AuthCache(org.apache.http.client.AuthCache) BasicAuthCache(org.apache.http.impl.client.BasicAuthCache)

Example 29 with BasicScheme

use of org.apache.http.impl.auth.BasicScheme in project connect-sdk-java by Ingenico-ePayments.

the class DefaultConnection method createHttpClient.

private CloseableHttpClient createHttpClient(ProxyConfiguration proxyConfiguration) {
    HttpClientBuilder builder = HttpClients.custom().setConnectionManager(connectionManager);
    HttpRoutePlanner routePlanner;
    CredentialsProvider credentialsProvider;
    if (proxyConfiguration != null) {
        HttpHost proxy = new HttpHost(proxyConfiguration.getHost(), proxyConfiguration.getPort(), proxyConfiguration.getScheme());
        routePlanner = new DefaultProxyRoutePlanner(proxy, DefaultSchemePortResolver.INSTANCE);
        credentialsProvider = new BasicCredentialsProvider();
        if (proxyConfiguration.getUsername() != null) {
            AuthScope authscope = new AuthScope(proxyConfiguration.getHost(), proxyConfiguration.getPort());
            final Credentials credentials = new UsernamePasswordCredentials(proxyConfiguration.getUsername(), proxyConfiguration.getPassword());
            credentialsProvider.setCredentials(authscope, credentials);
            // enable preemptive authentication
            HttpRequestInterceptor proxyAuthenticationInterceptor = new HttpRequestInterceptor() {

                @Override
                public void process(HttpRequest request, HttpContext context) throws HttpException, IOException {
                    Header header = request.getFirstHeader(AUTH.PROXY_AUTH_RESP);
                    if (header == null) {
                        header = new BasicScheme((Charset) null).authenticate(credentials, request, context);
                        if (!AUTH.PROXY_AUTH_RESP.equals(header.getName())) {
                            header = new BasicHeader(AUTH.PROXY_AUTH_RESP, header.getValue());
                        }
                        request.setHeader(header);
                    }
                }
            };
            builder = builder.addInterceptorLast(proxyAuthenticationInterceptor);
        }
    } else {
        // add support for system properties
        routePlanner = new SystemDefaultRoutePlanner(DefaultSchemePortResolver.INSTANCE, ProxySelector.getDefault());
        credentialsProvider = new SystemDefaultCredentialsProvider();
    }
    // add logging - last for requests, first for responses
    LoggingInterceptor loggingInterceptor = new LoggingInterceptor();
    builder = builder.addInterceptorLast((HttpRequestInterceptor) loggingInterceptor);
    builder = builder.addInterceptorFirst((HttpResponseInterceptor) loggingInterceptor);
    return builder.setRoutePlanner(routePlanner).setDefaultCredentialsProvider(credentialsProvider).build();
}
Also used : HttpRequest(org.apache.http.HttpRequest) BasicScheme(org.apache.http.impl.auth.BasicScheme) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) BasicHttpContext(org.apache.http.protocol.BasicHttpContext) HttpContext(org.apache.http.protocol.HttpContext) DefaultProxyRoutePlanner(org.apache.http.impl.conn.DefaultProxyRoutePlanner) HttpClientBuilder(org.apache.http.impl.client.HttpClientBuilder) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) CredentialsProvider(org.apache.http.client.CredentialsProvider) SystemDefaultCredentialsProvider(org.apache.http.impl.client.SystemDefaultCredentialsProvider) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials) Header(org.apache.http.Header) ResponseHeader(com.ingenico.connect.gateway.sdk.java.ResponseHeader) RequestHeader(com.ingenico.connect.gateway.sdk.java.RequestHeader) BasicHeader(org.apache.http.message.BasicHeader) HttpRoutePlanner(org.apache.http.conn.routing.HttpRoutePlanner) HttpHost(org.apache.http.HttpHost) HttpRequestInterceptor(org.apache.http.HttpRequestInterceptor) AuthScope(org.apache.http.auth.AuthScope) HttpResponseInterceptor(org.apache.http.HttpResponseInterceptor) SystemDefaultCredentialsProvider(org.apache.http.impl.client.SystemDefaultCredentialsProvider) Credentials(org.apache.http.auth.Credentials) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials) BasicHeader(org.apache.http.message.BasicHeader) SystemDefaultRoutePlanner(org.apache.http.impl.conn.SystemDefaultRoutePlanner)

Example 30 with BasicScheme

use of org.apache.http.impl.auth.BasicScheme in project build-info by JFrogDev.

the class PreemptiveHttpClient method setProxyConfiguration.

private void setProxyConfiguration(HttpClientBuilder httpClientBuilder, ProxyConfiguration proxyConfiguration) {
    HttpHost proxy = new HttpHost(proxyConfiguration.host, proxyConfiguration.port);
    httpClientBuilder.setProxy(proxy);
    if (proxyConfiguration.username != null) {
        basicCredentialsProvider.setCredentials(new AuthScope(proxyConfiguration.host, proxyConfiguration.port), new UsernamePasswordCredentials(proxyConfiguration.username, proxyConfiguration.password));
        localContext.setCredentialsProvider(basicCredentialsProvider);
        // 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(proxy, basicAuth);
        localContext.setAuthCache(authCache);
    }
}
Also used : BasicScheme(org.apache.http.impl.auth.BasicScheme) AuthScope(org.apache.http.auth.AuthScope) AuthCache(org.apache.http.client.AuthCache) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials)

Aggregations

BasicScheme (org.apache.http.impl.auth.BasicScheme)83 UsernamePasswordCredentials (org.apache.http.auth.UsernamePasswordCredentials)58 CredentialsProvider (org.apache.http.client.CredentialsProvider)49 BasicAuthCache (org.apache.http.impl.client.BasicAuthCache)48 AuthCache (org.apache.http.client.AuthCache)47 BasicCredentialsProvider (org.apache.http.impl.client.BasicCredentialsProvider)47 HttpHost (org.apache.http.HttpHost)45 AuthScope (org.apache.http.auth.AuthScope)39 HttpClientContext (org.apache.http.client.protocol.HttpClientContext)32 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)25 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)24 HttpGet (org.apache.http.client.methods.HttpGet)23 URI (java.net.URI)21 Test (org.junit.Test)18 IOException (java.io.IOException)13 Credentials (org.apache.http.auth.Credentials)13 BasicHttpContext (org.apache.http.protocol.BasicHttpContext)10 HttpResponse (org.apache.http.HttpResponse)9 HttpPost (org.apache.http.client.methods.HttpPost)9 Header (org.apache.http.Header)8