Samples JDK
EchoServerTest.java
1 package com.freemindcafe.socket.ssl.sample2;
2 
3 import static com.freemindcafe.utils.FileSystemUtils.currentDir;
4 import javax.net.ssl.SSLServerSocket;
5 import javax.net.ssl.SSLServerSocketFactory;
6 import javax.net.ssl.SSLSocket;
7 
8 import org.junit.Test;
9 
10 import java.io.BufferedReader;
11 import java.io.InputStream;
12 import java.io.InputStreamReader;
13 
14 public class EchoServerTest {
15 
16  @Test
17  public void ssl_server_that_demands_client_auth() {
18  try {
19 
20  System.setProperty(
21  "javax.net.ssl.trustStore",currentDir()+"/src/com/freemindcafe/socket/ssl/sample2/serverkeystore.jks");
22  System.setProperty("javax.net.ssl.trustStorePassword", "password");
23  System.setProperty(
24  "javax.net.ssl.keyStore",
25  currentDir()+"/src/com/freemindcafe/socket/ssl/sample2/serverkeystore.jks");
26  System.setProperty("javax.net.ssl.keyStorePassword", "password");
27  System.setProperty("javax.net.debug", "ssl:handshake");
28 
29  SSLServerSocketFactory sslserversocketfactory = (SSLServerSocketFactory) SSLServerSocketFactory
30  .getDefault();
31  SSLServerSocket sslserversocket = (SSLServerSocket) sslserversocketfactory
32  .createServerSocket(9999);
33  //sslserversocket.setWantClientAuth(true);
34  sslserversocket.setNeedClientAuth(true);
35  SSLSocket sslsocket = (SSLSocket) sslserversocket.accept();
36 
37  InputStream inputstream = sslsocket.getInputStream();
38  InputStreamReader inputstreamreader = new InputStreamReader(
39  inputstream);
40  BufferedReader bufferedreader = new BufferedReader(
41  inputstreamreader);
42 
43  String string = null;
44  while ((string = bufferedreader.readLine()) != null) {
45  System.out.println("server printing ################");
46  System.out.println(string);
47  System.out.flush();
48  }
49  } catch (Exception exception) {
50  exception.printStackTrace();
51  }
52  }
53 }