Java WebService over SSL

来源:互联网 发布:数据库爆破 编辑:程序博客网 时间:2024/06/17 19:14

I thought that customizing web service on https server (packed with JRE) and its client should be easy. But the lack of documentation make it quiet hard. Here are results of my findings. The topics covered are: Two-way SSL communication, custom SSLContext for webservice running on Java SDK embedded https server, dummy TrustManager, client certificate authentication. I’ll cover both the server and client side.

Suppose, you have created 2 certifikate keystore files, let’s say server.jks (containing server private key and client public certificate) andclient.jks (symmetrically, the client private key and server public certificate).

The server side code follows, consider it as code for reference.

Obtain wsdl and generate client classes, from commandline:

wget -O echo.wsdl http://localhost:8082/ws?wsdl
wsimport -Xnocompile http://localhost:8082/ws?wsdl

And finally write a client:

package com.example.echo;
 
import java.io.FileInputStream;
import java.security.KeyStore;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.Map;
 
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import javax.xml.namespace.QName;
import javax.xml.ws.BindingProvider;
 
public class SSLWebClient {
 
  publicvoidcallEcho(String wsUrl, String msg)throwsException {
    SSLContext sslCtx = SSLContext.getInstance("SSL");
    TrustManager[] trustManagers =newTrustManager[] {
      newX509TrustManager() {
        publicX509Certificate[] getAcceptedIssuers() {
          returnnewX509Certificate[0];
        }
        publicvoidcheckClientTrusted(X509Certificate[] certs, String authType)throwsCertificateException {
        }
        publicvoidcheckServerTrusted(X509Certificate[] certs, String authType)throwsCertificateException {
        }
      }
    };
    KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
    KeyStore ks = KeyStore.getInstance("JKS");
    FileInputStream keyStoreIn =newFileInputStream("client.jks");
    try{
      ks.load(keyStoreIn,"passphrase".toCharArray());
    }finally{
      keyStoreIn.close();
    }
    kmf.init(ks,"passphrase".toCharArray());
    sslCtx.init(kmf.getKeyManagers(), trustManagers,null);
     
    // This is a key point: We must not initialize service with target URL, because then
    // the WSDL would be downloaded from the HTTPS server, before we initialize our
    // context properties. So we initialize the EchoService with previously downloaded WSDL
    // and then use it to call the real service through HTTPS.
    EchoService service =newEchoService(
        getClass().getResource("echo.wsdl"),
        newQName("http://www.example.com/Echo","EchoService"));
    Echo port = service.getEchoPort();
 
    Map<String, Object> ctxt = ((BindingProvider)port).getRequestContext();
    ctxt.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, wsUrl);
     
    // Sets dummy hostname verifier, so that server certificate CN doesn't
    // have to match its hostname
    HostnameVerifier hnv =newHostnameVerifier() {
      @Override
      publicbooleanverify(String hostname, SSLSession sslSession) {
        returntrue;
      }
    };
    ctxt.put("com.sun.xml.internal.ws.transport.https.client.hostname.verifier", hnv);
    ctxt.put("com.sun.xml.ws.transport.https.client.hostname.verifier", hnv);
     
    // Sets socket factory from our ssl context,
    // which has dummy Trust manager. If used, server certificate doesn't need to be
    // in our trust keystore nor in Java "cacerts" keystore.
    ctxt.put("com.sun.xml.internal.ws.transport.https.client.SSLSocketFactory", sslCtx.getSocketFactory());
    ctxt.put("com.sun.xml.ws.transport.https.client.SSLSocketFactory", sslCtx.getSocketFactory());
     
    String response = port.echo(msg);
    System.out.println("Response: "+ response);
  }
 
  publicstaticvoidmain(String[] args)throwsException {
    newSSLWebClient().callEcho("https://localhost:8081/ws/Echo","Hello");
  }
}
0 0
原创粉丝点击