Search in sources :

Example 1 with CredentialsProvider

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

the class PreemptiveAuthInterceptor method process.

public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException {
    AuthState authState = (AuthState) context.getAttribute(HttpClientContext.TARGET_AUTH_STATE);
    // If no auth scheme avaialble yet, try to initialize it preemptively
    if (authState.getAuthScheme() == null) {
        AuthScheme authScheme = (AuthScheme) context.getAttribute("preemptive-auth");
        CredentialsProvider credsProvider = (CredentialsProvider) context.getAttribute(HttpClientContext.CREDS_PROVIDER);
        if (authScheme != null) {
            Credentials creds = credsProvider.getCredentials(AuthScope.ANY);
            if (creds == null) {
                throw new HttpException("No credentials for preemptive authentication");
            }
            authState.update(authScheme, creds);
        }
    }
}
Also used : AuthState(org.apache.http.auth.AuthState) HttpException(org.apache.http.HttpException) CredentialsProvider(org.apache.http.client.CredentialsProvider) Credentials(org.apache.http.auth.Credentials) AuthScheme(org.apache.http.auth.AuthScheme)

Example 2 with CredentialsProvider

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

the class AuthenticatorTestCase method getHttpClient.

private HttpClient getHttpClient() {
    HttpClientBuilder builder = HttpClientBuilder.create();
    // Register auth schema
    builder.setDefaultAuthSchemeRegistry(s -> httpContext -> new SPNegoScheme(true, true));
    Credentials useJaasCreds = new Credentials() {

        public String getPassword() {
            return null;
        }

        public Principal getUserPrincipal() {
            return null;
        }
    };
    CredentialsProvider jaasCredentialProvider = new BasicCredentialsProvider();
    jaasCredentialProvider.setCredentials(AuthScope.ANY, useJaasCreds);
    // Set credential provider
    builder.setDefaultCredentialsProvider(jaasCredentialProvider);
    return builder.build();
}
Also used : SPNegoScheme(org.apache.http.impl.auth.SPNegoScheme) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) HttpClientBuilder(org.apache.http.impl.client.HttpClientBuilder) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) CredentialsProvider(org.apache.http.client.CredentialsProvider) Credentials(org.apache.http.auth.Credentials)

Example 3 with CredentialsProvider

use of org.apache.http.client.CredentialsProvider in project weixin-java-tools by chanjarster.

the class WxCpServiceImpl method setWxCpConfigStorage.

public void setWxCpConfigStorage(WxCpConfigStorage wxConfigProvider) {
    this.wxCpConfigStorage = wxConfigProvider;
    String http_proxy_host = wxCpConfigStorage.getHttp_proxy_host();
    int http_proxy_port = wxCpConfigStorage.getHttp_proxy_port();
    String http_proxy_username = wxCpConfigStorage.getHttp_proxy_username();
    String http_proxy_password = wxCpConfigStorage.getHttp_proxy_password();
    if (StringUtils.isNotBlank(http_proxy_host)) {
        // 使用代理服务器
        if (StringUtils.isNotBlank(http_proxy_username)) {
            // 需要用户认证的代理服务器
            CredentialsProvider credsProvider = new BasicCredentialsProvider();
            credsProvider.setCredentials(new AuthScope(http_proxy_host, http_proxy_port), new UsernamePasswordCredentials(http_proxy_username, http_proxy_password));
            httpClient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();
        } else {
            // 无需用户认证的代理服务器
            httpClient = HttpClients.createDefault();
        }
        httpProxy = new HttpHost(http_proxy_host, http_proxy_port);
    } else {
        httpClient = HttpClients.createDefault();
    }
}
Also used : BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) HttpHost(org.apache.http.HttpHost) AuthScope(org.apache.http.auth.AuthScope) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) CredentialsProvider(org.apache.http.client.CredentialsProvider) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials)

Example 4 with CredentialsProvider

use of org.apache.http.client.CredentialsProvider in project webmagic by code4craft.

the class HttpClientGenerator method generateClient.

private CloseableHttpClient generateClient(Site site, Proxy proxy) {
    CredentialsProvider credsProvider = null;
    HttpClientBuilder httpClientBuilder = HttpClients.custom();
    if (proxy != null && StringUtils.isNotBlank(proxy.getUser()) && StringUtils.isNotBlank(proxy.getPassword())) {
        credsProvider = new BasicCredentialsProvider();
        credsProvider.setCredentials(new AuthScope(proxy.getHttpHost().getAddress().getHostAddress(), proxy.getHttpHost().getPort()), new UsernamePasswordCredentials(proxy.getUser(), proxy.getPassword()));
        httpClientBuilder.setDefaultCredentialsProvider(credsProvider);
    }
    if (site != null && site.getHttpProxy() != null && site.getUsernamePasswordCredentials() != null) {
        credsProvider = new BasicCredentialsProvider();
        credsProvider.setCredentials(//可以访问的范围
        new AuthScope(site.getHttpProxy()), //用户名和密码
        site.getUsernamePasswordCredentials());
        httpClientBuilder.setDefaultCredentialsProvider(credsProvider);
    }
    httpClientBuilder.setConnectionManager(connectionManager);
    if (site != null && site.getUserAgent() != null) {
        httpClientBuilder.setUserAgent(site.getUserAgent());
    } else {
        httpClientBuilder.setUserAgent("");
    }
    if (site == null || site.isUseGzip()) {
        httpClientBuilder.addInterceptorFirst(new HttpRequestInterceptor() {

            public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException {
                if (!request.containsHeader("Accept-Encoding")) {
                    request.addHeader("Accept-Encoding", "gzip");
                }
            }
        });
    }
    //解决post/redirect/post 302跳转问题
    httpClientBuilder.setRedirectStrategy(new CustomRedirectStrategy());
    SocketConfig.Builder socketConfigBuilder = SocketConfig.custom();
    socketConfigBuilder.setSoKeepAlive(true).setTcpNoDelay(true);
    if (site != null) {
        socketConfigBuilder.setSoTimeout(site.getTimeOut());
    }
    SocketConfig socketConfig = socketConfigBuilder.build();
    httpClientBuilder.setDefaultSocketConfig(socketConfig);
    connectionManager.setDefaultSocketConfig(socketConfig);
    if (site != null) {
        httpClientBuilder.setRetryHandler(new DefaultHttpRequestRetryHandler(site.getRetryTimes(), true));
        generateCookie(httpClientBuilder, site);
    }
    return httpClientBuilder.build();
}
Also used : HttpRequest(org.apache.http.HttpRequest) SocketConfig(org.apache.http.config.SocketConfig) HttpContext(org.apache.http.protocol.HttpContext) CredentialsProvider(org.apache.http.client.CredentialsProvider) IOException(java.io.IOException) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials) HttpRequestInterceptor(org.apache.http.HttpRequestInterceptor) AuthScope(org.apache.http.auth.AuthScope) HttpException(org.apache.http.HttpException)

Example 5 with CredentialsProvider

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

the class HttpClientBuilderTest method usesProxyWithAuth.

@Test
public void usesProxyWithAuth() throws Exception {
    HttpClientConfiguration config = new HttpClientConfiguration();
    AuthConfiguration auth = new AuthConfiguration("secret", "stuff");
    ProxyConfiguration proxy = new ProxyConfiguration("192.168.52.11", 8080, "http", auth);
    config.setProxyConfiguration(proxy);
    CloseableHttpClient httpClient = checkProxy(config, new HttpHost("dropwizard.io", 80), new HttpHost("192.168.52.11", 8080, "http"));
    CredentialsProvider credentialsProvider = (CredentialsProvider) FieldUtils.getField(httpClient.getClass(), "credentialsProvider", true).get(httpClient);
    assertThat(credentialsProvider.getCredentials(new AuthScope("192.168.52.11", 8080))).isEqualTo(new UsernamePasswordCredentials("secret", "stuff"));
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) ProxyConfiguration(io.dropwizard.client.proxy.ProxyConfiguration) HttpHost(org.apache.http.HttpHost) AuthScope(org.apache.http.auth.AuthScope) AuthConfiguration(io.dropwizard.client.proxy.AuthConfiguration) CredentialsProvider(org.apache.http.client.CredentialsProvider) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials) Test(org.junit.Test)

Aggregations

CredentialsProvider (org.apache.http.client.CredentialsProvider)79 BasicCredentialsProvider (org.apache.http.impl.client.BasicCredentialsProvider)52 UsernamePasswordCredentials (org.apache.http.auth.UsernamePasswordCredentials)50 AuthScope (org.apache.http.auth.AuthScope)43 HttpHost (org.apache.http.HttpHost)26 HttpGet (org.apache.http.client.methods.HttpGet)16 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)16 HttpResponse (org.apache.http.HttpResponse)15 Test (org.junit.Test)14 Credentials (org.apache.http.auth.Credentials)13 IOException (java.io.IOException)12 Header (org.apache.http.Header)11 HttpClientContext (org.apache.http.client.protocol.HttpClientContext)11 HttpRequest (org.apache.http.HttpRequest)10 BasicScheme (org.apache.http.impl.auth.BasicScheme)9 HttpClientBuilder (org.apache.http.impl.client.HttpClientBuilder)9 HttpEntity (org.apache.http.HttpEntity)8 AuthenticationException (org.apache.http.auth.AuthenticationException)8 AbortableHttpRequest (org.apache.http.client.methods.AbortableHttpRequest)8 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)8