|
| 1 | + ------ |
| 2 | + Async Http Client - Configuring SSL |
| 3 | + ------ |
| 4 | + Jeanfrancois Arcand |
| 5 | + ------ |
| 6 | + 2012 |
| 7 | + |
| 8 | +Configuring SSL |
| 9 | + |
| 10 | + Configuring the library to support SSL is simple. By default you don't have to configure anything if you don't need to use your own certificates etc. |
| 11 | + |
| 12 | ++-----+ |
| 13 | +AsyncHttpClient client = new AsyncHttpClient(); |
| 14 | +Response response = client.prepareGet(("https://sonatype.com").execute().get(); |
| 15 | ++-----+ |
| 16 | + |
| 17 | + The library will detect it's an SSL request and appropriately locate the key store, trust store etc. |
| 18 | + If you need to configure those objects, all you need to do is to create an <<<SSLContext>>> and set it using the |
| 19 | + <<<AsyncHttpClient>>>'s Builder as showed below: |
| 20 | + |
| 21 | ++-----+ |
| 22 | +InputStream keyStoreStream = .... |
| 23 | +char[] keyStorePassword = "changeit".toCharArray(); |
| 24 | +KeyStore ks = KeyStore.getInstance("JKS"); |
| 25 | +ks.load(keyStoreStream, keyStorePassword); |
| 26 | + |
| 27 | +char[] certificatePassword = "changeit".toCharArray(); |
| 28 | +KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509"); |
| 29 | +kmf.init(ks, certificatePassword); |
| 30 | + |
| 31 | +KeyManager[] keyManagers = kmf.getKeyManagers(); |
| 32 | +TrustManager[] trustManagers = new TrustManager[]{DUMMY_TRUST_MANAGER}; |
| 33 | +SecureRandom secureRandom = new SecureRandom(); |
| 34 | + |
| 35 | +SSLContext sslContext = SSLContext.getInstance("TLS"); |
| 36 | +sslContext.init(keyManagers, trustManagers, secureRandom); |
| 37 | +Builder builder = new AsyncHttpClientConfig.Builder(); |
| 38 | +builder.setSSLContext(myOwnThreadPool); |
| 39 | +AsyncHttpClient client = new AsyncHttpClient(builder.build()); |
| 40 | ++-----+ |
0 commit comments