Example usage for org.apache.http.impl.client DefaultHttpClient getAuthSchemes

List of usage examples for org.apache.http.impl.client DefaultHttpClient getAuthSchemes

Introduction

In this page you can find the example usage for org.apache.http.impl.client DefaultHttpClient getAuthSchemes.

Prototype

public synchronized final AuthSchemeRegistry getAuthSchemes() 

Source Link

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   w w  w. j a  v  a 2s.  co  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:org.gradle.api.internal.externalresource.transport.http.ntlm.NTLMSchemeFactory.java

public static void register(DefaultHttpClient httpClient) {
    httpClient.getAuthSchemes().register("ntlm", new NTLMSchemeFactory());
}

From source file:com.eviware.soapui.support.httpclient.JCIFSTest.java

@Test
public void test() throws ParseException, IOException {
    try {// w  w w  . j a va2 s  .co  m
        DefaultHttpClient httpClient = new DefaultHttpClient();

        httpClient.getAuthSchemes().register(AuthPolicy.NTLM, new NTLMSchemeFactory());
        httpClient.getAuthSchemes().register(AuthPolicy.SPNEGO, new NTLMSchemeFactory());

        NTCredentials creds = new NTCredentials("testuser", "kebabsalladT357", "", "");
        httpClient.getCredentialsProvider().setCredentials(AuthScope.ANY, creds);

        HttpHost target = new HttpHost("dev-appsrv01.eviware.local", 81, "http");
        HttpContext localContext = new BasicHttpContext();
        HttpGet httpget = new HttpGet("/");

        HttpResponse response1 = httpClient.execute(target, httpget, localContext);
        HttpEntity entity1 = response1.getEntity();

        //      System.out.println( "----------------------------------------" );
        //System.out.println( response1.getStatusLine() );
        //      System.out.println( "----------------------------------------" );
        if (entity1 != null) {
            //System.out.println( EntityUtils.toString( entity1 ) );
        }
        //      System.out.println( "----------------------------------------" );

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

        Assert.assertEquals(response1.getStatusLine().getStatusCode(), 200);
    } catch (UnknownHostException e) {
        /* ignore */
    } catch (HttpHostConnectException e) {
        /* ignore */
    } catch (SocketException e) {
        /* ignore */
    }

    Assert.assertTrue(true);
}

From source file:nl.esciencecenter.octopus.webservice.JobLauncherServiceTest.java

@Test
public void testMacifyHttpClient() throws URISyntaxException {
    // FIXME Waiting for https://github.com/NLeSC/octopus/issues/38 to be resolved
    JobLauncherConfiguration config = sampleConfiguration();
    DefaultHttpClient httpClient = new DefaultHttpClient();

    JobLauncherService.macifyHttpClient(httpClient, config.getMacs());

    assertTrue("MAC Registered auth scheme", httpClient.getAuthSchemes().getSchemeNames().contains("mac"));

    MacCredential expected_creds = config.getMacs().get(0);
    AuthScope authscope = expected_creds.getAuthScope();
    Credentials creds = httpClient.getCredentialsProvider().getCredentials(authscope);
    assertEquals(expected_creds, creds);

    List<String> authSchemes = Collections.unmodifiableList(Arrays.asList(new String[] { MacScheme.SCHEME_NAME,
            AuthPolicy.SPNEGO, AuthPolicy.KERBEROS, AuthPolicy.NTLM, AuthPolicy.DIGEST, AuthPolicy.BASIC }));
    assertEquals(authSchemes, httpClient.getParams().getParameter(AuthPNames.TARGET_AUTH_PREF));
}

From source file:nl.esciencecenter.osmium.JobLauncherServiceTest.java

@Test
public void testMacifyHttpClient() throws URISyntaxException {
    // FIXME Waiting for https://github.com/NLeSC/xenon/issues/38 to be resolved
    JobLauncherConfiguration config = sampleConfiguration();
    DefaultHttpClient httpClient = new DefaultHttpClient();

    JobLauncherService.macifyHttpClient(httpClient, config.getMacs());

    assertTrue("MAC Registered auth scheme", httpClient.getAuthSchemes().getSchemeNames().contains("mac"));

    MacCredential expected_creds = config.getMacs().get(0);
    AuthScope authscope = expected_creds.getAuthScope();
    Credentials creds = httpClient.getCredentialsProvider().getCredentials(authscope);
    assertEquals(expected_creds, creds);

    List<String> authSchemes = Collections.unmodifiableList(Arrays.asList(new String[] { MacScheme.SCHEME_NAME,
            AuthPolicy.SPNEGO, AuthPolicy.KERBEROS, AuthPolicy.NTLM, AuthPolicy.DIGEST, AuthPolicy.BASIC }));
    assertEquals(authSchemes, httpClient.getParams().getParameter(AuthPNames.TARGET_AUTH_PREF));
}

From source file:org.apache.hadoop.gateway.dispatch.AppCookieManager.java

/**
 * Fetches hadoop.auth cookie from hadoop service authenticating using SpNego
 * //  w  ww. j a  v  a  2s  .co m
 * @param outboundRequest
 *          out going request
 * @param refresh
 *          flag indicating whether to refresh the cached cookie
 * @return hadoop.auth cookie from hadoop service authenticating using SpNego
 * @throws IOException
 *           in case of errors
 */
public String getAppCookie(HttpUriRequest outboundRequest, boolean refresh) throws IOException {

    URI uri = outboundRequest.getURI();
    String scheme = uri.getScheme();
    String host = uri.getHost();
    int port = uri.getPort();
    if (!refresh) {
        if (appCookie != null) {
            return appCookie;
        }
    }

    DefaultHttpClient client = new DefaultHttpClient();
    SPNegoSchemeFactory spNegoSF = new SPNegoSchemeFactory(/* stripPort */true);
    // spNegoSF.setSpengoGenerator(new BouncySpnegoTokenGenerator());
    client.getAuthSchemes().register(AuthPolicy.SPNEGO, spNegoSF);
    client.getCredentialsProvider().setCredentials(new AuthScope(/* host */null, /* port */-1, /* realm */null),
            EMPTY_JAAS_CREDENTIALS);

    clearAppCookie();
    String hadoopAuthCookie = null;
    HttpResponse httpResponse = null;
    try {
        HttpHost httpHost = new HttpHost(host, port, scheme);
        HttpRequest httpRequest = createKerberosAuthenticationRequest(outboundRequest);
        httpResponse = client.execute(httpHost, httpRequest);
        Header[] headers = httpResponse.getHeaders(SET_COOKIE);
        hadoopAuthCookie = getHadoopAuthCookieValue(headers);
        EntityUtils.consume(httpResponse.getEntity());
        if (hadoopAuthCookie == null) {
            LOG.failedSPNegoAuthn(uri.toString());
            auditor.audit(Action.AUTHENTICATION, uri.toString(), ResourceType.URI, ActionOutcome.FAILURE);
            throw new IOException("SPNego authn failed, can not get hadoop.auth cookie");
        }
    } finally {
        if (httpResponse != null) {
            HttpEntity entity = httpResponse.getEntity();
            if (entity != null) {
                entity.getContent().close();
            }
        }

    }
    LOG.successfulSPNegoAuthn(uri.toString());
    auditor.audit(Action.AUTHENTICATION, uri.toString(), ResourceType.URI, ActionOutcome.SUCCESS);
    hadoopAuthCookie = HADOOP_AUTH_EQ + quote(hadoopAuthCookie);
    setAppCookie(hadoopAuthCookie);
    return appCookie;
}

From source file:org.apache.ambari.server.controller.internal.AppCookieManager.java

/**
 * Returns hadoop.auth cookie, doing needed SPNego authentication
 * // ww  w.java 2s .  c  o  m
 * @param endpoint
 *          the URL of the Hadoop service
 * @param refresh
 *          flag indicating wehther to refresh the cookie, if
 *          <code>true</code>, we do a new SPNego authentication and refresh
 *          the cookie even if the cookie already exists in local cache
 * @return hadoop.auth cookie value
 * @throws IOException
 *           in case of problem getting hadoop.auth cookie
 */
public String getAppCookie(String endpoint, boolean refresh) throws IOException {

    HttpUriRequest outboundRequest = new HttpGet(endpoint);
    URI uri = outboundRequest.getURI();
    String scheme = uri.getScheme();
    String host = uri.getHost();
    int port = uri.getPort();
    String path = uri.getPath();
    if (!refresh) {
        String appCookie = endpointCookieMap.get(endpoint);
        if (appCookie != null) {
            return appCookie;
        }
    }

    clearAppCookie(endpoint);

    DefaultHttpClient client = new DefaultHttpClient();
    SPNegoSchemeFactory spNegoSF = new SPNegoSchemeFactory(/* stripPort */true);
    // spNegoSF.setSpengoGenerator(new BouncySpnegoTokenGenerator());
    client.getAuthSchemes().register(AuthPolicy.SPNEGO, spNegoSF);
    client.getCredentialsProvider().setCredentials(new AuthScope(/* host */null, /* port */-1, /* realm */null),
            EMPTY_JAAS_CREDENTIALS);

    String hadoopAuthCookie = null;
    HttpResponse httpResponse = null;
    try {
        HttpHost httpHost = new HttpHost(host, port, scheme);
        HttpRequest httpRequest = new HttpOptions(path);
        httpResponse = client.execute(httpHost, httpRequest);
        Header[] headers = httpResponse.getHeaders(SET_COOKIE);
        hadoopAuthCookie = getHadoopAuthCookieValue(headers);
        if (hadoopAuthCookie == null) {
            LOG.error("SPNego authentication failed, can not get hadoop.auth cookie for URL: " + endpoint);
            throw new IOException("SPNego authentication failed, can not get hadoop.auth cookie");
        }
    } finally {
        if (httpResponse != null) {
            HttpEntity entity = httpResponse.getEntity();
            if (entity != null) {
                entity.getContent().close();
            }
        }

    }

    hadoopAuthCookie = HADOOP_AUTH_EQ + quote(hadoopAuthCookie);
    setAppCookie(endpoint, hadoopAuthCookie);
    if (LOG.isInfoEnabled()) {
        LOG.info("Successful SPNego authentication to URL:" + uri.toString());
    }
    return hadoopAuthCookie;
}

From source file:org.apache.hadoop.gateway.hive.HiveHttpClientDispatch.java

protected HttpResponse executeKerberosDispatch(HttpUriRequest outboundRequest, DefaultHttpClient client)
        throws IOException, ClientProtocolException {
    //DefaultHttpClient client = new DefaultHttpClient();
    SPNegoSchemeFactory spNegoSF = new SPNegoSchemeFactory(/* stripPort */true);
    // spNegoSF.setSpengoGenerator(new BouncySpnegoTokenGenerator());
    client.getAuthSchemes().register(AuthPolicy.SPNEGO, spNegoSF);
    client.getCredentialsProvider().setCredentials(new AuthScope(/* host */null, /* port */-1, /* realm */null),
            EMPTY_JAAS_CREDENTIALS);/*from  w ww .j  a v  a 2s .  c  o  m*/
    return client.execute(outboundRequest);
}

From source file:org.keycloak.testsuite.adapter.federation.AbstractKerberosAdapterTest.java

protected void initHttpClient(boolean useSpnego) {
    if (client != null) {
        after();/*from   w  w w  .j ava2 s . c  o m*/
    }
    DefaultHttpClient httpClient = (DefaultHttpClient) new HttpClientBuilder().build();
    httpClient.getAuthSchemes().register(AuthPolicy.SPNEGO, spnegoSchemeFactory);

    if (useSpnego) {
        Credentials fake = new Credentials() {

            public String getPassword() {
                return null;
            }

            public Principal getUserPrincipal() {
                return null;
            }

        };

        httpClient.getCredentialsProvider().setCredentials(new AuthScope(null, -1, null), fake);
    }
    ApacheHttpClient4Engine engine = new ApacheHttpClient4Engine(httpClient);
    client = new ResteasyClientBuilder().httpEngine(engine).build();
}

From source file:org.keycloak.testsuite.federation.AbstractKerberosTest.java

protected void initHttpClient(boolean useSpnego) {
    if (client != null) {
        after();/*w w  w. j  a v a2 s.c o  m*/
    }

    DefaultHttpClient httpClient = (DefaultHttpClient) new HttpClientBuilder().build();
    httpClient.getAuthSchemes().register(AuthPolicy.SPNEGO, spnegoSchemeFactory);

    if (useSpnego) {
        Credentials fake = new Credentials() {

            public String getPassword() {
                return null;
            }

            public Principal getUserPrincipal() {
                return null;
            }

        };

        httpClient.getCredentialsProvider().setCredentials(new AuthScope(null, -1, null), fake);
    }

    ApacheHttpClient4Engine engine = new ApacheHttpClient4Engine(httpClient);
    client = new ResteasyClientBuilder().httpEngine(engine).build();
}