Register certified "https" protocol for SOAP(Axis 2) request

Asked

Viewed 267 times

1

I created a service to register the "https" protocol and inject the certificate into it using the Socketfactory implementation, but it works well only one request at a time, but when using multi-threading does not work, It’s like you picked up the wrong certificate at the time of the request on the webservice. The webservice SOAP classes were generated by Axis 2 using the WSDL saved from 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 consult several clients at the same time on the webservice, but the certificates are different. In case you want to know, the webservices are from NF-e Brazil.

  • I will assume that you have isolated the connections. That being said, you need now to isolate the certificates and the access data according to the connections. One way to do this is by using Threadlocal.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.