Java WebService over SSL

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 {
 
   public void callEcho(String wsUrl, String msg) throws Exception {
     SSLContext sslCtx = SSLContext.getInstance( "SSL" );
     TrustManager[] trustManagers = new TrustManager[] {
       new X509TrustManager() {
         public X509Certificate[] getAcceptedIssuers() {
           return new X509Certificate[ 0 ];
         }
         public void checkClientTrusted(X509Certificate[] certs, String authType) throws CertificateException {
         }
         public void checkServerTrusted(X509Certificate[] certs, String authType) throws CertificateException {
         }
       }
     };
     KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
     KeyStore ks = KeyStore.getInstance( "JKS" );
     FileInputStream keyStoreIn = new FileInputStream( "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 = new EchoService(
         getClass().getResource( "echo.wsdl" ),
         new QName( "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 = new HostnameVerifier() {
       @Override
       public boolean verify(String hostname, SSLSession sslSession) {
         return true ;
       }
     };
     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);
   }
 
   public static void main(String[] args) throws Exception {
     new SSLWebClient().callEcho( "https://localhost:8081/ws/Echo" , "Hello" );
   }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值