I created a service to register the "https" protocol and inject the certificate into it using the SocketFactory implementation, but only one request works fine at a time, but when using multi-threading it does not work, certificate at the time of the request in the webservice. The webservice SOAP classes were generated by Axis 2 using the saved WSDL of the page.
Code to register protocol and inject certificate (PFX):
public boolean assinar(InputStream pfx, final String password) throws Exception {
try {
InputStream in = new ByteArrayInputStream(getBytesFromInputStream(pfx));
KeyStore ks = KeyStore.getInstance("PKCS12");
ks.load(in, password.toCharArray());
in.close();
Enumeration<String> aliases = ks.aliases();
String alias = null;
while (aliases.hasMoreElements()) {
alias = (String) aliases.nextElement();
if (ks.isKeyEntry(alias)) break;
}
X509Certificate certificate = (X509Certificate) ks.getCertificate(alias);
PrivateKey privateKey = (PrivateKey) ks.getKey(alias, password.toCharArray());
SocketFactoryDynamic socketFactoryDinamico = new SocketFactoryDynamic(certificate, privateKey);
socketFactoryDinamico.setFileCacerts("NFeCacerts");
Protocol.registerProtocol("https", new Protocol("https", socketFactoryDinamico, 443));
return true;
} catch (IOException | NoSuchAlgorithmException | CertificateException | KeyStoreException | UnrecoverableKeyException e) {
throw e;
}
}
I want to query multiple clients at the same time in webservice, but the certificates are different. If you want to know, the webservices are from NF-e from Brazil.