Search in sources :

Example 71 with CredentialsProvider

use of org.apache.http.client.CredentialsProvider in project lucene-solr by apache.

the class HttpClientUtil method setupBuilder.

private static HttpClientBuilder setupBuilder(HttpClientBuilder builder, SolrParams config) {
    Builder requestConfigBuilder = RequestConfig.custom().setRedirectsEnabled(config.getBool(HttpClientUtil.PROP_FOLLOW_REDIRECTS, false)).setDecompressionEnabled(false).setConnectTimeout(config.getInt(HttpClientUtil.PROP_CONNECTION_TIMEOUT, DEFAULT_CONNECT_TIMEOUT)).setSocketTimeout(config.getInt(HttpClientUtil.PROP_SO_TIMEOUT, DEFAULT_SO_TIMEOUT));
    String cpolicy = cookiePolicy;
    if (cpolicy != null) {
        requestConfigBuilder.setCookieSpec(cpolicy);
    }
    RequestConfig requestConfig = requestConfigBuilder.build();
    HttpClientBuilder retBuilder = builder.setDefaultRequestConfig(requestConfig);
    if (config.getBool(HttpClientUtil.PROP_USE_RETRY, true)) {
        retBuilder = retBuilder.setRetryHandler(new SolrHttpRequestRetryHandler(3));
    } else {
        retBuilder = retBuilder.setRetryHandler(NO_RETRY);
    }
    final String basicAuthUser = config.get(HttpClientUtil.PROP_BASIC_AUTH_USER);
    final String basicAuthPass = config.get(HttpClientUtil.PROP_BASIC_AUTH_PASS);
    if (basicAuthUser != null && basicAuthPass != null) {
        CredentialsProvider credsProvider = new BasicCredentialsProvider();
        credsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(basicAuthUser, basicAuthPass));
        retBuilder.setDefaultCredentialsProvider(credsProvider);
    }
    if (config.getBool(HttpClientUtil.PROP_ALLOW_COMPRESSION, false)) {
        retBuilder.addInterceptorFirst(new UseCompressionRequestInterceptor());
        retBuilder.addInterceptorFirst(new UseCompressionResponseInterceptor());
    } else {
        retBuilder.disableContentCompression();
    }
    return retBuilder;
}
Also used : RequestConfig(org.apache.http.client.config.RequestConfig) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) RegistryBuilder(org.apache.http.config.RegistryBuilder) Builder(org.apache.http.client.config.RequestConfig.Builder) HttpClientBuilder(org.apache.http.impl.client.HttpClientBuilder) HttpClientBuilder(org.apache.http.impl.client.HttpClientBuilder) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) CredentialsProvider(org.apache.http.client.CredentialsProvider) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials)

Example 72 with CredentialsProvider

use of org.apache.http.client.CredentialsProvider in project jmeter by apache.

the class HTTPHC4Impl method setConnectionAuthorization.

/**
     * Setup credentials for url AuthScope but keeps Proxy AuthScope credentials
     * @param client HttpClient
     * @param url URL
     * @param authManager {@link AuthManager}
     * @param key key
     */
private void setConnectionAuthorization(CloseableHttpClient client, URL url, AuthManager authManager, HttpClientKey key) {
    CredentialsProvider credentialsProvider = ((AbstractHttpClient) client).getCredentialsProvider();
    if (authManager != null) {
        if (authManager.hasAuthForURL(url)) {
            authManager.setupCredentials(client, url, credentialsProvider, LOCALHOST);
        } else {
            credentialsProvider.clear();
        }
    } else {
        Credentials credentials = null;
        AuthScope authScope = null;
        if (key.hasProxy && !StringUtils.isEmpty(key.proxyUser)) {
            authScope = new AuthScope(key.proxyHost, key.proxyPort);
            credentials = credentialsProvider.getCredentials(authScope);
        }
        credentialsProvider.clear();
        if (credentials != null) {
            credentialsProvider.setCredentials(authScope, credentials);
        }
    }
}
Also used : AbstractHttpClient(org.apache.http.impl.client.AbstractHttpClient) AuthScope(org.apache.http.auth.AuthScope) CredentialsProvider(org.apache.http.client.CredentialsProvider) Credentials(org.apache.http.auth.Credentials) NTCredentials(org.apache.http.auth.NTCredentials)

Example 73 with CredentialsProvider

use of org.apache.http.client.CredentialsProvider in project lucene-solr by apache.

the class Krb5HttpClientBuilder method getBuilder.

public SolrHttpClientBuilder getBuilder(SolrHttpClientBuilder builder) {
    if (System.getProperty(LOGIN_CONFIG_PROP) != null) {
        String configValue = System.getProperty(LOGIN_CONFIG_PROP);
        if (configValue != null) {
            logger.info("Setting up SPNego auth with config: " + configValue);
            final String useSubjectCredsProp = "javax.security.auth.useSubjectCredsOnly";
            String useSubjectCredsVal = System.getProperty(useSubjectCredsProp);
            // authentication mechanism can load the credentials from the JAAS configuration.
            if (useSubjectCredsVal == null) {
                System.setProperty(useSubjectCredsProp, "false");
            } else if (!useSubjectCredsVal.toLowerCase(Locale.ROOT).equals("false")) {
                // Don't overwrite the prop value if it's already been written to something else,
                // but log because it is likely the Credentials won't be loaded correctly.
                logger.warn("System Property: " + useSubjectCredsProp + " set to: " + useSubjectCredsVal + " not false.  SPNego authentication may not be successful.");
            }
            javax.security.auth.login.Configuration.setConfiguration(jaasConfig);
            //Enable only SPNEGO authentication scheme.
            builder.setAuthSchemeRegistryProvider(() -> {
                Lookup<AuthSchemeProvider> authProviders = RegistryBuilder.<AuthSchemeProvider>create().register(AuthSchemes.SPNEGO, new SPNegoSchemeFactory(true, false)).build();
                return authProviders;
            });
            // Get the credentials from the JAAS configuration rather than here
            Credentials useJaasCreds = new Credentials() {

                public String getPassword() {
                    return null;
                }

                public Principal getUserPrincipal() {
                    return null;
                }
            };
            HttpClientUtil.setCookiePolicy(SolrPortAwareCookieSpecFactory.POLICY_NAME);
            builder.setCookieSpecRegistryProvider(() -> {
                SolrPortAwareCookieSpecFactory cookieFactory = new SolrPortAwareCookieSpecFactory();
                Lookup<CookieSpecProvider> cookieRegistry = RegistryBuilder.<CookieSpecProvider>create().register(SolrPortAwareCookieSpecFactory.POLICY_NAME, cookieFactory).build();
                return cookieRegistry;
            });
            builder.setDefaultCredentialsProvider(() -> {
                CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
                credentialsProvider.setCredentials(AuthScope.ANY, useJaasCreds);
                return credentialsProvider;
            });
            HttpClientUtil.addRequestInterceptor(bufferedEntityInterceptor);
        }
    } else {
        logger.warn("{} is configured without specifying system property '{}'", getClass().getName(), LOGIN_CONFIG_PROP);
    }
    return builder;
}
Also used : BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) CookieSpecProvider(org.apache.http.cookie.CookieSpecProvider) AuthSchemeProvider(org.apache.http.auth.AuthSchemeProvider) SPNegoSchemeFactory(org.apache.http.impl.auth.SPNegoSchemeFactory) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) CredentialsProvider(org.apache.http.client.CredentialsProvider) Credentials(org.apache.http.auth.Credentials)

Example 74 with CredentialsProvider

use of org.apache.http.client.CredentialsProvider in project lucene-solr by apache.

the class PreemptiveBasicAuthClientBuilderFactory method initHttpClientBuilder.

private SolrHttpClientBuilder initHttpClientBuilder(SolrHttpClientBuilder builder) {
    final String basicAuthUser = defaultParams.get(HttpClientUtil.PROP_BASIC_AUTH_USER);
    final String basicAuthPass = defaultParams.get(HttpClientUtil.PROP_BASIC_AUTH_PASS);
    if (basicAuthUser == null || basicAuthPass == null) {
        throw new IllegalArgumentException("username & password must be specified with " + getClass().getName());
    }
    builder.setDefaultCredentialsProvider(new CredentialsProviderProvider() {

        @Override
        public CredentialsProvider getCredentialsProvider() {
            CredentialsProvider credsProvider = new BasicCredentialsProvider();
            credsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(basicAuthUser, basicAuthPass));
            return credsProvider;
        }
    });
    HttpClientUtil.addRequestInterceptor(requestInterceptor);
    return builder;
}
Also used : BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) CredentialsProviderProvider(org.apache.solr.client.solrj.impl.SolrHttpClientBuilder.CredentialsProviderProvider) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) CredentialsProvider(org.apache.http.client.CredentialsProvider) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials)

Example 75 with CredentialsProvider

use of org.apache.http.client.CredentialsProvider in project lucene-solr by apache.

the class PreemptiveAuth method process.

@Override
public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException {
    AuthState authState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE);
    // If no auth scheme available yet, try to initialize it preemptively
    if (authState.getAuthScheme() == null) {
        CredentialsProvider credsProvider = (CredentialsProvider) context.getAttribute(ClientContext.CREDS_PROVIDER);
        Credentials creds = credsProvider.getCredentials(AuthScope.ANY);
        authState.update(authScheme, creds);
    }
}
Also used : AuthState(org.apache.http.auth.AuthState) CredentialsProvider(org.apache.http.client.CredentialsProvider) Credentials(org.apache.http.auth.Credentials)

Aggregations

CredentialsProvider (org.apache.http.client.CredentialsProvider)92 BasicCredentialsProvider (org.apache.http.impl.client.BasicCredentialsProvider)65 UsernamePasswordCredentials (org.apache.http.auth.UsernamePasswordCredentials)57 AuthScope (org.apache.http.auth.AuthScope)51 HttpHost (org.apache.http.HttpHost)28 HttpGet (org.apache.http.client.methods.HttpGet)19 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)17 Test (org.junit.Test)17 HttpResponse (org.apache.http.HttpResponse)16 Credentials (org.apache.http.auth.Credentials)16 IOException (java.io.IOException)13 HttpClientContext (org.apache.http.client.protocol.HttpClientContext)12 BasicScheme (org.apache.http.impl.auth.BasicScheme)12 Header (org.apache.http.Header)11 HttpRequest (org.apache.http.HttpRequest)10 AuthCache (org.apache.http.client.AuthCache)10 BasicAuthCache (org.apache.http.impl.client.BasicAuthCache)10 HttpClientBuilder (org.apache.http.impl.client.HttpClientBuilder)10 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)9 HttpEntity (org.apache.http.HttpEntity)8