動作環境
+ Windows10 + Tomcat-8.5.8 + JDK1.8 + Eclipse Neon
環境設定
* TomcatでのSSL通信の設定は以下の関連記事を参照のことhttp://blogs.yahoo.co.jp/dk521123/36513426.html
サンプル
* ひとまず、オレオレ証明書でも何でも気にせず通すような処理を実装してみる。
サーバ側
* 以下の関連記事を参照のことhttp://blogs.yahoo.co.jp/dk521123/36139336.html
クライアント側
import java.net.URL; import java.security.cert.X509Certificate; import javax.net.ssl.HostnameVerifier; import javax.net.ssl.HttpsURLConnection; import javax.net.ssl.SSLContext; import javax.net.ssl.SSLSession; import javax.net.ssl.TrustManager; import javax.net.ssl.X509TrustManager; public class Main { public static void main(String[] args) { System.out.println("Start!"); try { // ★SSL通信用の処理★ // Create a trust manager that does not validate certificate chains TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() { public X509Certificate[] getAcceptedIssuers() { System.out.println("getAcceptedIssuers"); return null; } public void checkClientTrusted(X509Certificate[] certs, String authType) { System.out.println("checkClientTrusted " + authType); } public void checkServerTrusted(X509Certificate[] certs, String authType) { System.out.println("checkServerTrusted " + authType); } } }; SSLContext sslContext = SSLContext.getInstance("TLS"); HostnameVerifier hostnameVerifier = new HostnameVerifier() { public boolean verify(String arg0, SSLSession arg1) { System.out.println("verify " + arg0); return true; } }; sslContext.init(null, trustAllCerts, new java.security.SecureRandom()); HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory()); HttpsURLConnection.setDefaultHostnameVerifier(hostnameVerifier); URL url = new URL("https://localhost:8443/SampleService/service/SampleWebService.ws?wsdl"); SampleWebServiceService service = new SampleWebServiceService(url); SampleWebService proxy = service.getSampleWebServicePort(); long start = System.currentTimeMillis(); String result = proxy.sayYourAge("Ken", 11L); long end = System.currentTimeMillis(); System.out.println((end - start) + "ms"); System.out.println("Result : " + result); } catch (Exception ex) { ex.printStackTrace(); } System.out.println("Done"); } }
実行結果
Start! checkServerTrusted ECDHE_RSA getAcceptedIssuers 33ms Result : Ken's age is 11. Done
参考文献
http://ws.apache.org/xmlrpc/ssl.htmlhttp://stackoverflow.com/questions/4722644/web-service-client-with-java-application-and-ssl