Search in sources :

Example 16 with AuthScope

use of org.apache.http.auth.AuthScope in project crawler4j by yasserg.

the class PageFetcher method doNtLogin.

/**
     * Do NT auth for Microsoft AD sites.
     */
private void doNtLogin(NtAuthInfo authInfo) {
    logger.info("NT authentication for: " + authInfo.getLoginTarget());
    HttpHost targetHost = new HttpHost(authInfo.getHost(), authInfo.getPort(), authInfo.getProtocol());
    CredentialsProvider credsProvider = new BasicCredentialsProvider();
    try {
        credsProvider.setCredentials(new AuthScope(targetHost.getHostName(), targetHost.getPort()), new NTCredentials(authInfo.getUsername(), authInfo.getPassword(), InetAddress.getLocalHost().getHostName(), authInfo.getDomain()));
    } catch (UnknownHostException e) {
        logger.error("Error creating NT credentials", e);
    }
    httpClient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();
}
Also used : BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) UnknownHostException(java.net.UnknownHostException) HttpHost(org.apache.http.HttpHost) AuthScope(org.apache.http.auth.AuthScope) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) CredentialsProvider(org.apache.http.client.CredentialsProvider) NTCredentials(org.apache.http.auth.NTCredentials)

Example 17 with AuthScope

use of org.apache.http.auth.AuthScope in project crawler4j by yasserg.

the class PageFetcher method doBasicLogin.

/**
     * BASIC authentication<br/>
     * Official Example: https://hc.apache
     * .org/httpcomponents-client-ga/httpclient/examples/org/apache/http/examples
     * /client/ClientAuthentication.java
     * */
private void doBasicLogin(BasicAuthInfo authInfo) {
    logger.info("BASIC authentication for: " + authInfo.getLoginTarget());
    HttpHost targetHost = new HttpHost(authInfo.getHost(), authInfo.getPort(), authInfo.getProtocol());
    CredentialsProvider credsProvider = new BasicCredentialsProvider();
    credsProvider.setCredentials(new AuthScope(targetHost.getHostName(), targetHost.getPort()), new UsernamePasswordCredentials(authInfo.getUsername(), authInfo.getPassword()));
    httpClient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();
}
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 18 with AuthScope

use of org.apache.http.auth.AuthScope in project opennms by OpenNMS.

the class HttpClientWrapper method enablePreemptiveAuth.

protected void enablePreemptiveAuth(final HttpClientBuilder builder) {
    /**
         * Add an HttpRequestInterceptor that will perform preemptive authentication
         * @see http://hc.apache.org/httpcomponents-client-4.0.1/tutorial/html/authentication.html
         */
    final HttpRequestInterceptor preemptiveAuth = new HttpRequestInterceptor() {

        @Override
        public void process(final HttpRequest request, final HttpContext context) throws IOException {
            if (context instanceof HttpClientContext) {
                final HttpClientContext clientContext = (HttpClientContext) context;
                final AuthState authState = clientContext.getTargetAuthState();
                final CredentialsProvider credsProvider = clientContext.getCredentialsProvider();
                final HttpHost targetHost = clientContext.getTargetHost();
                // If not authentication scheme has been initialized yet
                if (authState.getAuthScheme() == null) {
                    final AuthScope authScope = new AuthScope(targetHost.getHostName(), targetHost.getPort());
                    // Obtain credentials matching the target host
                    final Credentials creds = credsProvider.getCredentials(authScope);
                    // If found, generate BasicScheme preemptively
                    if (creds != null) {
                        authState.update(new BasicScheme(), creds);
                    }
                }
            } else {
                throw new IllegalArgumentException("Not sure how to handle a non-HttpClientContext context.");
            }
        }
    };
    builder.addInterceptorFirst(preemptiveAuth);
}
Also used : HttpRequest(org.apache.http.HttpRequest) BasicScheme(org.apache.http.impl.auth.BasicScheme) AuthState(org.apache.http.auth.AuthState) HttpHost(org.apache.http.HttpHost) HttpRequestInterceptor(org.apache.http.HttpRequestInterceptor) HttpContext(org.apache.http.protocol.HttpContext) AuthScope(org.apache.http.auth.AuthScope) HttpClientContext(org.apache.http.client.protocol.HttpClientContext) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) CredentialsProvider(org.apache.http.client.CredentialsProvider) Credentials(org.apache.http.auth.Credentials) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials)

Example 19 with AuthScope

use of org.apache.http.auth.AuthScope in project opennms by OpenNMS.

the class ScvEnabledRestClientImpl method getResponse.

// Setup a client with pre-emptive authentication
private CloseableHttpResponse getResponse(HttpGet httpget) throws Exception {
    CloseableHttpResponse response = null;
    HttpHost target = new HttpHost(url.getHost(), url.getPort(), url.getProtocol());
    CredentialsProvider credsProvider = new BasicCredentialsProvider();
    credsProvider.setCredentials(new AuthScope(target.getHostName(), target.getPort()), getCredentials());
    CloseableHttpClient httpclient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();
    AuthCache authCache = new BasicAuthCache();
    BasicScheme basicAuth = new BasicScheme();
    authCache.put(target, basicAuth);
    HttpClientContext localContext = HttpClientContext.create();
    localContext.setAuthCache(authCache);
    response = httpclient.execute(target, httpget, localContext);
    return response;
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) BasicScheme(org.apache.http.impl.auth.BasicScheme) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) HttpHost(org.apache.http.HttpHost) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) AuthScope(org.apache.http.auth.AuthScope) AuthCache(org.apache.http.client.AuthCache) BasicAuthCache(org.apache.http.impl.client.BasicAuthCache) 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)

Example 20 with AuthScope

use of org.apache.http.auth.AuthScope in project tdi-studio-se by Talend.

the class DynamicsCRMClient method setHttpclientProxy.

/**
     * Setup proxy for httpClient
     */
private void setHttpclientProxy(DefaultHttpClient httpClient) {
    Proxy proxy = getProxy();
    String proxyUser = System.getProperty("https.proxyUser");
    String proxyPwd = System.getProperty("https.proxyPassword");
    // set by other components like tSetProxy
    if (proxy != null) {
        final HttpHost httpHost = new HttpHost(((InetSocketAddress) proxy.address()).getHostName(), ((InetSocketAddress) proxy.address()).getPort());
        // Sets usage of HTTP proxy
        httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, httpHost);
        // TODO Because the proxy of get accesToken can't support authentication. remove this ?
        if (proxyUser != null && proxyPwd != null) {
            httpClient.getCredentialsProvider().setCredentials(new AuthScope(httpHost), new UsernamePasswordCredentials(proxyUser, proxyPwd));
        }
    }
}
Also used : Proxy(java.net.Proxy) HttpHost(org.apache.http.HttpHost) AuthScope(org.apache.http.auth.AuthScope) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials)

Aggregations

AuthScope (org.apache.http.auth.AuthScope)95 UsernamePasswordCredentials (org.apache.http.auth.UsernamePasswordCredentials)60 CredentialsProvider (org.apache.http.client.CredentialsProvider)42 BasicCredentialsProvider (org.apache.http.impl.client.BasicCredentialsProvider)41 HttpHost (org.apache.http.HttpHost)28 Credentials (org.apache.http.auth.Credentials)22 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)19 Test (org.junit.Test)19 HttpResponse (org.apache.http.HttpResponse)17 HttpClientContext (org.apache.http.client.protocol.HttpClientContext)14 HttpGet (org.apache.http.client.methods.HttpGet)13 DefaultHttpClient (org.apache.http.impl.client.DefaultHttpClient)12 IOException (java.io.IOException)11 BasicScheme (org.apache.http.impl.auth.BasicScheme)11 HttpEntity (org.apache.http.HttpEntity)10 AuthScheme (org.apache.http.auth.AuthScheme)8 NTCredentials (org.apache.http.auth.NTCredentials)7 AuthCache (org.apache.http.client.AuthCache)7 BasicAuthCache (org.apache.http.impl.client.BasicAuthCache)7 URL (java.net.URL)6