forked from ivanp81/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
eugenp
committed
Aug 4, 2013
1 parent
a7a0328
commit 2b9f237
Showing
7 changed files
with
124 additions
and
69 deletions.
There are no files selected for viewing
70 changes: 70 additions & 0 deletions
70
...t-digest-auth/src/main/java/org/baeldung/client/ClientPreemptiveDigestAuthentication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package org.baeldung.client; | ||
|
||
import org.apache.http.HttpEntity; | ||
import org.apache.http.HttpHost; | ||
import org.apache.http.HttpResponse; | ||
import org.apache.http.auth.AuthScope; | ||
import org.apache.http.auth.UsernamePasswordCredentials; | ||
import org.apache.http.client.AuthCache; | ||
import org.apache.http.client.methods.HttpGet; | ||
import org.apache.http.client.protocol.ClientContext; | ||
import org.apache.http.impl.auth.DigestScheme; | ||
import org.apache.http.impl.client.BasicAuthCache; | ||
import org.apache.http.impl.client.DefaultHttpClient; | ||
import org.apache.http.protocol.BasicHttpContext; | ||
import org.apache.http.util.EntityUtils; | ||
|
||
/** | ||
* An example of HttpClient can be customized to authenticate | ||
* preemptively using DIGEST scheme. | ||
* <b/> | ||
* Generally, preemptive authentication can be considered less | ||
* secure than a response to an authentication challenge | ||
* and therefore discouraged. | ||
*/ | ||
public class ClientPreemptiveDigestAuthentication { | ||
|
||
public static void main(final String[] args) throws Exception { | ||
final HttpHost targetHost = new HttpHost("localhost", 8080, "http"); | ||
|
||
final DefaultHttpClient httpclient = new DefaultHttpClient(); | ||
try { | ||
httpclient.getCredentialsProvider().setCredentials(new AuthScope(targetHost.getHostName(), targetHost.getPort()), new UsernamePasswordCredentials("user1", "user1Pass")); | ||
|
||
// Create AuthCache instance | ||
final AuthCache authCache = new BasicAuthCache(); | ||
// Generate DIGEST scheme object, initialize it and add it to the local auth cache | ||
final DigestScheme digestAuth = new DigestScheme(); | ||
// Suppose we already know the realm name | ||
digestAuth.overrideParamter("realm", "Custom Realm Name"); | ||
|
||
// digestAuth.overrideParamter("nonce", "whatever"); | ||
authCache.put(targetHost, digestAuth); | ||
|
||
// Add AuthCache to the execution context | ||
final BasicHttpContext localcontext = new BasicHttpContext(); | ||
localcontext.setAttribute(ClientContext.AUTH_CACHE, authCache); | ||
|
||
final HttpGet httpget = new HttpGet("http://localhost:8080/spring-security-rest-digest-auth/api/foos/1"); | ||
|
||
System.out.println("executing request: " + httpget.getRequestLine()); | ||
System.out.println("to target: " + targetHost); | ||
|
||
for (int i = 0; i < 3; i++) { | ||
final HttpResponse response = httpclient.execute(targetHost, httpget, localcontext); | ||
final 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(); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 0 additions & 58 deletions
58
spring-security-rest-digest-auth/src/main/java/org/baeldung/client/RestTemplateFactory.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters