Skip to content

Commit e52a4d0

Browse files
kcacademicecheyne
authored andcommitted
Handshake failures (eugenp#5624)
* BAEL-2250: Adding files for the article on SSL handshake failure. * BAEL-2250 cleanup formatting * Applied review feedback on the article. * Adding cipher suite and protocol selection in server and client * Corrected some code conventions. * Revert: BAEL-2250 cleanup formatting * Made further changes for the review comments on the tutorial. * Fixed some formatting issues.
1 parent 433bb89 commit e52a4d0

2 files changed

Lines changed: 13 additions & 9 deletions

File tree

‎core-java/src/main/java/com/baeldung/ssl/example/SimpleClient.java‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,26 @@
88
import javax.net.SocketFactory;
99
import javax.net.ssl.SSLSocket;
1010
import javax.net.ssl.SSLSocketFactory;
11+
import javax.net.ssl.SSLParameters;
1112

1213
public class SimpleClient {
13-
static void startClient(String host, int port) throws IOException {
14+
static String startClient(String host, int port) throws IOException {
1415
SocketFactory factory = SSLSocketFactory.getDefault();
16+
1517
try (Socket connection = factory.createSocket(host, port)) {
1618
((SSLSocket) connection).setEnabledCipherSuites(
1719
new String[] { "TLS_DHE_DSS_WITH_AES_256_CBC_SHA256"});
1820
((SSLSocket) connection).setEnabledProtocols(
1921
new String[] { "TLSv1.2"});
20-
BufferedReader input = new BufferedReader(
21-
new InputStreamReader(connection.getInputStream()));
22-
System.out.println(input.readLine());
22+
SSLParameters sslParams = new SSLParameters();
23+
sslParams.setEndpointIdentificationAlgorithm("HTTPS");
24+
((SSLSocket) connection).setSSLParameters(sslParams);
25+
BufferedReader input = new BufferedReader(new InputStreamReader(connection.getInputStream()));
26+
return input.readLine();
2327
}
2428
}
2529

2630
public static void main(String[] args) throws IOException {
27-
startClient("localhost", 1234);
31+
System.out.println(startClient("localhost", 8443));
2832
}
2933
}

‎core-java/src/main/java/com/baeldung/ssl/example/SimpleServer.java‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import java.io.PrintWriter;
55
import java.net.ServerSocket;
66
import java.net.Socket;
7-
import java.util.Date;
87

98
import javax.net.ServerSocketFactory;
109
import javax.net.ssl.SSLServerSocket;
@@ -13,6 +12,7 @@
1312
public class SimpleServer {
1413
static void startServer(int port) throws IOException {
1514
ServerSocketFactory factory = SSLServerSocketFactory.getDefault();
15+
1616
try (ServerSocket listener = factory.createServerSocket(port)) {
1717
((SSLServerSocket) listener).setNeedClientAuth(true);
1818
((SSLServerSocket) listener).setEnabledCipherSuites(
@@ -22,13 +22,13 @@ static void startServer(int port) throws IOException {
2222
while (true) {
2323
try (Socket socket = listener.accept()) {
2424
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
25-
out.println(new Date().toString());
25+
out.println("Hello World!");
2626
}
2727
}
2828
}
2929
}
3030

3131
public static void main(String[] args) throws IOException {
32-
startServer(1234);
32+
startServer(8443);
3333
}
34-
}
34+
}

0 commit comments

Comments
 (0)