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:org.keycloak.testsuite.federation.kerberos.AbstractKerberosTest.java

protected void initHttpClient(boolean useSpnego) {
    if (client != null) {
        cleanupApacheHttpClient();/*from  w ww. j  a v a2s  .co m*/
    }

    DefaultHttpClient httpClient = (DefaultHttpClient) new HttpClientBuilder().disableCookieCache(false)
            .build();

    httpClient.getAuthSchemes().register(AuthSchemes.SPNEGO, spnegoSchemeFactory);

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

            @Override
            public String getPassword() {
                return null;
            }

            @Override
            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:my.madet.function.HttpParser.java

public String FetchUrL(String url) {
    StringBuffer result = new StringBuffer();
    DefaultHttpClient httpclient = new DefaultHttpClient();
    try {/*w  ww.  j  a v a  2  s  . co m*/
        // register ntlm auth scheme
        httpclient.getAuthSchemes().register("ntlm", new NTLMSchemeFactory());
        httpclient.getCredentialsProvider().setCredentials(
                new AuthScope("info.uniten.edu.my", AuthScope.ANY_PORT),
                new NTCredentials(unitenid, unitenpassw, "", ""));

        HttpGet request = new HttpGet(url);
        HttpResponse httpResponse = httpclient.execute(request);

        BufferedReader br = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent()));

        String line = "";
        while ((line = br.readLine()) != null) {
            result.append(line);
        }

        br.close();

    } catch (UnknownHostException e) { // no internet connection catch
        e.printStackTrace();
        Log.e("FetchUrL", "No internet ");
        return "No_Internet";
    } catch (Exception e) {
        Log.e("FetchUrL", "Error in http connection:" + e.toString());
        return null;
    }

    String resultString = result.toString();
    String regex = "(?i)<h1>Unauthorized Access</h1>";
    Pattern pattern = Pattern.compile(regex);
    Matcher matcher = pattern.matcher(resultString);
    if (matcher.matches())
        return "Unauthorized";

    Log.i("FetchUrL content: ", result.toString());
    return resultString;

}

From source file:com.xebialabs.overthere.cifs.winrm.WinRmClient.java

private void configureHttpClient(final DefaultHttpClient httpclient) throws GeneralSecurityException {
    configureTrust(httpclient);// www  .j a  v a2 s.  c  o  m

    configureAuthentication(httpclient, BASIC, new BasicUserPrincipal(username));

    if (enableKerberos) {
        String spnServiceClass = kerberosUseHttpSpn ? "HTTP" : "WSMAN";
        httpclient.getAuthSchemes().register(KERBEROS, new WsmanKerberosSchemeFactory(!kerberosAddPortToSpn,
                spnServiceClass, unmappedAddress, unmappedPort));
        httpclient.getAuthSchemes().register(SPNEGO, new WsmanSPNegoSchemeFactory(!kerberosAddPortToSpn,
                spnServiceClass, unmappedAddress, unmappedPort));
        configureAuthentication(httpclient, KERBEROS, new KerberosPrincipal(username));
        configureAuthentication(httpclient, SPNEGO, new KerberosPrincipal(username));
    }

    httpclient.getParams().setBooleanParameter(HANDLE_AUTHENTICATION, true);
}

From source file:org.brunocvcunha.taskerbox.core.http.TaskerboxHttpBox.java

/**
 * Build a new HTTP Client for the given parameters
 *
 * @param params/*from   w  w  w .ja  v  a  2  s.  co  m*/
 * @return
 */
public DefaultHttpClient buildNewHttpClient(HttpParams params) {
    PoolingClientConnectionManager cxMgr = new PoolingClientConnectionManager(
            SchemeRegistryFactory.createDefault());
    cxMgr.setMaxTotal(100);
    cxMgr.setDefaultMaxPerRoute(20);

    DefaultHttpClient httpClient = new DefaultHttpClient(cxMgr, params);
    httpClient.getParams().setParameter(CoreProtocolPNames.USER_AGENT,
            "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.111 Safari/537.36");
    // httpClient.getParams().setParameter(ClientPNames.COOKIE_POLICY,
    // CookiePolicy.BROWSER_COMPATIBILITY);
    if (this.useNtlm) {
        httpClient.getAuthSchemes().register("NTLM", new NTLMSchemeFactory());
        httpClient.getAuthSchemes().register("BASIC", new BasicSchemeFactory());
        httpClient.getAuthSchemes().register("DIGEST", new DigestSchemeFactory());
        httpClient.getAuthSchemes().register("SPNEGO", new SPNegoSchemeFactory());
        httpClient.getAuthSchemes().register("KERBEROS", new KerberosSchemeFactory());
    }

    try {
        SSLContext sc = SSLContext.getInstance("SSL");
        sc.init(null, getTrustingManager(), new java.security.SecureRandom());
        SSLSocketFactory socketFactory = new SSLSocketFactory(sc);
        Scheme sch = new Scheme("https", 443, socketFactory);
        httpClient.getConnectionManager().getSchemeRegistry().register(sch);
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (KeyManagementException e) {
        e.printStackTrace();
    }

    if (this.useProxy) {
        if (this.proxySocks) {

            log.info("Using proxy socks " + this.socksHost + ":" + this.socksPort);

            System.setProperty("socksProxyHost", this.socksHost);
            System.setProperty("socksProxyPort", String.valueOf(this.socksPort));

        } else {
            HttpHost proxy = new HttpHost(this.proxyHost, this.proxyPort);
            httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);

            if (this.authProxy) {

                List<String> authPreferences = new ArrayList<>();

                if (this.ntlmProxy) {

                    NTCredentials creds = new NTCredentials(this.proxyUser, this.proxyPassword,
                            this.proxyWorkstation, this.proxyDomain);
                    httpClient.getCredentialsProvider()
                            .setCredentials(new AuthScope(this.proxyHost, this.proxyPort), creds);
                    // httpClient.getCredentialsProvider().setCredentials(
                    // AuthScope.ANY, creds);

                    authPreferences.add(AuthPolicy.NTLM);
                } else {
                    UsernamePasswordCredentials creds = new UsernamePasswordCredentials(this.proxyUser,
                            this.proxyPassword);
                    httpClient.getCredentialsProvider().setCredentials(AuthScope.ANY, creds);

                    authPreferences.add(AuthPolicy.BASIC);
                }

                httpClient.getParams().setParameter(AuthPNames.TARGET_AUTH_PREF, authPreferences);
            }
        }

    }

    return httpClient;
}

From source file:org.obiba.opal.rest.client.magma.OpalJavaClient.java

private void createClient() {
    log.info("Connecting to Opal: {}", opalURI);
    DefaultHttpClient httpClient = new DefaultHttpClient();
    if (keyStore == null)
        httpClient.getCredentialsProvider().setCredentials(AuthScope.ANY, credentials);
    httpClient.getParams().setParameter(ClientPNames.HANDLE_AUTHENTICATION, Boolean.TRUE);
    httpClient.getParams().setParameter(AuthPNames.TARGET_AUTH_PREF,
            Collections.singletonList(OpalAuth.CREDENTIALS_HEADER));
    httpClient.getParams().setParameter(ClientPNames.CONNECTION_MANAGER_FACTORY_CLASS_NAME,
            OpalClientConnectionManagerFactory.class.getName());
    httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, connectionTimeout);
    httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, soTimeout);
    httpClient.setHttpRequestRetryHandler(new DefaultHttpRequestRetryHandler(DEFAULT_MAX_ATTEMPT, false));
    httpClient.getAuthSchemes().register(OpalAuth.CREDENTIALS_HEADER, new OpalAuthScheme.Factory());

    try {//from  w w  w.  j  a  v  a 2 s.  c om
        httpClient.getConnectionManager().getSchemeRegistry()
                .register(new Scheme("https", HTTPS_PORT, getSocketFactory()));
    } catch (NoSuchAlgorithmException | KeyManagementException e) {
        throw new RuntimeException(e);
    }
    client = enableCaching(httpClient);

    ctx = new BasicHttpContext();
    ctx.setAttribute(ClientContext.COOKIE_STORE, new BasicCookieStore());
}

From source file:com.telefonica.iot.cygnus.backends.http.HttpClientFactory.java

/**
 * Gets a HTTP client./*from  ww w .  j  a  v  a 2s .  c o m*/
 * @param ssl True if SSL connections are desired. False otherwise
 * @param krb5Auth.
 * @return A http client obtained from the (SSL) Connections Manager.
 */
public DefaultHttpClient getHttpClient(boolean ssl, boolean krb5Auth) {
    DefaultHttpClient httpClient;

    if (ssl) {
        httpClient = new DefaultHttpClient(sslConnectionsManager);
    } else {
        httpClient = new DefaultHttpClient(connectionsManager);
    } // if else

    if (krb5Auth) {
        // http://stackoverflow.com/questions/21629132/httpclient-set-credentials-for-kerberos-authentication

        System.setProperty("java.security.auth.login.config", loginConfFile);
        System.setProperty("java.security.krb5.conf", krb5ConfFile);
        System.setProperty("sun.security.krb5.debug", "false");
        System.setProperty("javax.security.auth.useSubjectCredsOnly", "false");
        Credentials jaasCredentials = new Credentials() {

            @Override
            public String getPassword() {
                return null;
            } // getPassword

            @Override
            public Principal getUserPrincipal() {
                return null;
            } // getUserPrincipal

        };

        // 'true' means the port is stripped from the principal names
        SPNegoSchemeFactory spnegoSchemeFactory = new SPNegoSchemeFactory(true);
        httpClient.getAuthSchemes().register(AuthPolicy.SPNEGO, spnegoSchemeFactory);
        httpClient.getCredentialsProvider().setCredentials(new AuthScope(null, -1, null), jaasCredentials);
    } // if

    return httpClient;
}