Skip to content

Commit f29fe8f

Browse files
authored
Baeldung/java 5187 (eugenp#11431)
* Commit source code to branch * BAEL-5065 improvement of groupBy with complex key * Client and Server SSLSocket implementation to support https client authentication.
1 parent 5e4e1e4 commit f29fe8f

2 files changed

Lines changed: 87 additions & 0 deletions

File tree

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.baeldung.httpsclientauthentication;
2+
3+
import java.io.BufferedInputStream;
4+
import java.io.BufferedOutputStream;
5+
import java.io.IOException;
6+
import java.io.InputStream;
7+
import java.io.OutputStream;
8+
9+
import javax.net.SocketFactory;
10+
import javax.net.ssl.SSLSocket;
11+
import javax.net.ssl.SSLSocketFactory;
12+
13+
public class SSLScocketClient {
14+
15+
static void startClient(String host, int port) throws IOException {
16+
17+
SocketFactory factory = SSLSocketFactory.getDefault();
18+
19+
try (SSLSocket socket = (SSLSocket) factory.createSocket(host, port)) {
20+
socket.setEnabledCipherSuites(new String[] { "TLS_AES_128_GCM_SHA256" });
21+
socket.setEnabledProtocols(new String[] { "TLSv1.3" });
22+
InputStream is = new BufferedInputStream(socket.getInputStream());
23+
String message = "Hello World Message";
24+
System.out.println("sending message: " + message);
25+
OutputStream os = new BufferedOutputStream(socket.getOutputStream());
26+
os.write(message.getBytes());
27+
os.flush();
28+
byte[] data = new byte[2048];
29+
int len = is.read(data);
30+
if (len <= 0) {
31+
throw new IOException("no data received");
32+
}
33+
System.out.printf("client received %d bytes: %s%n", len, new String(data, 0, len));
34+
}
35+
}
36+
37+
public static void main(String[] args) throws IOException {
38+
39+
startClient("localhost", 8443);
40+
}
41+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.baeldung.httpsclientauthentication;
2+
3+
import java.io.BufferedInputStream;
4+
import java.io.BufferedOutputStream;
5+
import java.io.IOException;
6+
import java.io.InputStream;
7+
import java.io.OutputStream;
8+
import java.net.Socket;
9+
10+
import javax.net.ServerSocketFactory;
11+
import javax.net.ssl.SSLServerSocket;
12+
import javax.net.ssl.SSLServerSocketFactory;
13+
14+
public class SSLSocketEchoServer {
15+
16+
static void startServer(int port) throws IOException {
17+
18+
ServerSocketFactory factory = SSLServerSocketFactory.getDefault();
19+
20+
try (SSLServerSocket listener = (SSLServerSocket) factory.createServerSocket(port)) {
21+
listener.setNeedClientAuth(true);
22+
listener.setEnabledCipherSuites(new String[] { "TLS_AES_128_GCM_SHA256" });
23+
listener.setEnabledProtocols(new String[] { "TLSv1.3" });
24+
System.out.println("listening for messages...");
25+
try (Socket socket = listener.accept()) {
26+
InputStream is = new BufferedInputStream(socket.getInputStream());
27+
OutputStream os = new BufferedOutputStream(socket.getOutputStream());
28+
byte[] data = new byte[2048];
29+
int len = is.read(data);
30+
if (len <= 0) {
31+
throw new IOException("no data received");
32+
}
33+
String message = new String(data, 0, len);
34+
System.out.printf("server received %d bytes: %s%n", len, message);
35+
String response = message + " processed by server";
36+
os.write(response.getBytes(), 0, response.getBytes().length);
37+
os.flush();
38+
}
39+
System.out.println("message processed, exiting");
40+
}
41+
}
42+
43+
public static void main(String[] args) throws IOException {
44+
startServer(8443);
45+
}
46+
}

0 commit comments

Comments
 (0)