CDelaney
CDelaney

Reputation: 1248

How do I use SSLContext with axis

I have a web app that calls a webservice that requires an SSL certificate. We have a class that implements X509KeyManager for getting the certificate, and I can build an instance of SSLContext with that key manager, but I'm having no luck using it.

I used wsdl2java to generate the classes for the requests using axis (1), unfortunately all the solutions I've found are for axis2.

Here are a couple solutions I've found for axis2 that I haven't been able to replicate with axis (1):

//solution 1
BindingProvider bindingProvider = (BindingProvider) service; 
bindingProvider.getRequestContext().put("com.sun.xml.internal.ws.transport.https.client.SSLSocketFactory", sslContext.getSocketFactory());

//solution 2
aStub._getServiceClient().getOptions().setProperty(HTTPConstants.CUSTOM_PROTOCOL‌​_HANDLER, new Protocol("https",(ProtocolSocketFactory)new SSLProtocolSocketFactory(sslCtx),443));

Upvotes: 3

Views: 5616

Answers (1)

CDelaney
CDelaney

Reputation: 1248

Here's how I was able to accomplish this:

I created a class that extends JSSESocketFactory, similar to SunJSEESocketFactory (org.apache.axis.components.net)

public class MySocketFactory extends JSSESocketFactory implements SecureSocketFactory {
...
    protected void initFactory() throws IOException {
...
            SSLContext context = getContext();
            this.sslFactory = context.getSocketFactory();
...
    }

    protected SSLContext getContext() throws Exception {
        MyKeyManager myKeyManager = new MyKeyManager();
        KeyManager[] km = new X509KeyManager[] { myKeyManager };
        SSLContext context = SSLContext.getInstance("SSLv3");
        context.init(km, null, null);
        return context;
    }
}

and then before making any calls to the web service requiring the certificate

AxisProperties.setProperty("axis.socketSecureFactory",
         MySocketFactory.class.getCanonicalName());

Upvotes: 3

Related Questions