|
| 1 | +package org.baeldung.httpclient; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | + |
| 5 | +import org.apache.http.Header; |
| 6 | +import org.apache.http.HttpHost; |
| 7 | +import org.apache.http.HttpResponse; |
| 8 | +import org.apache.http.auth.AuthScope; |
| 9 | +import org.apache.http.auth.AuthenticationException; |
| 10 | +import org.apache.http.auth.MalformedChallengeException; |
| 11 | +import org.apache.http.auth.UsernamePasswordCredentials; |
| 12 | +import org.apache.http.client.AuthCache; |
| 13 | +import org.apache.http.client.ClientProtocolException; |
| 14 | +import org.apache.http.client.CredentialsProvider; |
| 15 | +import org.apache.http.client.methods.CloseableHttpResponse; |
| 16 | +import org.apache.http.client.methods.HttpGet; |
| 17 | +import org.apache.http.client.protocol.HttpClientContext; |
| 18 | +import org.apache.http.impl.auth.DigestScheme; |
| 19 | +import org.apache.http.impl.client.BasicAuthCache; |
| 20 | +import org.apache.http.impl.client.BasicCredentialsProvider; |
| 21 | +import org.apache.http.impl.client.CloseableHttpClient; |
| 22 | +import org.apache.http.impl.client.HttpClients; |
| 23 | +import org.apache.http.util.EntityUtils; |
| 24 | +import org.junit.Test; |
| 25 | + |
| 26 | +public class SandboxTest { |
| 27 | + |
| 28 | + @Test |
| 29 | + public final void whenInterestingDigestAuthScenario_then200OK() throws AuthenticationException, ClientProtocolException, IOException, MalformedChallengeException { |
| 30 | + final HttpHost targetHost = new HttpHost("httpbin.org", 80, "http"); |
| 31 | + |
| 32 | + // set up the credentials to run agains the server |
| 33 | + final CredentialsProvider credsProvider = new BasicCredentialsProvider(); |
| 34 | + credsProvider.setCredentials(new AuthScope(targetHost.getHostName(), targetHost.getPort()), new UsernamePasswordCredentials("user", "passwd")); |
| 35 | + |
| 36 | + // We need a first run to get a 401 to seed the digest auth |
| 37 | + |
| 38 | + // Make a client using those creds |
| 39 | + final CloseableHttpClient client = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build(); |
| 40 | + |
| 41 | + // And make a call to the URL we are after |
| 42 | + final HttpGet httpget = new HttpGet("http://httpbin.org/digest-auth/auth/user/passwd"); |
| 43 | + |
| 44 | + // Create a context to use |
| 45 | + final HttpClientContext context = HttpClientContext.create(); |
| 46 | + |
| 47 | + // Get a response from the sever (expect a 401!) |
| 48 | + final HttpResponse authResponse = client.execute(targetHost, httpget, context); |
| 49 | + |
| 50 | + // Pull out the auth header that came back from the server |
| 51 | + final Header challenge = authResponse.getHeaders("WWW-Authenticate")[0]; |
| 52 | + |
| 53 | + // Lets use a digest scheme to solve it |
| 54 | + final DigestScheme digest = new DigestScheme(); |
| 55 | + digest.processChallenge(challenge); |
| 56 | + |
| 57 | + // Make a header with the solution based upon user/password and what the digest got out of the initial 401 reponse |
| 58 | + final Header solution = digest.authenticate(new UsernamePasswordCredentials("user", "passwd"), httpget, context); |
| 59 | + |
| 60 | + // Need an auth cache to use the new digest we made |
| 61 | + final AuthCache authCache = new BasicAuthCache(); |
| 62 | + authCache.put(targetHost, digest); |
| 63 | + |
| 64 | + // Add the authCache and thus solved digest to the context |
| 65 | + context.setAuthCache(authCache); |
| 66 | + |
| 67 | + // Pimp up our http get with the solved header made by the digest |
| 68 | + httpget.addHeader(solution); |
| 69 | + |
| 70 | + // use it! |
| 71 | + System.out.println("Executing request " + httpget.getRequestLine() + " to target " + targetHost); |
| 72 | + |
| 73 | + for (int i = 0; i < 3; i++) { |
| 74 | + final CloseableHttpResponse responseGood = client.execute(targetHost, httpget, context); |
| 75 | + |
| 76 | + try { |
| 77 | + System.out.println("----------------------------------------"); |
| 78 | + System.out.println(responseGood.getStatusLine()); |
| 79 | + System.out.println(EntityUtils.toString(responseGood.getEntity())); |
| 80 | + } finally { |
| 81 | + responseGood.close(); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + } |
| 86 | +} |
0 commit comments