|
| 1 | +import java.io.BufferedReader; |
| 2 | +import java.io.File; |
| 3 | +import java.io.FileInputStream; |
| 4 | +import java.io.InputStreamReader; |
| 5 | +import java.net.URI; |
| 6 | +import java.security.KeyStore; |
| 7 | + |
| 8 | +import javax.net.ssl.KeyManagerFactory; |
| 9 | +import javax.net.ssl.SSLContext; |
| 10 | +import javax.net.ssl.TrustManagerFactory; |
| 11 | + |
| 12 | +import org.java_websocket.WebSocket; |
| 13 | +import org.java_websocket.client.DefaultSSLWebSocketClientFactory; |
| 14 | +import org.java_websocket.client.WebSocketClient; |
| 15 | +import org.java_websocket.handshake.ServerHandshake; |
| 16 | + |
| 17 | +class WebSocketChatClient extends WebSocketClient { |
| 18 | + |
| 19 | + public WebSocketChatClient( URI serverUri ) { |
| 20 | + super( serverUri ); |
| 21 | + } |
| 22 | + |
| 23 | + @Override |
| 24 | + public void onOpen( ServerHandshake handshakedata ) { |
| 25 | + System.out.println( "Connected" ); |
| 26 | + |
| 27 | + } |
| 28 | + |
| 29 | + @Override |
| 30 | + public void onMessage( String message ) { |
| 31 | + System.out.println( "got: " + message ); |
| 32 | + |
| 33 | + } |
| 34 | + |
| 35 | + @Override |
| 36 | + public void onClose( int code, String reason, boolean remote ) { |
| 37 | + System.out.println( "Disconnected" ); |
| 38 | + System.exit( 0 ); |
| 39 | + |
| 40 | + } |
| 41 | + |
| 42 | + @Override |
| 43 | + public void onError( Exception ex ) { |
| 44 | + ex.printStackTrace(); |
| 45 | + |
| 46 | + } |
| 47 | + |
| 48 | +} |
| 49 | + |
| 50 | +public class SSLClientExample { |
| 51 | + |
| 52 | + /* |
| 53 | + * Keystore with certificate created like so (in JKS format): |
| 54 | + * |
| 55 | + *keytool -genkey -validity 3650 -keystore "keystore.jks" -storepass "storepassword" -keypass "keypassword" -alias "default" -dname "CN=127.0.0.1, OU=MyOrgUnit, O=MyOrg, L=MyCity, S=MyRegion, C=MyCountry" |
| 56 | + */ |
| 57 | + public static void main( String[] args ) throws Exception { |
| 58 | + WebSocket.DEBUG = true; |
| 59 | + |
| 60 | + WebSocketChatClient chatclient = new WebSocketChatClient( new URI( "wss://localhost:8887" ) ); |
| 61 | + |
| 62 | + // load up the key store |
| 63 | + String STORETYPE = "JKS"; |
| 64 | + String KEYSTORE = "keystore.jks"; |
| 65 | + String STOREPASSWORD = "storepassword"; |
| 66 | + String KEYPASSWORD = "keypassword"; |
| 67 | + |
| 68 | + KeyStore ks = KeyStore.getInstance( STORETYPE ); |
| 69 | + File kf = new File( KEYSTORE ); |
| 70 | + ks.load( new FileInputStream( kf ), STOREPASSWORD.toCharArray() ); |
| 71 | + |
| 72 | + KeyManagerFactory kmf = KeyManagerFactory.getInstance( "SunX509" ); |
| 73 | + kmf.init( ks, KEYPASSWORD.toCharArray() ); |
| 74 | + TrustManagerFactory tmf = TrustManagerFactory.getInstance( "SunX509" ); |
| 75 | + tmf.init( ks ); |
| 76 | + |
| 77 | + SSLContext sslContext = null; |
| 78 | + sslContext = SSLContext.getInstance( "TLS" ); |
| 79 | + sslContext.init( kmf.getKeyManagers(), tmf.getTrustManagers(), null ); |
| 80 | + |
| 81 | + chatclient.setWebSocketFactory( new DefaultSSLWebSocketClientFactory( sslContext ) ); |
| 82 | + |
| 83 | + chatclient.connect(); |
| 84 | + |
| 85 | + BufferedReader reader = new BufferedReader( new InputStreamReader( System.in ) ); |
| 86 | + while ( true ) { |
| 87 | + chatclient.send( reader.readLine() ); |
| 88 | + } |
| 89 | + |
| 90 | + } |
| 91 | +} |
0 commit comments