如何将Play WS与SSL配合使用? [英] How to use Play WS with SSL?
问题描述
我的Java客户端应用程序需要执行REST调用.我被指示使用Play的WS实现.目前,我有这个:
My Java client application needs to do REST calls. I was instructed to use Play's WS implementation. Currently, I have this:
AsyncHttpClientConfig.Builder builder = new com.ning.http.client.AsyncHttpClientConfig.Builder();
NingWSClient wsc = new play.libs.ws.ning.NingWSClient(builder.build());
WSRequestHolder holder = wsc.url("http://www.simpleweb.org/");
这有效.但是,我的应用程序需要访问使用SSL的安全Web服务.我的客户有PKCS12证书.如何让WS使用此证书建立安全连接?
This works. However, my application needs to access a secure web service that uses SSL. I have a PKCS12 cert for my client. How can I get WS to use this certificate to establish a secure connection?
请注意,这不是Play应用程序.
To be clear, this isn't a Play application.
推荐答案
无法直接使用WS. 播放文档说:"WS不支持客户端证书(也称为双向TLS/MTLS /客户端身份验证).您应该直接在 AsyncHttpClientConfig 并设置适当的KeyStore和TrustStore."
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);
这篇关于如何将Play WS与SSL配合使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!