24
24
import org .apache .http .Header ;
25
25
import org .apache .http .HeaderElement ;
26
26
import org .apache .http .HttpEntity ;
27
+ import org .apache .http .HttpException ;
27
28
import org .apache .http .HttpHost ;
28
29
import org .apache .http .HttpRequest ;
29
30
import org .apache .http .HttpRequestInterceptor ;
30
31
import org .apache .http .HttpResponse ;
31
32
import org .apache .http .HttpResponseInterceptor ;
32
33
import org .apache .http .HttpVersion ;
33
34
import org .apache .http .auth .AuthScope ;
35
+ import org .apache .http .auth .AuthState ;
36
+ import org .apache .http .auth .Credentials ;
34
37
import org .apache .http .auth .UsernamePasswordCredentials ;
35
38
import org .apache .http .client .CookieStore ;
39
+ import org .apache .http .client .CredentialsProvider ;
36
40
import org .apache .http .client .HttpClient ;
37
41
import org .apache .http .client .methods .HttpDelete ;
38
42
import org .apache .http .client .methods .HttpEntityEnclosingRequestBase ;
50
54
import org .apache .http .conn .scheme .SchemeRegistry ;
51
55
import org .apache .http .conn .ssl .SSLSocketFactory ;
52
56
import org .apache .http .entity .HttpEntityWrapper ;
57
+ import org .apache .http .impl .auth .BasicScheme ;
53
58
import org .apache .http .impl .client .DefaultHttpClient ;
54
59
import org .apache .http .impl .client .DefaultRedirectHandler ;
55
60
import org .apache .http .impl .conn .tsccm .ThreadSafeClientConnManager ;
58
63
import org .apache .http .params .HttpParams ;
59
64
import org .apache .http .params .HttpProtocolParams ;
60
65
import org .apache .http .protocol .BasicHttpContext ;
66
+ import org .apache .http .protocol .ExecutionContext ;
61
67
import org .apache .http .protocol .HttpContext ;
62
68
import org .apache .http .protocol .SyncBasicHttpContext ;
63
69
@@ -256,6 +262,24 @@ public void process(HttpResponse response, HttpContext context) {
256
262
}
257
263
});
258
264
265
+ httpClient .addRequestInterceptor (new HttpRequestInterceptor () {
266
+ public void process (final HttpRequest request , final HttpContext context ) throws HttpException , IOException {
267
+ AuthState authState = (AuthState ) context .getAttribute (ClientContext .TARGET_AUTH_STATE );
268
+ CredentialsProvider credsProvider = (CredentialsProvider ) context .getAttribute (
269
+ ClientContext .CREDS_PROVIDER );
270
+ HttpHost targetHost = (HttpHost ) context .getAttribute (ExecutionContext .HTTP_TARGET_HOST );
271
+
272
+ if (authState .getAuthScheme () == null ) {
273
+ AuthScope authScope = new AuthScope (targetHost .getHostName (), targetHost .getPort ());
274
+ Credentials creds = credsProvider .getCredentials (authScope );
275
+ if (creds != null ) {
276
+ authState .setAuthScheme (new BasicScheme ());
277
+ authState .setCredentials (creds );
278
+ }
279
+ }
280
+ }
281
+ }, 0 );
282
+
259
283
httpClient .setHttpRequestRetryHandler (new RetryHandler (DEFAULT_MAX_RETRIES , DEFAULT_RETRY_SLEEP_TIME_MILLIS ));
260
284
}
261
285
@@ -414,7 +438,6 @@ public void setProxy(String hostname, int port, String username, String password
414
438
httpParams .setParameter (ConnRoutePNames .DEFAULT_PROXY , proxy );
415
439
}
416
440
417
-
418
441
/**
419
442
* Sets the SSLSocketFactory to user when making requests. By default, a new, default
420
443
* SSLSocketFactory is used.
@@ -462,8 +485,19 @@ public void removeHeader(String header) {
462
485
* @param password Basic Auth password
463
486
*/
464
487
public void setBasicAuth (String username , String password ) {
465
- AuthScope scope = AuthScope .ANY ;
466
- setBasicAuth (username , password , scope );
488
+ setBasicAuth (username , password , false );
489
+ }
490
+
491
+ /**
492
+ * Sets basic authentication for the request. Uses AuthScope.ANY. This is the same as
493
+ * setBasicAuth('username','password',AuthScope.ANY)
494
+ *
495
+ * @param username Basic Auth username
496
+ * @param password Basic Auth password
497
+ * @param preemtive sets authorization in preemtive manner
498
+ */
499
+ public void setBasicAuth (String username , String password , boolean preemtive ) {
500
+ setBasicAuth (username , password , null , preemtive );
467
501
}
468
502
469
503
/**
@@ -475,12 +509,40 @@ public void setBasicAuth(String username, String password) {
475
509
* @param scope - an AuthScope object
476
510
*/
477
511
public void setBasicAuth (String username , String password , AuthScope scope ) {
512
+ setBasicAuth (username , password , scope , false );
513
+ }
514
+
515
+ /**
516
+ * Sets basic authentication for the request. You should pass in your AuthScope for security. It
517
+ * should be like this setBasicAuth("username","password", new AuthScope("host",port,AuthScope.ANY_REALM))
518
+ *
519
+ * @param username Basic Auth username
520
+ * @param password Basic Auth password
521
+ * @param scope an AuthScope object
522
+ * @param preemtive sets authorization in preemtive manner
523
+ */
524
+ public void setBasicAuth (String username , String password , AuthScope scope , boolean preemtive ) {
478
525
UsernamePasswordCredentials credentials = new UsernamePasswordCredentials (username , password );
479
- this .httpClient .getCredentialsProvider ().setCredentials (scope , credentials );
526
+ this .httpClient .getCredentialsProvider ().setCredentials (scope == null ? AuthScope .ANY : scope , credentials );
527
+ setAuthenticationPreemptive (preemtive );
528
+ }
529
+
530
+ /**
531
+ * Sets HttpRequestInterceptor which handles authorization in preemtive way, as workaround you
532
+ * can use call `AsyncHttpClient.addHeader("Authorization","Basic base64OfUsernameAndPassword==")`
533
+ *
534
+ * @param isPreemtive whether the authorization is processed in preemtive way
535
+ */
536
+ public void setAuthenticationPreemptive (boolean isPreemtive ) {
537
+ if (isPreemtive ) {
538
+ httpClient .addRequestInterceptor (new PreemtiveAuthorizationHttpRequestInterceptor (), 0 );
539
+ } else {
540
+ httpClient .removeRequestInterceptorByClass (PreemtiveAuthorizationHttpRequestInterceptor .class );
541
+ }
480
542
}
481
543
482
544
/**
483
- * Removes set basic auth credentials
545
+ * Removes previously set basic auth credentials
484
546
*/
485
547
public void clearBasicAuth () {
486
548
this .httpClient .getCredentialsProvider ().clear ();
0 commit comments