|
| 1 | +package org.baeldung.client; |
| 2 | + |
| 3 | +import org.apache.http.HttpEntity; |
| 4 | +import org.apache.http.HttpHost; |
| 5 | +import org.apache.http.HttpResponse; |
| 6 | +import org.apache.http.auth.AuthScope; |
| 7 | +import org.apache.http.auth.UsernamePasswordCredentials; |
| 8 | +import org.apache.http.client.AuthCache; |
| 9 | +import org.apache.http.client.methods.HttpGet; |
| 10 | +import org.apache.http.client.protocol.ClientContext; |
| 11 | +import org.apache.http.impl.auth.DigestScheme; |
| 12 | +import org.apache.http.impl.client.BasicAuthCache; |
| 13 | +import org.apache.http.impl.client.DefaultHttpClient; |
| 14 | +import org.apache.http.protocol.BasicHttpContext; |
| 15 | +import org.apache.http.util.EntityUtils; |
| 16 | + |
| 17 | +/** |
| 18 | + * An example of HttpClient can be customized to authenticate |
| 19 | + * preemptively using DIGEST scheme. |
| 20 | + * <b/> |
| 21 | + * Generally, preemptive authentication can be considered less |
| 22 | + * secure than a response to an authentication challenge |
| 23 | + * and therefore discouraged. |
| 24 | + */ |
| 25 | +public class ClientPreemptiveDigestAuthentication { |
| 26 | + |
| 27 | + public static void main(final String[] args) throws Exception { |
| 28 | + final HttpHost targetHost = new HttpHost("localhost", 8080, "http"); |
| 29 | + |
| 30 | + final DefaultHttpClient httpclient = new DefaultHttpClient(); |
| 31 | + try { |
| 32 | + httpclient.getCredentialsProvider().setCredentials(new AuthScope(targetHost.getHostName(), targetHost.getPort()), new UsernamePasswordCredentials("user1", "user1Pass")); |
| 33 | + |
| 34 | + // Create AuthCache instance |
| 35 | + final AuthCache authCache = new BasicAuthCache(); |
| 36 | + // Generate DIGEST scheme object, initialize it and add it to the local auth cache |
| 37 | + final DigestScheme digestAuth = new DigestScheme(); |
| 38 | + // Suppose we already know the realm name |
| 39 | + digestAuth.overrideParamter("realm", "Custom Realm Name"); |
| 40 | + |
| 41 | + // digestAuth.overrideParamter("nonce", "whatever"); |
| 42 | + authCache.put(targetHost, digestAuth); |
| 43 | + |
| 44 | + // Add AuthCache to the execution context |
| 45 | + final BasicHttpContext localcontext = new BasicHttpContext(); |
| 46 | + localcontext.setAttribute(ClientContext.AUTH_CACHE, authCache); |
| 47 | + |
| 48 | + final HttpGet httpget = new HttpGet("http://localhost:8080/spring-security-rest-digest-auth/api/foos/1"); |
| 49 | + |
| 50 | + System.out.println("executing request: " + httpget.getRequestLine()); |
| 51 | + System.out.println("to target: " + targetHost); |
| 52 | + |
| 53 | + for (int i = 0; i < 3; i++) { |
| 54 | + final HttpResponse response = httpclient.execute(targetHost, httpget, localcontext); |
| 55 | + final HttpEntity entity = response.getEntity(); |
| 56 | + |
| 57 | + System.out.println("----------------------------------------"); |
| 58 | + System.out.println(response.getStatusLine()); |
| 59 | + if (entity != null) { |
| 60 | + System.out.println("Response content length: " + entity.getContentLength()); |
| 61 | + } |
| 62 | + EntityUtils.consume(entity); |
| 63 | + } |
| 64 | + } finally { |
| 65 | + // When HttpClient instance is no longer needed, shut down the connection manager to ensure immediate deallocation of all system resources |
| 66 | + httpclient.getConnectionManager().shutdown(); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | +} |
0 commit comments