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

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

Introduction

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

Prototype

public synchronized void setAuthSchemes(final AuthSchemeRegistry registry) 

Source Link

Usage

From source file:com.mycompany.kerberosbyip.NewMain.java

private void configureHttpClient(final DefaultHttpClient httpclient) throws GeneralSecurityException {
    AuthSchemeRegistry registry = new AuthSchemeRegistry();
    registry.register(KERBEROS, new WsmanKerberosSchemeFactory(true, "WSMAN", ipAddress, port));
    registry.register(SPNEGO, new WsmanSPNegoSchemeFactory(true, "WSMAN", ipAddress, port));
    httpclient.setAuthSchemes(registry);

    final Credentials jaasCreds = new Credentials() {
        @Override/*from w w  w  .j  a  va 2 s .com*/
        public String getPassword() {
            return null;
        }

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

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

From source file:org.apache.kylin.engine.mr.common.HadoopStatusGetter.java

private String getHttpResponseWithKerberosAuth(String url) throws IOException {
    String krb5ConfigPath = System.getProperty("java.security.krb5.conf");
    if (krb5ConfigPath == null) {
        krb5ConfigPath = DEFAULT_KRB5_CONFIG_LOCATION;
    }/*w  ww  .  j av  a  2 s . com*/
    boolean skipPortAtKerberosDatabaseLookup = true;
    System.setProperty("java.security.krb5.conf", krb5ConfigPath);
    System.setProperty("sun.security.krb5.debug", "true");
    System.setProperty("javax.security.auth.useSubjectCredsOnly", "false");

    DefaultHttpClient client = new DefaultHttpClient();
    AuthSchemeRegistry authSchemeRegistry = new AuthSchemeRegistry();
    authSchemeRegistry.register(AuthPolicy.SPNEGO, new SPNegoSchemeFactory(skipPortAtKerberosDatabaseLookup));
    client.setAuthSchemes(authSchemeRegistry);

    BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
    Credentials useJaasCreds = new Credentials() {
        public String getPassword() {
            return null;
        }

        public Principal getUserPrincipal() {
            return null;
        }
    };
    credentialsProvider.setCredentials(new AuthScope(null, -1, null), useJaasCreds);
    client.setCredentialsProvider(credentialsProvider);

    String response = null;
    while (response == null) {
        if (url.startsWith("https://")) {
            registerEasyHttps();
        }
        if (url.contains("anonymous=true") == false) {
            url += url.contains("?") ? "&" : "?";
            url += "anonymous=true";
        }
        HttpGet httpget = new HttpGet(url);
        httpget.addHeader("accept", "application/json");
        try {
            HttpResponse httpResponse = client.execute(httpget);
            String redirect = null;
            org.apache.http.Header h = httpResponse.getFirstHeader("Location");
            if (h != null) {
                redirect = h.getValue();
                if (isValidURL(redirect) == false) {
                    logger.info("Get invalid redirect url, skip it: " + redirect);
                    Thread.sleep(1000L);
                    continue;
                }
            } else {
                h = httpResponse.getFirstHeader("Refresh");
                if (h != null) {
                    String s = h.getValue();
                    int cut = s.indexOf("url=");
                    if (cut >= 0) {
                        redirect = s.substring(cut + 4);

                        if (isValidURL(redirect) == false) {
                            logger.info("Get invalid redirect url, skip it: " + redirect);
                            Thread.sleep(1000L);
                            continue;
                        }
                    }
                }
            }

            if (redirect == null) {
                response = IOUtils.toString(httpResponse.getEntity().getContent(), Charset.defaultCharset());
                logger.debug("Job " + mrJobId + " get status check result.\n");
            } else {
                url = redirect;
                logger.debug("Job " + mrJobId + " check redirect url " + url + ".\n");
            }
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            logger.error(e.getMessage());
        } finally {
            httpget.releaseConnection();
        }
    }

    return response;
}

From source file:org.apache.solr.client.solrj.impl.Krb5HttpClientConfigurer.java

public void configure(DefaultHttpClient httpClient, SolrParams config) {
    super.configure(httpClient, config);

    if (System.getProperty(LOGIN_CONFIG_PROP) != null) {
        String configValue = System.getProperty(LOGIN_CONFIG_PROP);

        if (configValue != null) {
            logger.info("Setting up SPNego auth with config: " + configValue);
            final String useSubjectCredsProp = "javax.security.auth.useSubjectCredsOnly";
            String useSubjectCredsVal = System.getProperty(useSubjectCredsProp);

            // "javax.security.auth.useSubjectCredsOnly" should be false so that the underlying
            // authentication mechanism can load the credentials from the JAAS configuration.
            if (useSubjectCredsVal == null) {
                System.setProperty(useSubjectCredsProp, "false");
            } else if (!useSubjectCredsVal.toLowerCase(Locale.ROOT).equals("false")) {
                // Don't overwrite the prop value if it's already been written to something else,
                // but log because it is likely the Credentials won't be loaded correctly.
                logger.warn("System Property: " + useSubjectCredsProp + " set to: " + useSubjectCredsVal
                        + " not false.  SPNego authentication may not be successful.");
            }//  w w w. j  a  v  a 2s  .com

            javax.security.auth.login.Configuration.setConfiguration(jaasConfig);
            //Enable only SPNEGO authentication scheme.
            AuthSchemeRegistry registry = new AuthSchemeRegistry();
            registry.register(AuthSchemes.SPNEGO, new SPNegoSchemeFactory(true, false));
            httpClient.setAuthSchemes(registry);
            // Get the credentials from the JAAS configuration rather than here
            Credentials useJaasCreds = new Credentials() {
                public String getPassword() {
                    return null;
                }

                public Principal getUserPrincipal() {
                    return null;
                }
            };

            SolrPortAwareCookieSpecFactory cookieFactory = new SolrPortAwareCookieSpecFactory();
            httpClient.getCookieSpecs().register(cookieFactory.POLICY_NAME, cookieFactory);
            httpClient.getParams().setParameter(ClientPNames.COOKIE_POLICY, cookieFactory.POLICY_NAME);

            httpClient.getCredentialsProvider().setCredentials(AuthScope.ANY, useJaasCreds);

            httpClient.addRequestInterceptor(bufferedEntityInterceptor);
        } else {
            httpClient.getCredentialsProvider().clear();
        }
    }
}