|
| 1 | +import java.io.File; |
| 2 | +import java.io.FileInputStream; |
| 3 | +import java.io.IOException; |
| 4 | +import java.net.Socket; |
| 5 | +import java.nio.channels.ByteChannel; |
| 6 | +import java.nio.channels.SocketChannel; |
| 7 | +import java.security.KeyStore; |
| 8 | +import java.util.List; |
| 9 | + |
| 10 | +import javax.net.ssl.KeyManagerFactory; |
| 11 | +import javax.net.ssl.SSLContext; |
| 12 | +import javax.net.ssl.SSLEngine; |
| 13 | +import javax.net.ssl.TrustManagerFactory; |
| 14 | + |
| 15 | +import org.java_websocket.SSLSocketChannel; |
| 16 | +import org.java_websocket.WebSocket; |
| 17 | +import org.java_websocket.WebSocketAdapter; |
| 18 | +import org.java_websocket.WebSocketImpl; |
| 19 | +import org.java_websocket.drafts.Draft; |
| 20 | +import org.java_websocket.server.WebSocketServer; |
| 21 | + |
| 22 | +public class SSLExample { |
| 23 | + |
| 24 | + /* |
| 25 | + * Keystore with certificate created like so (in JKS format): |
| 26 | + * |
| 27 | + *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" |
| 28 | + */ |
| 29 | + public static void main( String[] args ) throws Exception { |
| 30 | + WebSocket.DEBUG = true; |
| 31 | + |
| 32 | + ChatServer chatserver = new ChatServer( 8887 ); |
| 33 | + |
| 34 | + // load up the key store |
| 35 | + String STORETYPE = "JKS"; |
| 36 | + String KEYSTORE = "keystore.jks"; |
| 37 | + String STOREPASSWORD = "storepassword"; |
| 38 | + String KEYPASSWORD = "keypassword"; |
| 39 | + |
| 40 | + KeyStore ks = KeyStore.getInstance( STORETYPE ); |
| 41 | + File kf = new File( KEYSTORE ); |
| 42 | + ks.load( new FileInputStream( kf ), STOREPASSWORD.toCharArray() ); |
| 43 | + |
| 44 | + KeyManagerFactory kmf = KeyManagerFactory.getInstance( "SunX509" ); |
| 45 | + kmf.init( ks, KEYPASSWORD.toCharArray() ); |
| 46 | + TrustManagerFactory tmf = TrustManagerFactory.getInstance( "SunX509" ); |
| 47 | + tmf.init( ks ); |
| 48 | + |
| 49 | + SSLContext sslContext = null; |
| 50 | + sslContext = SSLContext.getInstance( "TLS" ); |
| 51 | + sslContext.init( kmf.getKeyManagers(), tmf.getTrustManagers(), null ); |
| 52 | + |
| 53 | + chatserver.setWebSocketFactory( new SSLWebSocketServerFactory( sslContext ) ); |
| 54 | + |
| 55 | + chatserver.start(); |
| 56 | + |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +class SSLWebSocketServerFactory implements WebSocketServer.WebSocketServerFactory { |
| 61 | + private SSLContext sslcontext; |
| 62 | + SSLWebSocketServerFactory( SSLContext sslContext ) { |
| 63 | + this.sslcontext = sslContext; |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + public ByteChannel wrapChannel( SocketChannel c ) throws IOException { |
| 68 | + SSLEngine e = sslcontext.createSSLEngine(); |
| 69 | + e.setUseClientMode( false ); |
| 70 | + return new SSLSocketChannel( c, e ); |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + public WebSocketImpl createWebSocket( WebSocketAdapter a, Draft d, Socket c ) { |
| 75 | + return new WebSocketImpl( a, d, c ); |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + public WebSocketImpl createWebSocket( WebSocketAdapter a, List<Draft> d, Socket s ) { |
| 80 | + return new WebSocketImpl( a, d, s ); |
| 81 | + } |
| 82 | +} |
0 commit comments