Example usage for org.apache.http.impl.auth DigestScheme DigestScheme

List of usage examples for org.apache.http.impl.auth DigestScheme DigestScheme

Introduction

In this page you can find the example usage for org.apache.http.impl.auth DigestScheme DigestScheme.

Prototype

public DigestScheme() 

Source Link

Usage

From source file:com.dlmu.heipacker.crawler.client.ClientPreemptiveDigestAuthentication.java

public static void main(String[] args) throws Exception {

    HttpHost targetHost = new HttpHost("localhost", 80, "http");

    DefaultHttpClient httpclient = new DefaultHttpClient();
    try {/*from   w ww  .  jav a2  s.c  o m*/
        httpclient.getCredentialsProvider().setCredentials(
                new AuthScope(targetHost.getHostName(), targetHost.getPort()),
                new UsernamePasswordCredentials("username", "password"));

        // Create AuthCache instance
        AuthCache authCache = new BasicAuthCache();
        // Generate DIGEST scheme object, initialize it and add it to the local
        // auth cache
        DigestScheme digestAuth = new DigestScheme();
        // Suppose we already know the realm name
        digestAuth.overrideParamter("realm", "some realm");
        // Suppose we already know the expected nonce value
        digestAuth.overrideParamter("nonce", "whatever");
        authCache.put(targetHost, digestAuth);

        // Add AuthCache to the execution context
        BasicHttpContext localcontext = new BasicHttpContext();
        localcontext.setAttribute(ClientContext.AUTH_CACHE, authCache);

        HttpGet httpget = new HttpGet("/");

        System.out.println("executing request: " + httpget.getRequestLine());
        System.out.println("to target: " + targetHost);

        for (int i = 0; i < 3; i++) {
            HttpResponse response = httpclient.execute(targetHost, httpget, localcontext);
            HttpEntity entity = response.getEntity();

            System.out.println("----------------------------------------");
            System.out.println(response.getStatusLine());
            if (entity != null) {
                System.out.println("Response content length: " + entity.getContentLength());
            }
            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:demo.example.ClientPreemptiveDigestAuthentication.java

public static void main(String[] args) throws Exception {
    HttpHost target = new HttpHost("httpbin.org", 80, "http");
    CredentialsProvider credsProvider = new BasicCredentialsProvider();
    credsProvider.setCredentials(new AuthScope(target.getHostName(), target.getPort()),
            new UsernamePasswordCredentials("user", "passwd"));
    CloseableHttpClient httpclient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();
    try {/*from  w ww  .  j a v a2  s.  c  o m*/

        // Create AuthCache instance
        AuthCache authCache = new BasicAuthCache();
        // Generate DIGEST scheme object, initialize it and add it to the local
        // auth cache
        DigestScheme digestAuth = new DigestScheme();
        // Suppose we already know the realm name
        digestAuth.overrideParamter("realm", "some realm");
        // Suppose we already know the expected nonce value
        digestAuth.overrideParamter("nonce", "whatever");
        authCache.put(target, digestAuth);

        // Add AuthCache to the execution context
        HttpClientContext localContext = HttpClientContext.create();
        localContext.setAuthCache(authCache);

        HttpGet httpget = new HttpGet("http://httpbin.org/digest-auth/auth/user/passwd");

        System.out.println("Executing request " + httpget.getRequestLine() + " to target " + target);
        for (int i = 0; i < 3; i++) {
            CloseableHttpResponse response = httpclient.execute(target, httpget, localContext);
            try {
                System.out.println("----------------------------------------");
                System.out.println(response.getStatusLine());
                System.out.println(EntityUtils.toString(response.getEntity()));
            } finally {
                response.close();
            }
        }
    } finally {
        httpclient.close();
    }
}

From source file:com.lxf.spider.client.ClientPreemptiveDigestAuthentication.java

public static void main(String[] args) throws Exception {
    HttpHost target = new HttpHost("localhost", 80, "http");
    CredentialsProvider credsProvider = new BasicCredentialsProvider();
    credsProvider.setCredentials(new AuthScope(target.getHostName(), target.getPort()),
            new UsernamePasswordCredentials("username", "password"));
    CloseableHttpClient httpclient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();
    try {/*from www . j  a v  a  2 s  .  c  o  m*/

        // Create AuthCache instance
        AuthCache authCache = new BasicAuthCache();
        // Generate DIGEST scheme object, initialize it and add it to the local
        // auth cache
        DigestScheme digestAuth = new DigestScheme();
        // Suppose we already know the realm name
        digestAuth.overrideParamter("realm", "some realm");
        // Suppose we already know the expected nonce value
        digestAuth.overrideParamter("nonce", "whatever");
        authCache.put(target, digestAuth);

        // Add AuthCache to the execution context
        HttpClientContext localContext = HttpClientContext.create();
        localContext.setAuthCache(authCache);

        HttpGet httpget = new HttpGet("/");

        System.out.println("Executing request " + httpget.getRequestLine() + " to target " + target);
        for (int i = 0; i < 3; i++) {
            CloseableHttpResponse response = httpclient.execute(target, httpget, localContext);
            try {
                System.out.println("----------------------------------------");
                System.out.println(response.getStatusLine());
                EntityUtils.consume(response.getEntity());
            } finally {
                response.close();
            }
        }
    } finally {
        httpclient.close();
    }
}

From source file:httpclient.client.ClientPreemptiveDigestAuthentication.java

public static void main(String[] args) throws Exception {

    DefaultHttpClient httpclient = new DefaultHttpClient();

    httpclient.getCredentialsProvider().setCredentials(new AuthScope("localhost", 80),
            new UsernamePasswordCredentials("username", "password"));

    BasicHttpContext localcontext = new BasicHttpContext();
    // Generate DIGEST scheme object, initialize it and stick it to 
    // the local execution context
    DigestScheme digestAuth = new DigestScheme();
    // Suppose we already know the realm name
    digestAuth.overrideParamter("realm", "some realm");
    // Suppose we already know the expected nonce value 
    digestAuth.overrideParamter("nonce", "whatever");
    localcontext.setAttribute("preemptive-auth", digestAuth);

    // Add as the first request interceptor
    httpclient.addRequestInterceptor(new PreemptiveAuth(), 0);
    // Add as the last response interceptor
    httpclient.addResponseInterceptor(new PersistentDigest());

    HttpHost targetHost = new HttpHost("localhost", 80, "http");

    HttpGet httpget = new HttpGet("/");

    System.out.println("executing request: " + httpget.getRequestLine());
    System.out.println("to target: " + targetHost);

    for (int i = 0; i < 3; i++) {
        HttpResponse response = httpclient.execute(targetHost, httpget, localcontext);
        HttpEntity entity = response.getEntity();

        System.out.println("----------------------------------------");
        System.out.println(response.getStatusLine());
        if (entity != null) {
            System.out.println("Response content length: " + entity.getContentLength());
            entity.consumeContent();/* ww w .  j a  va  2s  . co m*/
        }
    }

    // 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:com.jonbanjo.cups.operations.AuthHeader.java

static void makeAuthHeader(HttpRequest request, AuthInfo auth) {

    if (auth.reason == AuthInfo.AUTH_NOT_SUPPORTED) {
        return;/*w w w  .  j av a 2  s  .  c o  m*/
    }

    if (auth.username.equals("") || (auth.password.equals(""))) {
        auth.reason = AuthInfo.AUTH_REQUIRED;
        return;
    }

    UsernamePasswordCredentials creds = new UsernamePasswordCredentials(auth.username, auth.password);
    if (auth.getType().equals("Basic")) {
        BasicScheme basicScheme = new BasicScheme();
        try {
            auth.setAuthHeader(basicScheme.authenticate(creds, request));
        } catch (Exception e) {
            System.err.println(e.toString());
            auth.reason = AuthInfo.AUTH_BAD;
        }
    } else if (auth.getType().equals("Digest")) {
        try {
            DigestScheme digestScheme = new DigestScheme();
            digestScheme.processChallenge(auth.getHttpHeader());
            auth.setAuthHeader(digestScheme.authenticate(creds, request));
            String test0 = auth.getHttpHeader().getValue();
            String test1 = auth.getAuthHeader().getValue();
            System.out.println();
        } catch (Exception e) {
            System.err.println(e.toString());
            auth.reason = AuthInfo.AUTH_BAD;
        }
    } else {
        auth.reason = AuthInfo.AUTH_NOT_SUPPORTED;
    }
}

From source file:com.liferay.mobile.android.auth.basic.DigestAuthentication.java

@Override
public Request authenticate(Proxy proxy, Response response) throws IOException {

    Request request = response.request();
    Builder builder = request.newBuilder();

    try {/* w  ww .jav a  2  s  .  c  o m*/
        BasicHeader authenticateHeader = new BasicHeader(Headers.WWW_AUTHENTICATE,
                response.header(Headers.WWW_AUTHENTICATE));

        DigestScheme scheme = new DigestScheme();
        scheme.processChallenge(authenticateHeader);

        BasicHttpRequest basicHttpRequest = new BasicHttpRequest(request.method(), request.uri().getPath());

        UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(username, password);

        String authorizationHeader = scheme.authenticate(credentials, basicHttpRequest).getValue();

        builder.addHeader(Headers.AUTHORIZATION, authorizationHeader);
    } catch (Exception e) {
        throw new IOException(e);
    }

    return builder.build();
}

From source file:com.liferay.mobile.sdk.auth.DigestAuthentication.java

@Override
public Request authenticate(Proxy proxy, Response response) throws IOException {

    Request request = response.request();

    Builder builder = request.newBuilder();

    try {//w  w  w  . java 2s .c om
        BasicHeader authenticateHeader = new BasicHeader(Headers.WWW_AUTHENTICATE,
                response.header(Headers.WWW_AUTHENTICATE));

        DigestScheme scheme = new DigestScheme();

        scheme.processChallenge(authenticateHeader);

        BasicHttpRequest basicHttpRequest = new BasicHttpRequest(request.method(), request.uri().getPath());

        UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(username, password);

        String authorizationHeader = scheme.authenticate(credentials, basicHttpRequest).getValue();

        builder.addHeader(Headers.AUTHORIZATION, authorizationHeader);
    } catch (Exception e) {
        throw new IOException(e);
    }

    return builder.build();
}

From source file:org.geosdi.geoplatform.connector.server.security.DigestPreemptiveSecurityConnector.java

@Override
protected AuthSchemeBase createScheme() {
    return new DigestScheme();
}

From source file:org.codelibs.fess.es.config.exentity.WebAuthentication.java

private AuthScheme getAuthScheme() {
    final String scheme = getProtocolScheme();
    if (Constants.BASIC.equals(scheme)) {
        return new BasicScheme();
    } else if (Constants.DIGEST.equals(scheme)) {
        return new DigestScheme();
    } else if (Constants.NTLM.equals(scheme)) {
        final Properties props = new Properties();
        getWebConfig().getConfigParameterMap(ConfigName.CONFIG).entrySet().stream()
                .filter(e -> e.getKey().startsWith("jcifs.")).forEach(e -> {
                    props.setProperty(e.getKey(), e.getValue());
                });/*from   ww w  .j a v  a  2  s.c om*/
        return new NTLMScheme(new JcifsEngine(props));
    } else if (Constants.FORM.equals(scheme)) {
        final Map<String, String> parameterMap = ParameterUtil.parse(getParameters());
        return new FormScheme(parameterMap);
    }
    return null;
}

From source file:securitydigest.TestDigestScheme.java

public void testDigestAuthenticationWithNoRealm() throws Exception {
    Header authChallenge = new BasicHeader(AUTH.WWW_AUTH, "Digest");
    try {// w  w w .ja  v a  2 s .c o m
        AuthScheme authscheme = new DigestScheme();
        authscheme.processChallenge(authChallenge);
        fail("Should have thrown MalformedChallengeException");
    } catch (MalformedChallengeException e) {
        // expected
    }
}