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

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

Introduction

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

Prototype

String ANY_REALM

To view the source code for org.apache.http.auth AuthScope ANY_REALM.

Click Source Link

Document

The null value represents any realm.

Usage

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()));

}

From source file:org.eclipse.mylyn.commons.repositories.http.core.HttpUtil.java

public static void configureAuthentication(AbstractHttpClient client, RepositoryLocation location,
        UserCredentials credentials) {/* w  w w  .j  a  v  a2  s  . c  om*/
    Assert.isNotNull(client);
    Assert.isNotNull(location);
    Assert.isNotNull(credentials);
    String url = location.getUrl();
    Assert.isNotNull(url, "The location url must not be null"); //$NON-NLS-1$

    String host = NetUtil.getHost(url);
    int port = NetUtil.getPort(url);

    NTCredentials ntlmCredentials = getNtCredentials(credentials, ""); //$NON-NLS-1$
    if (ntlmCredentials != null) {
        AuthScope authScopeNtlm = new AuthScope(host, port, AuthScope.ANY_REALM, AuthPolicy.NTLM);
        client.getCredentialsProvider().setCredentials(authScopeNtlm, ntlmCredentials);
    }

    UsernamePasswordCredentials usernamePasswordCredentials = getUserNamePasswordCredentials(credentials);
    AuthScope authScopeAny = new AuthScope(host, port, AuthScope.ANY_REALM);
    client.getCredentialsProvider().setCredentials(authScopeAny, usernamePasswordCredentials);
}

From source file:org.elasticsearch.xpack.security.authc.kerberos.SpnegoHttpClientConfigCallbackHandler.java

private void setupSpnegoAuthSchemeSupport(HttpAsyncClientBuilder httpClientBuilder) {
    final Lookup<AuthSchemeProvider> authSchemeRegistry = RegistryBuilder.<AuthSchemeProvider>create()
            .register(AuthSchemes.SPNEGO, new SPNegoSchemeFactory()).build();

    final GSSManager gssManager = GSSManager.getInstance();
    try {/*from  w w  w  .j a  v a  2 s.  c o m*/
        final GSSName gssUserPrincipalName = gssManager.createName(userPrincipalName, GSSName.NT_USER_NAME);
        login();
        final AccessControlContext acc = AccessController.getContext();
        final GSSCredential credential = doAsPrivilegedWrapper(loginContext.getSubject(),
                (PrivilegedExceptionAction<GSSCredential>) () -> gssManager.createCredential(
                        gssUserPrincipalName, GSSCredential.DEFAULT_LIFETIME, SPNEGO_OID,
                        GSSCredential.INITIATE_ONLY),
                acc);

        final KerberosCredentialsProvider credentialsProvider = new KerberosCredentialsProvider();
        credentialsProvider.setCredentials(
                new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM, AuthSchemes.SPNEGO),
                new KerberosCredentials(credential));
        httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
    } catch (GSSException e) {
        throw new RuntimeException(e);
    } catch (PrivilegedActionException e) {
        throw new RuntimeException(e.getCause());
    }
    httpClientBuilder.setDefaultAuthSchemeRegistry(authSchemeRegistry);
}

From source file:com.globo.aclapi.client.ClientAclAPI.java

private static HttpClient newDefaultHttpClient(SSLSocketFactory socketFactory, HttpParams params,
        ProxySelector proxySelector) {
    SchemeRegistry registry = new SchemeRegistry();
    registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
    registry.register(new Scheme("https", socketFactory, 443));

    ClientConnectionManager connectionManager = new ThreadSafeClientConnManager(params, registry);

    DefaultHttpClient httpClient = new DefaultHttpClient(connectionManager, params) {
        @Override/*from   w w w .  ja va2s.c  o  m*/
        protected HttpContext createHttpContext() {
            HttpContext httpContext = super.createHttpContext();
            AuthSchemeRegistry authSchemeRegistry = new AuthSchemeRegistry();
            authSchemeRegistry.register("Bearer", new BearerAuthSchemeFactory());
            httpContext.setAttribute(ClientContext.AUTHSCHEME_REGISTRY, authSchemeRegistry);
            AuthScope sessionScope = new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM,
                    "Bearer");

            Credentials credentials = new TokenCredentials("");
            CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
            credentialsProvider.setCredentials(sessionScope, credentials);
            httpContext.setAttribute(ClientContext.CREDS_PROVIDER, credentialsProvider);
            return httpContext;
        }
    };
    httpClient.setHttpRequestRetryHandler(new DefaultHttpRequestRetryHandler(0, false));
    httpClient.setRoutePlanner(new ProxySelectorRoutePlanner(registry, proxySelector));

    return httpClient;
}

From source file:org.transdroid.daemon.Vuze.VuzeXmlOverHttpClient.java

/**
 * XMLRPCClient constructor. Creates new instance based on server URI
 * @param settings The server connection settings
 * @param uri The URI of the XML RPC to connect to
 * @throws DaemonException Thrown when settings are missing or conflicting
 *///ww w . j  a v a  2s  .c om
public VuzeXmlOverHttpClient(DaemonSettings settings, URI uri) throws DaemonException {
    postMethod = new HttpPost(uri);
    postMethod.addHeader("Content-Type", "text/xml");

    // WARNING
    // I had to disable "Expect: 100-Continue" header since I had 
    // two second delay between sending http POST request and POST body 
    HttpParams httpParams = postMethod.getParams();
    HttpProtocolParams.setUseExpectContinue(httpParams, false);

    HttpConnectionParams.setConnectionTimeout(httpParams, settings.getTimeoutInMilliseconds());
    HttpConnectionParams.setSoTimeout(httpParams, settings.getTimeoutInMilliseconds());

    SchemeRegistry registry = new SchemeRegistry();
    registry.register(new Scheme("http", new PlainSocketFactory(), 80));
    SocketFactory https_socket = settings.getSslTrustAll() ? new FakeSocketFactory()
            : settings.getSslTrustKey() != null ? new FakeSocketFactory(settings.getSslTrustKey())
                    : SSLSocketFactory.getSocketFactory();
    registry.register(new Scheme("https", https_socket, 443));

    client = new DefaultHttpClient(new ThreadSafeClientConnManager(httpParams, registry), httpParams);
    if (settings.shouldUseAuthentication()) {
        if (settings.getUsername() == null || settings.getPassword() == null) {
            throw new DaemonException(DaemonException.ExceptionType.AuthenticationFailure,
                    "No username or password set, while authentication was enabled.");
        } else {
            username = settings.getUsername();
            password = settings.getPassword();
            ((DefaultHttpClient) client).getCredentialsProvider()
                    .setCredentials(new AuthScope(postMethod.getURI().getHost(), postMethod.getURI().getPort(),
                            AuthScope.ANY_REALM), new UsernamePasswordCredentials(username, password));
        }
    }

    random = new Random();
    random.nextInt();
}

From source file:com.predic8.membrane.test.AssertUtils.java

private static DefaultHttpClient getAuthenticatingHttpClient(String host, int port, String user, String pass) {
    DefaultHttpClient hc = new DefaultHttpClient();
    HttpRequestInterceptor preemptiveAuth = new HttpRequestInterceptor() {
        public void process(final HttpRequest request, final HttpContext context)
                throws HttpException, IOException {
            AuthState authState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE);
            CredentialsProvider credsProvider = (CredentialsProvider) context
                    .getAttribute(ClientContext.CREDS_PROVIDER);
            HttpHost targetHost = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
            if (authState.getAuthScheme() == null) {
                AuthScope authScope = new AuthScope(targetHost.getHostName(), targetHost.getPort());
                Credentials creds = credsProvider.getCredentials(authScope);
                if (creds != null) {
                    authState.update(new BasicScheme(), creds);
                }/*from ww  w  .  j a  va2s  .  c o m*/
            }
        }
    };
    hc.addRequestInterceptor(preemptiveAuth, 0);
    Credentials defaultcreds = new UsernamePasswordCredentials(user, pass);
    BasicCredentialsProvider bcp = new BasicCredentialsProvider();
    bcp.setCredentials(new AuthScope(host, port, AuthScope.ANY_REALM), defaultcreds);
    hc.setCredentialsProvider(bcp);
    hc.setCookieStore(new BasicCookieStore());
    return hc;
}

From source file:com.lightbox.android.network.HttpHelper.java

/**
 * Set Http Basic Authentication. /* w  w  w .j  av  a  2s . c  o  m*/
 * @param hostname
 * @param username
 * @param password
 */
public void setBasicAuth(String hostname, String username, String password) {
    if (hostname != null && username != null && password != null) {
        mHttpClient.getCredentialsProvider().setCredentials(new AuthScope(hostname, 443, AuthScope.ANY_REALM),
                new UsernamePasswordCredentials(username, password));
        mHttpClient.getCredentialsProvider().setCredentials(new AuthScope(hostname, 80, AuthScope.ANY_REALM),
                new UsernamePasswordCredentials(username, password));
    }
}