1 6 package org.jfox.ioc.connector.jrmps; 7 8 import java.io.IOException ; 9 import java.io.Serializable ; 10 import java.net.Socket ; 11 import java.rmi.server.RMIClientSocketFactory ; 12 import java.security.KeyStore ; 13 import javax.net.ssl.SSLContext; 14 import javax.net.ssl.TrustManagerFactory; 15 16 17 20 21 public class JRMPSClientSocketFactory implements RMIClientSocketFactory , Serializable { 22 23 private String SSLTrustKeyFile = "jfox.truststore"; 24 private String password = "jfox.org"; 25 26 27 public JRMPSClientSocketFactory(String SSLKeyFile, String password) { 28 this.SSLTrustKeyFile = SSLKeyFile; 29 this.password = password; 30 } 31 32 public synchronized Socket createSocket(String host, int port) throws IOException { 33 try { 34 KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType()); 35 keyStore.load(Thread.currentThread().getContextClassLoader().getResourceAsStream(SSLTrustKeyFile), password.toCharArray()); 36 37 40 TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); 41 tmf.init(keyStore); 42 43 SSLContext sslCtx = SSLContext.getInstance("TLS"); 44 sslCtx.init(null, tmf.getTrustManagers(), null); 45 return sslCtx.getSocketFactory().createSocket(host, port); 46 } 47 catch(Exception e) { 48 e.printStackTrace(); 49 return null; 50 } 51 } 52 53 public static void main(String [] args) { 54 55 } 56 } 57 58 | Popular Tags |