From 7af6badf6302fa8b94d3d10eb1c1c63a9a607cc9 Mon Sep 17 00:00:00 2001 From: mbarriola <85458535+mbarriola@users.noreply.github.com> Date: Wed, 10 Nov 2021 03:27:28 -0500 Subject: [PATCH] Baeldung/java 5187 (#11431) * Commit source code to branch * BAEL-5065 improvement of groupBy with complex key * Client and Server SSLSocket implementation to support https client authentication. --- .../SSLScocketClient.java | 41 +++++++++++++++++ .../SSLSocketEchoServer.java | 46 +++++++++++++++++++ 2 files changed, 87 insertions(+) create mode 100644 core-java-modules/core-java-11-2/src/main/java/com/baeldung/httpsclientauthentication/SSLScocketClient.java create mode 100644 core-java-modules/core-java-11-2/src/main/java/com/baeldung/httpsclientauthentication/SSLSocketEchoServer.java diff --git a/core-java-modules/core-java-11-2/src/main/java/com/baeldung/httpsclientauthentication/SSLScocketClient.java b/core-java-modules/core-java-11-2/src/main/java/com/baeldung/httpsclientauthentication/SSLScocketClient.java new file mode 100644 index 0000000000..286ecfc9df --- /dev/null +++ b/core-java-modules/core-java-11-2/src/main/java/com/baeldung/httpsclientauthentication/SSLScocketClient.java @@ -0,0 +1,41 @@ +package com.baeldung.httpsclientauthentication; + +import java.io.BufferedInputStream; +import java.io.BufferedOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; + +import javax.net.SocketFactory; +import javax.net.ssl.SSLSocket; +import javax.net.ssl.SSLSocketFactory; + +public class SSLScocketClient { + + static void startClient(String host, int port) throws IOException { + + SocketFactory factory = SSLSocketFactory.getDefault(); + + try (SSLSocket socket = (SSLSocket) factory.createSocket(host, port)) { + socket.setEnabledCipherSuites(new String[] { "TLS_AES_128_GCM_SHA256" }); + socket.setEnabledProtocols(new String[] { "TLSv1.3" }); + InputStream is = new BufferedInputStream(socket.getInputStream()); + String message = "Hello World Message"; + System.out.println("sending message: " + message); + OutputStream os = new BufferedOutputStream(socket.getOutputStream()); + os.write(message.getBytes()); + os.flush(); + byte[] data = new byte[2048]; + int len = is.read(data); + if (len <= 0) { + throw new IOException("no data received"); + } + System.out.printf("client received %d bytes: %s%n", len, new String(data, 0, len)); + } + } + + public static void main(String[] args) throws IOException { + + startClient("localhost", 8443); + } +} diff --git a/core-java-modules/core-java-11-2/src/main/java/com/baeldung/httpsclientauthentication/SSLSocketEchoServer.java b/core-java-modules/core-java-11-2/src/main/java/com/baeldung/httpsclientauthentication/SSLSocketEchoServer.java new file mode 100644 index 0000000000..66efa18fe9 --- /dev/null +++ b/core-java-modules/core-java-11-2/src/main/java/com/baeldung/httpsclientauthentication/SSLSocketEchoServer.java @@ -0,0 +1,46 @@ +package com.baeldung.httpsclientauthentication; + +import java.io.BufferedInputStream; +import java.io.BufferedOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.net.Socket; + +import javax.net.ServerSocketFactory; +import javax.net.ssl.SSLServerSocket; +import javax.net.ssl.SSLServerSocketFactory; + +public class SSLSocketEchoServer { + + static void startServer(int port) throws IOException { + + ServerSocketFactory factory = SSLServerSocketFactory.getDefault(); + + try (SSLServerSocket listener = (SSLServerSocket) factory.createServerSocket(port)) { + listener.setNeedClientAuth(true); + listener.setEnabledCipherSuites(new String[] { "TLS_AES_128_GCM_SHA256" }); + listener.setEnabledProtocols(new String[] { "TLSv1.3" }); + System.out.println("listening for messages..."); + try (Socket socket = listener.accept()) { + InputStream is = new BufferedInputStream(socket.getInputStream()); + OutputStream os = new BufferedOutputStream(socket.getOutputStream()); + byte[] data = new byte[2048]; + int len = is.read(data); + if (len <= 0) { + throw new IOException("no data received"); + } + String message = new String(data, 0, len); + System.out.printf("server received %d bytes: %s%n", len, message); + String response = message + " processed by server"; + os.write(response.getBytes(), 0, response.getBytes().length); + os.flush(); + } + System.out.println("message processed, exiting"); + } + } + + public static void main(String[] args) throws IOException { + startServer(8443); + } +} \ No newline at end of file