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:org.transdroid.daemon.util.HttpHelper.java

/**
 * Creates a standard Apache HttpClient that is thread safe, supports different SSL auth methods and basic
 * authentication//from  w  w  w  . j a  va2s.  c o  m
 * @param sslTrustAll Whether to trust all SSL certificates
 * @param sslTrustkey A specific SSL key to accept exclusively
 * @param timeout The connection timeout for all requests
 * @param authAddress The authentication domain address
 * @param authPort The authentication domain port number
 * @return An HttpClient that should be stored locally and reused for every new request
 * @throws DaemonException Thrown when information (such as username/password) is missing
 */
public static DefaultHttpClient createStandardHttpClient(boolean userBasicAuth, String username,
        String password, boolean sslTrustAll, String sslTrustkey, int timeout, String authAddress, int authPort)
        throws DaemonException {

    // Register http and https sockets
    SchemeRegistry registry = new SchemeRegistry();
    registry.register(new Scheme("http", new PlainSocketFactory(), 80));
    SocketFactory https_socket = sslTrustAll ? new FakeSocketFactory()
            : sslTrustkey != null ? new FakeSocketFactory(sslTrustkey) : SSLSocketFactory.getSocketFactory();
    registry.register(new Scheme("https", https_socket, 443));

    // Standard parameters
    HttpParams httpparams = new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(httpparams, timeout);
    HttpConnectionParams.setSoTimeout(httpparams, timeout);
    if (userAgent != null) {
        HttpProtocolParams.setUserAgent(httpparams, userAgent);
    }

    DefaultHttpClient httpclient = new DefaultHttpClient(new ThreadSafeClientConnManager(httpparams, registry),
            httpparams);

    // Authentication credentials
    if (userBasicAuth) {
        if (username == null || password == null) {
            throw new DaemonException(ExceptionType.AuthenticationFailure,
                    "No username or password was provided while we hadauthentication enabled");
        }
        httpclient.getCredentialsProvider().setCredentials(
                new AuthScope(authAddress, authPort, AuthScope.ANY_REALM),
                new UsernamePasswordCredentials(username, password));
    }

    return httpclient;

}

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   ww  w  .j  av  a2  s  .c o  m*/
        public String getPassword() {
            return null;
        }

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

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

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

/**
 * Gets a HTTP client./*from   ww w.  j  ava2s. 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;
}

From source file:org.squashtest.tm.plugin.testautomation.jenkins.internal.net.HttpClientProvider.java

protected void registerServer(TestAutomationServer server) {

    URL baseURL = server.getBaseURL();

    credentialsProvider.setCredentials(new AuthScope(baseURL.getHost(), baseURL.getPort(), AuthScope.ANY_REALM),
            new UsernamePasswordCredentials(server.getLogin(), server.getPassword()));

}