Example usage for org.apache.http.auth AuthScope AuthScope

List of usage examples for org.apache.http.auth AuthScope AuthScope

Introduction

In this page you can find the example usage for org.apache.http.auth AuthScope AuthScope.

Prototype

public AuthScope(final String host, final int port, final String realm) 

Source Link

Document

Creates a new credentials scope for the given host, port, realm, and any authentication scheme.

Usage

From source file:com.dlmu.heipacker.crawler.client.ClientKerberosAuthentication.java

public static void main(String[] args) throws Exception {

    System.setProperty("java.security.auth.login.config", "login.conf");
    System.setProperty("java.security.krb5.conf", "krb5.conf");
    System.setProperty("sun.security.krb5.debug", "true");
    System.setProperty("javax.security.auth.useSubjectCredsOnly", "false");

    DefaultHttpClient httpclient = new DefaultHttpClient();
    try {//from   www  . ja va 2s  .  c  o  m
        httpclient.getAuthSchemes().register(AuthPolicy.SPNEGO, new SPNegoSchemeFactory());

        Credentials use_jaas_creds = new Credentials() {

            public String getPassword() {
                return null;
            }

            public Principal getUserPrincipal() {
                return null;
            }

        };

        httpclient.getCredentialsProvider().setCredentials(new AuthScope(null, -1, null), use_jaas_creds);

        HttpUriRequest request = new HttpGet("http://kerberoshost/");
        HttpResponse response = httpclient.execute(request);
        HttpEntity entity = response.getEntity();

        System.out.println("----------------------------------------");
        System.out.println(response.getStatusLine());
        System.out.println("----------------------------------------");
        if (entity != null) {
            System.out.println(EntityUtils.toString(entity));
        }
        System.out.println("----------------------------------------");

        // This ensures the connection gets released back to the manager
        EntityUtils.consume(entity);

    } finally {
        // When HttpClient instance is no longer needed,
        // shut down the connection manager to ensure
        // immediate deallocation of all system resources
        httpclient.getConnectionManager().shutdown();
    }
}

From source file:se.anyro.tagtider.utils.Http.java

public static DefaultHttpClient getClient() {
    if (client == null) {
        client = new DefaultHttpClient();
        Credentials creds = new UsernamePasswordCredentials("tagtider", "codemocracy");
        AuthScope scope = new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM);
        client.getCredentialsProvider().setCredentials(scope, creds);
    }//from   w  w  w  .  jav a2s  . c  om
    return client;
}

From source file:com.couchbase.capi.CAPITestCase.java

protected HttpClient getClient() {
    DefaultHttpClient result = new DefaultHttpClient();

    result.getCredentialsProvider().setCredentials(new AuthScope(null, -1, null),
            new UsernamePasswordCredentials("Administrator", "password"));

    return result;
}

From source file:org.nuxeo.connect.connector.http.ProxyHelper.java

public static void configureProxyIfNeeded(RequestConfig.Builder requestConfigBuilder,
        CredentialsProvider credentialsProvider, String url) {
    if (ConnectUrlConfig.useProxy()) {
        // configure proxy host
        HttpHost proxyHost = null;//  w w w . j  av a  2 s.c o m
        if (ConnectUrlConfig.useProxyPac()) {
            String[] proxy = pacResolver.findProxy(url);
            if (proxy != null) {
                proxyHost = new HttpHost(proxy[0], Integer.parseInt(proxy[1]));
            }
        } else {
            proxyHost = new HttpHost(ConnectUrlConfig.getProxyHost(), ConnectUrlConfig.getProxyPort());
        }

        if (proxyHost != null) {
            requestConfigBuilder.setProxy(proxyHost);
            // configure proxy auth in BA
            if (ConnectUrlConfig.isProxyAuthenticated()) {
                AuthScope authScope = new AuthScope(proxyHost.getHostName(), proxyHost.getPort(),
                        AuthScope.ANY_REALM);
                if (ConnectUrlConfig.isProxyNTLM()) {
                    NTCredentials ntlmCredential = new NTCredentials(ConnectUrlConfig.getProxyLogin(),
                            ConnectUrlConfig.getProxyPassword(), ConnectUrlConfig.getProxyNTLMHost(),
                            ConnectUrlConfig.getProxyNTLMDomain());
                    credentialsProvider.setCredentials(authScope, ntlmCredential);
                } else {
                    Credentials ba = new UsernamePasswordCredentials(ConnectUrlConfig.getProxyLogin(),
                            ConnectUrlConfig.getProxyPassword());
                    credentialsProvider.setCredentials(authScope, ba);
                }
            }
        }
    }
}

From source file:org.opendaylight.defense4all.framework.cli.ControlappsConnector.java

public ControlappsConnector(String username, String password, String restSubPath) throws Exception {

    this.username = username;
    this.password = password;
    restPrefix = "http://" + RESTSERVICE_HOSTNAME + ":" + RESTSERVICE_PORT + restSubPath;

    try {//from   w w w.  j  a  v a 2 s. co m
        objMapper = new ObjectMapper();
        objMapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false); // Ignore unknown fields

        // set authentication for rest template
        AuthScope authScope = new AuthScope(RESTSERVICE_HOSTNAME, RESTSERVICE_PORT, AuthScope.ANY_REALM);
        UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(username, password);
        DefaultHttpClient client = new DefaultHttpClient();
        client.getCredentialsProvider().setCredentials(authScope, credentials);

        HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory(client);
        restTemplate = new RestTemplate(factory);
        if (restTemplate == null)
            throw new Exception("");
    } catch (Throwable e) {
        throw new Exception("Failed to initialize connection with controlapps. Is it up?");
    }
}

From source file:org.tnova.service.catalog.client.RestTemplateFactory.java

@Override
public void afterPropertiesSet() throws Exception {
    final int timeout = 5;

    final RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(timeout * 1000)
            .setSocketTimeout(timeout * 1000).build();

    final BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
    credentialsProvider.setCredentials(new AuthScope("localhost", 8080, AuthScope.ANY_REALM),
            new UsernamePasswordCredentials("user1", "user1Pass"));
    final CloseableHttpClient client = HttpClientBuilder.create().setDefaultRequestConfig(requestConfig)
            .setDefaultCredentialsProvider(credentialsProvider).build();

    final ClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(client);
    restTemplate = new RestTemplate(requestFactory);

}

From source file:info.ajaxplorer.client.http.AjxpHttpClient.java

public void refreshCredentials(String user, String pass) {
    this.getCredentialsProvider().setCredentials(
            new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM),
            new UsernamePasswordCredentials(user, pass));
}

From source file:org.dmfs.android.authenticator.handlers.BasicHttpClientAuthenticationHandler.java

@Override
public void authenticate(AbstractHttpClient client) {
    // just set the credentials assuming that proper authentication schemes are registered with the AbstractHttpClient.
    CredentialsProvider credsProvider = new BasicCredentialsProvider();
    credsProvider.setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, mAuthToken.getRealm()),
            new UsernamePasswordCredentials(mAuthToken.getUsername(), mAuthToken.getPassword()));

    client.setCredentialsProvider(credsProvider);
}