How to use Play WS with SSL?

前端 未结 3 1681
无人及你
无人及你 2021-02-10 07:13

My Java client application needs to do REST calls. I was instructed to use Play\'s WS implementation. Currently, I have this:

AsyncHttpClientConfig.Builder build         


        
3条回答
  •  灰色年华
    2021-02-10 07:32

    Its not possible directly with WS. Play docs says : "WS does not support client certificates (aka mutual TLS / MTLS / client authentication). You should set the SSLContext directly in an instance of AsyncHttpClientConfig and set up the appropriate KeyStore and TrustStore."

    You could do something like this maybe:

    KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory
            .getDefaultAlgorithm());
    KeyStore keyStore = KeyStore.getInstance("pkcs12");
    InputStream inputStream = new FileInputStream("YOUR.p12");
    
    keyStore.load(inputStream, "Your password as char[]");
    keyManagerFactory.init(keyStore, "Your password as char[]");
    
    SSLContext sslContext = SSLContext.getInstance("TLSv1.2");
    sslContext.init(keyManagerFactory.getKeyManagers(), null,new SecureRandom());
    AsyncHttpClientConfig httpClientConfig = new AsyncHttpClientConfig.Builder().setSSLContext(sslContext).build();
    AsyncHttpClient httpClient = new AsyncHttpClient(httpClientConfig);
    

提交回复
热议问题