您可以使用類似這樣(注:這是一個補習學校項目,我沒有和它的不完整)
客戶
//load client private key
KeyStore clientKeys = KeyStore.getInstance("JKS");
clientKeys.load(new FileInputStream("proxyKeystore"),"password".toCharArray());
KeyManagerFactory clientKeyManager = KeyManagerFactory.getInstance("SunX509");
clientKeyManager.init(clientKeys,"password".toCharArray());
//load server public key
KeyStore serverPub = KeyStore.getInstance("JKS");
serverPub.load(new FileInputStream("proxyTrustedStore"),"password".toCharArray());
TrustManagerFactory trustManager = TrustManagerFactory.getInstance("SunX509");
trustManager.init(serverPub);
//use keys to create SSLSoket
SSLContext ssl = SSLContext.getInstance("TLS");
ssl.init(clientKeyManager.getKeyManagers(), trustManager.getTrustManagers(), SecureRandom.getInstance("SHA1PRNG"));
socket = (SSLSocket)ssl.getSocketFactory().createSocket("localhost", 8889);
socket.startHandshake();
服務器:
KeyStore serverKeys = KeyStore.getInstance("JKS");
serverKeys.load(new FileInputStream("authKeystore"),"password".toCharArray());
KeyManagerFactory serverKeyManager = KeyManagerFactory.getInstance("SunX509");
serverKeyManager.init(serverKeys,"password".toCharArray());
KeyStore clientPub = KeyStore.getInstance("JKS");
clientPub.load(new FileInputStream("authTrustedStore"),"password".toCharArray());
TrustManagerFactory trustManager = TrustManagerFactory.getInstance("SunX509");
trustManager.init(clientPub);
//use keys to create SSLSoket
SSLContext ssl = SSLContext.getInstance("TLS");
ssl.init(serverKeyManager.getKeyManagers(), trustManager.getTrustManagers(), SecureRandom.getInstance("SHA1PRNG"));
serverSock = (SSLServerSocket)ssl.getServerSocketFactory().createServerSocket(8889);
serverSock.setNeedClientAuth(true);
socket = (SSLSocket)serverSock.accept();
在您開始握手和/或接受serverSocket連接之前,您可以向服務器和/或客戶端請求證書。
總是有一臺服務器。連接的是客戶端,連接的是服務器。字節在互聯網中間不會神奇地相遇,並創建某種無服務器連接。你真正的問題是什麼? – Kayaman
@Kayaman請仔細閱讀此問題(此次完整) - 我正在尋找一些可以在純java中完成的工作,而不需要「服務器安裝」,如weblogic/glassfish/tomcat /等。 – user2813274
是的,我讀完了。你仍然有一個充當服務器的程序和一個充當客戶端的程序。你有沒有在客戶端服務器程序中使用過常規的'ServerSocket'? – Kayaman