Java Tutorial/Network/SSLServerSocket
Содержание
Creating an SSL Client Socket
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import javax.net.SocketFactory;
import javax.net.ssl.SSLSocketFactory;
public class Main {
public static void main(String[] argv) throws Exception {
int port = 443;
String hostname = "hostname";
SocketFactory socketFactory = SSLSocketFactory.getDefault();
Socket socket = socketFactory.createSocket(hostname, port);
InputStream in = socket.getInputStream();
OutputStream out = socket.getOutputStream();
// Read from in and write to out...
in.close();
out.close();
}
}
Creating an SSL Server Socket
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import javax.net.ServerSocketFactory;
import javax.net.ssl.SSLServerSocketFactory;
public class Main {
public static void main(String[] argv) throws Exception {
int port = 443;
ServerSocketFactory ssocketFactory = SSLServerSocketFactory.getDefault();
ServerSocket ssocket = ssocketFactory.createServerSocket(port);
Socket socket = ssocket.accept();
InputStream in = socket.getInputStream();
OutputStream out = socket.getOutputStream();
// Read from in and write to out...
in.close();
out.close();
}
}
Secure Communication with JSSE
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import javax.net.SocketFactory;
import javax.net.ssl.SSLSocketFactory;
public class Main {
public static void main(String args[]) throws Exception {
SocketFactory factory = SSLSocketFactory.getDefault();
Socket socket = factory.createSocket("127.0.0.1", 8080);
OutputStream outputStream = socket.getOutputStream();
PrintWriter out = new PrintWriter(outputStream);
out.print("GET / HTTP/1.0\r\n\r\n");
out.flush();
InputStream inputStream = socket.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader in = new BufferedReader(inputStreamReader);
String line;
while ((line = in.readLine()) != null) {
System.out.println(line);
}
out.close();
in.close();
socket.close();
}
}
SSL Server Demo
import javax.net.ssl.SSLServerSocket;
import javax.net.ssl.SSLServerSocketFactory;
import javax.net.ssl.SSLSocket;
public class Main {
public static void main(String[] argv) throws Exception {
SSLServerSocketFactory factory = (SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
SSLServerSocket serverSocket = (SSLServerSocket) factory.createServerSocket(8080);
String[] suites = serverSocket.getSupportedCipherSuites();
for (int i = 0; i < suites.length; i++) {
System.out.println(suites[i]);
}
serverSocket.setEnabledCipherSuites(suites);
String[] protocols = serverSocket.getSupportedProtocols();
for (int i = 0; i < protocols.length; i++) {
System.out.println(protocols[i]);
}
SSLSocket socket = (SSLSocket) serverSocket.accept();
socket.startHandshake();
System.out.println(socket.getRemoteSocketAddress());
}
}