Skip to content

Commit b72f319

Browse files
committed
httpclient digest examples
1 parent 22cebf3 commit b72f319

1 file changed

Lines changed: 62 additions & 2 deletions

File tree

‎httpclient/src/test/java/org/baeldung/httpclient/SandboxTest.java‎

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,18 @@
1111
import org.apache.http.auth.UsernamePasswordCredentials;
1212
import org.apache.http.client.AuthCache;
1313
import org.apache.http.client.ClientProtocolException;
14+
import org.apache.http.client.CookieStore;
1415
import org.apache.http.client.CredentialsProvider;
1516
import org.apache.http.client.methods.CloseableHttpResponse;
1617
import org.apache.http.client.methods.HttpGet;
1718
import org.apache.http.client.protocol.HttpClientContext;
1819
import org.apache.http.impl.auth.DigestScheme;
1920
import org.apache.http.impl.client.BasicAuthCache;
21+
import org.apache.http.impl.client.BasicCookieStore;
2022
import org.apache.http.impl.client.BasicCredentialsProvider;
2123
import org.apache.http.impl.client.CloseableHttpClient;
2224
import org.apache.http.impl.client.HttpClients;
25+
import org.apache.http.impl.cookie.BasicClientCookie;
2326
import org.apache.http.util.EntityUtils;
2427
import org.junit.Test;
2528

@@ -33,10 +36,17 @@ public final void whenInterestingDigestAuthScenario_then200OK() throws Authentic
3336
final CredentialsProvider credsProvider = new BasicCredentialsProvider();
3437
credsProvider.setCredentials(new AuthScope(targetHost.getHostName(), targetHost.getPort()), new UsernamePasswordCredentials("user", "passwd"));
3538

39+
// This endpoint need fake cookie to work properly
40+
final CookieStore cookieStore = new BasicCookieStore();
41+
final BasicClientCookie cookie = new BasicClientCookie("fake", "fake_value");
42+
cookie.setDomain("httpbin.org");
43+
cookie.setPath("/");
44+
cookieStore.addCookie(cookie);
45+
3646
// We need a first run to get a 401 to seed the digest auth
3747

3848
// Make a client using those creds
39-
final CloseableHttpClient client = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();
49+
final CloseableHttpClient client = HttpClients.custom().setDefaultCookieStore(cookieStore).setDefaultCredentialsProvider(credsProvider).build();
4050

4151
// And make a call to the URL we are after
4252
final HttpGet httpget = new HttpGet("http://httpbin.org/digest-auth/auth/user/passwd");
@@ -60,7 +70,7 @@ public final void whenInterestingDigestAuthScenario_then200OK() throws Authentic
6070
// Need an auth cache to use the new digest we made
6171
final AuthCache authCache = new BasicAuthCache();
6272
authCache.put(targetHost, digest);
63-
73+
6474
// Add the authCache and thus solved digest to the context
6575
context.setAuthCache(authCache);
6676

@@ -81,6 +91,56 @@ public final void whenInterestingDigestAuthScenario_then200OK() throws Authentic
8191
responseGood.close();
8292
}
8393
}
94+
client.close();
95+
}
8496

97+
// This test needs module spring-security-rest-digest-auth to be running
98+
@Test
99+
public final void whenWeKnowDigestParameters_thenNo401Status() throws AuthenticationException, ClientProtocolException, IOException, MalformedChallengeException {
100+
final HttpHost targetHost = new HttpHost("localhost", 8080, "http");
101+
102+
final CredentialsProvider credsProvider = new BasicCredentialsProvider();
103+
credsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("user1", "user1Pass"));
104+
105+
final CloseableHttpClient client = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();
106+
107+
final HttpGet httpget = new HttpGet("http://localhost:8080/spring-security-rest-digest-auth/api/foos/1");
108+
109+
final HttpClientContext context = HttpClientContext.create();
110+
// == make it preemptive
111+
final AuthCache authCache = new BasicAuthCache();
112+
final DigestScheme digestAuth = new DigestScheme();
113+
digestAuth.overrideParamter("realm", "Custom Realm Name");
114+
digestAuth.overrideParamter("nonce", "nonce value goes here");
115+
authCache.put(targetHost, digestAuth);
116+
context.setAuthCache(authCache);
117+
// == end
118+
System.out.println("Executing The Request knowing the digest parameters ==== ");
119+
final HttpResponse authResponse = client.execute(targetHost, httpget, context);
120+
System.out.println(authResponse.toString());
121+
client.close();
122+
}
123+
124+
// This test needs module spring-security-rest-digest-auth to be running
125+
@Test
126+
public final void whenDoNotKnowParameters_thenOnlyOne401() throws AuthenticationException, ClientProtocolException, IOException, MalformedChallengeException {
127+
final HttpClientContext context = HttpClientContext.create();
128+
final HttpHost targetHost = new HttpHost("localhost", 8080, "http");
129+
final CredentialsProvider credsProvider = new BasicCredentialsProvider();
130+
credsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("user1", "user1Pass"));
131+
final CloseableHttpClient client = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();
132+
133+
final HttpGet httpget = new HttpGet("http://localhost:8080/spring-security-rest-digest-auth/api/foos/1");
134+
System.out.println("Executing The Request NOT knowing the digest parameters ==== ");
135+
final HttpResponse tempResponse = client.execute(targetHost, httpget, context);
136+
System.out.println(tempResponse.toString());
137+
138+
for (int i = 0; i < 3; i++) {
139+
System.out.println("No more Challenges or 401");
140+
final CloseableHttpResponse authResponse = client.execute(targetHost, httpget, context);
141+
System.out.println(authResponse.toString());
142+
authResponse.close();
143+
}
144+
client.close();
85145
}
86146
}

0 commit comments

Comments
 (0)