17
17
import javax .net .ssl .SSLContext ;
18
18
import javax .net .ssl .TrustManager ;
19
19
import javax .net .ssl .X509TrustManager ;
20
+ import org .apache .commons .codec .binary .Base64 ;
20
21
import org .apache .http .HttpEntity ;
21
22
import org .apache .http .client .methods .CloseableHttpResponse ;
22
23
import org .apache .http .client .methods .HttpDelete ;
@@ -220,6 +221,18 @@ public RestClient setHeader(String key, String Value) {
220
221
return this ;
221
222
}
222
223
224
+ public RestClient authBasic (String user , String pass ) {
225
+
226
+ headers .put ("Authorization" , "Basic " + Base64 .encodeBase64String ((user + ":" + pass ).getBytes ()));
227
+ return this ;
228
+ }
229
+
230
+ public RestClient authKey (String key ) {
231
+
232
+ headers .put ("Authorization" , "Bearer " + key );
233
+ return this ;
234
+ }
235
+
223
236
public CloseableHttpClient getAllSSLClient () throws KeyStoreException , NoSuchAlgorithmException , KeyManagementException {
224
237
225
238
TrustManager [] trustAllCerts = new TrustManager []{new X509TrustManager () {
@@ -237,22 +250,21 @@ public void checkClientTrusted(java.security.cert.X509Certificate[] certs, Strin
237
250
public void checkServerTrusted (java .security .cert .X509Certificate [] certs , String authType ) {
238
251
}
239
252
}};
240
- if (ignoreSSLErrors ) {
241
- SSLContext context = SSLContext .getInstance ("SSL" );
242
- context .init (null , trustAllCerts , null );
243
- }
244
253
245
254
HttpClientBuilder builder = HttpClientBuilder .create ();
246
255
RegistryBuilder registryBuilder = RegistryBuilder .<ConnectionSocketFactory >create ();
256
+
257
+ SSLContext context = SSLContext .getInstance ("SSL" );
247
258
if (ignoreSSLErrors ) {
248
- SSLContext context = SSLContext .getInstance ("SSL" );
249
259
context .init (null , trustAllCerts , null );
250
- SSLConnectionSocketFactory sslConnectionFactory = new SSLConnectionSocketFactory (context , SSLConnectionSocketFactory .ALLOW_ALL_HOSTNAME_VERIFIER );
251
- builder .setSSLSocketFactory (sslConnectionFactory );
260
+ } else {
261
+ context .init (null , null , null );
262
+ }
263
+ SSLConnectionSocketFactory sslConnectionFactory = new SSLConnectionSocketFactory (context , SSLConnectionSocketFactory .ALLOW_ALL_HOSTNAME_VERIFIER );
264
+ builder .setSSLSocketFactory (sslConnectionFactory );
252
265
253
- registryBuilder .register ("https" , sslConnectionFactory );
266
+ registryBuilder .register ("https" , sslConnectionFactory );
254
267
255
- }
256
268
PlainConnectionSocketFactory plainConnectionSocketFactory = new PlainConnectionSocketFactory ();
257
269
registryBuilder .register ("http" , plainConnectionSocketFactory );
258
270
0 commit comments