package com.ssl.sslsocket.testSocket;
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManagerFactory;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.security.KeyStore;
/**
* @author zjhe
* @date 2019/8/12 9:54
*/
public class Client {
public static void main(String[] args)throws Exception {
// Socket socket=new Socket("localhost", 7777);//监听端口
String serverHost ="127.0.0.1"; //服务端地址
int serverPort =7777; //服务端监听端口
String clientPrivateKey ="E://cer//local//kclient.keystore"; //客户端私钥
String clientKeyPassword ="runaclient"; //客户端私钥密码
String trustKey ="E://cer//local//tclient.keystore"; //客户端信任证书列表,即服务端证书
String trustKeyPassword ="runashine"; //客户端信任证书密码
SSLContext ctx = SSLContext.getInstance("SSL");
KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
KeyStore ks = KeyStore.getInstance("JKS");
KeyStore tks = KeyStore.getInstance("JKS");
ks.load(new FileInputStream(clientPrivateKey), clientKeyPassword.toCharArray());
tks.load(new FileInputStream(trustKey), trustKeyPassword.toCharArray());
kmf.init(ks, clientKeyPassword.toCharArray());
tmf.init(tks);
ctx.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
Socket socket = (Socket) ctx.getSocketFactory().createSocket(serverHost, serverPort);
ObjectOutputStream out=new ObjectOutputStream(socket.getOutputStream());//包装对象流
out.writeObject(new User("小明", 21));//写入对象
socket.shutdownOutput();//关闭流的占用
//获取服务器发过来的消息
InputStream in = socket.getInputStream();
byte[] b=new byte[1024];
in.read(b);
System.out.println(new String(b,0,b.length));
//关闭流
in.close();
out.close();
socket.close();
}
}
package com.ssl.sslsocket.testSocket;
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLServerSocket;
import javax.net.ssl.TrustManagerFactory;
import java.io.FileInputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.security.KeyStore;
/**
* @author zjhe
* @date 2019/8/12 9:55
*/
public class Server {
public static void main(String[] args)throws Exception {
// ServerSocket socket=new ServerSocket(9999);//监听端口
//
// //循环监听接收各个客户端连接
// while (true) {
// Socket client = socket.accept();
//
// //开启多线程接受客户端信息
// ThreadServer ts=new ThreadServer(client);
// new Thread(ts).start();
// }
int port =7777;
String serverKeyStorePassword ="runashine";
String serverTrustKeyStorePassword ="runaclient";
SSLServerSocket serverSocket;
SSLContext ctx = SSLContext.getInstance("SSL");
KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
KeyStore ks = KeyStore.getInstance("JKS");
KeyStore tks = KeyStore.getInstance("JKS");
ks.load(new FileInputStream("E://cer//local//kserver.keystore"), serverKeyStorePassword.toCharArray());
tks.load(new FileInputStream("E://cer//local//tserver.keystore"), serverTrustKeyStorePassword.toCharArray());
kmf.init(ks, serverKeyStorePassword.toCharArray());
tmf.init(tks);
ctx.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
serverSocket = (SSLServerSocket) ctx.getServerSocketFactory().createServerSocket(port);
serverSocket.setNeedClientAuth(true);
//serverSocket.setSoTimeout(3000);
while (true) {
Socket client = serverSocket.accept();
//开启多线程接受客户端信息
ThreadServer ts=new ThreadServer(client);
new Thread(ts).start();
}
}
}