Play Framework: how can I use this certifcate to call HTTP WS.url?

I have a certificate from a REST service provider, and I need to tell this HTTP call to use it when accessing this provider. The code base I'm working with is used to use a third-party library to achieve this, but I want to use the timeout and subtleties of the WS.url call.

I looked at the API docs and docs on the website, and I just found a few hints, and these hints have not yet led me to any solutions.

+4
source share
1 answer

You should be able to approach it by adapting SSLContext AsyncHttpClient. The idea is to provide a client certificate to the base client. I assume you have pkcs12.

It seems that AsyncHttpClientConfig cannot be updated as it is automatically initialized. Therefore, a new customer must be created and used.

The following should send the request as indicated in the SSLContext containing the pkcs12 file.

KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory .getDefaultAlgorithm()); KeyStore clientCert = KeyStore.getInstance("pkcs12"); InputStream is = new FileInputStream("/conf/brazo.p12"); clientCert.load(is, "brazo".toCharArray()); kmf.init(clientCert, "brazo".toCharArray()); SSLContext sc = SSLContext.getInstance("TLSv1"); sc.init(kmf.getKeyManagers(), null, new SecureRandom()); AsyncHttpClientConfig config = new AsyncHttpClientConfig.Builder().setSSLContext(sc).build(); AsyncHttpClient client = new AsyncHttpClient(config); String url = "https://your-url"; Request req = new RequestBuilder().setUrl(url).setMethod("GET").build(); client.executeRequest(req); 

Unfortunately, this is moving away from Play! WS standard library.

+1
source

Source: https://habr.com/ru/post/1485853/


All Articles