JAVA + SSL Tutorial (server and client examples)
2011-01-17 19:21
519 查看
转载:http://stilius.net/java/java_ssl.php
JAVA + SSL Tutorial (server and client examples)
Certificate
First we need to make certificate, this is done by using keytool that is part of J2SE SDK (program will ask for certificate owner information and password, enter 123456 as password, or you can enter your password, but notice that you have to change it in other commands listen in this tutorial):
After this command you will have certificate file in working directory of issuing keytool command.
Compile it by using simple command:
Client source code (EchoClient.java)
[/code]
Compile it by using simple command:
Running server and client using SSL
First copy certificate file that you created before into working directory and run server with these parameters (notice that you have to change keyStore name and/or trustStrorePassword if you specified different options creating certificate:
And now again copy certificate file that you created before into working directory and run client with these parameters (notice that you have to change keyStore name and/or trustStrorePassword if you specified different options creating certificate:
If you want SSL debug information just add these parameters when running server and/or client:
Playing with server and client
Now just type any string on client console and press return. The same string has to appear on server console.
Copyright
This document is copyrighted to Tomas Vilda. You can use it in all ways, but don't change this section and allways include it.
JAVA + SSL Tutorial (server and client examples)
Certificate
First we need to make certificate, this is done by using keytool that is part of J2SE SDK (program will ask for certificate owner information and password, enter 123456 as password, or you can enter your password, but notice that you have to change it in other commands listen in this tutorial):
keytool -genkey -keystore mySrvKeystore -keyalg RSA
After this command you will have certificate file in working directory of issuing keytool command.
Server source code (EchoServer.java) import javax.net.ssl.SSLServerSocket; import javax.net.ssl.SSLServerSocketFactory; import javax.net.ssl.SSLSocket; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; public class EchoServer { public static void main(String[] arstring) { try { SSLServerSocketFactory sslserversocketfactory = (SSLServerSocketFactory) SSLServerSocketFactory.getDefault(); SSLServerSocket sslserversocket = (SSLServerSocket) sslserversocketfactory.createServerSocket(9999); SSLSocket sslsocket = (SSLSocket) sslserversocket.accept(); InputStream inputstream = sslsocket.getInputStream(); InputStreamReader inputstreamreader = new InputStreamReader(inputstream); BufferedReader bufferedreader = new BufferedReader(inputstreamreader); String string = null; while ((string = bufferedreader.readLine()) != null) { System.out.println(string); System.out.flush(); } } catch (Exception exception) { exception.printStackTrace(); } } }
Compile it by using simple command:
javac EchoServer.java
Client source code (EchoClient.java)
import javax.net.ssl.SSLSocket; import javax.net.ssl.SSLSocketFactory; import java.io.*; public class EchoClient { public static void main(String[] arstring) { try { SSLSocketFactory sslsocketfactory = (SSLSocketFactory) SSLSocketFactory.getDefault(); SSLSocket sslsocket = (SSLSocket) sslsocketfactory.createSocket("localhost", 9999); InputStream inputstream = System.in; InputStreamReader inputstreamreader = new InputStreamReader(inputstream); BufferedReader bufferedreader = new BufferedReader(inputstreamreader); OutputStream outputstream = sslsocket.getOutputStream(); OutputStreamWriter outputstreamwriter = new OutputStreamWriter(outputstream); BufferedWriter bufferedwriter = new BufferedWriter(outputstreamwriter); String string = null; while ((string = bufferedreader.readLine()) != null) { bufferedwriter.write(string + '\n'); bufferedwriter.flush(); } } catch (Exception exception) { exception.printStackTrace(); } } }
[/code]
Compile it by using simple command:
javac EchoClient.java
Running server and client using SSL
First copy certificate file that you created before into working directory and run server with these parameters (notice that you have to change keyStore name and/or trustStrorePassword if you specified different options creating certificate:
java -Djavax.net.ssl.keyStore=mySrvKeystore -Djavax.net.ssl.keyStorePassword=123456 EchoServer
And now again copy certificate file that you created before into working directory and run client with these parameters (notice that you have to change keyStore name and/or trustStrorePassword if you specified different options creating certificate:
java -Djavax.net.ssl.trustStore=mySrvKeystore -Djavax.net.ssl.trustStorePassword=123456 EchoClient
If you want SSL debug information just add these parameters when running server and/or client:
-Djava.protocol.handler.pkgs=com.sun.net.ssl.internal.www.protocol -Djavax.net.debug=ssl
Playing with server and client
Now just type any string on client console and press return. The same string has to appear on server console.
Copyright
This document is copyrighted to Tomas Vilda. You can use it in all ways, but don't change this section and allways include it.
相关文章推荐
- Simple TcpClient and TcpServer in Java
- Real differences between “java -server” and “java -client”-JVM Server和JVM Client的不同
- Real differences between “java -server” and “java -client”-JVM Server和JVM Client的不同
- java-string-tutorial-and-examples-beginners-programming
- Configure SSL server and client PSE
- Thrift tutorial(python server and php client)
- differences between “java -server” and “java -client”
- Java Exception Handling Tutorial with Examples and Best Practices
- SSL/TLS client/server for .NET and SSL tunnelling
- Simple OAuth Client and Server Examples
- Winsocket UDP Client and Server Examples
- SSL Server cert and client no cert
- java nio mina 学习笔记1 Simple Server and Client
- java wiki - apache httpserver and httpclient
- java -server 和 -client 的不同,及 java -server 时抛错原因
- 应用java多线程实现server端与多client之间的通信
- Java HotSpot Client VM 和 Java HotSpot Server VM的配置
- Java实现基础的client/server模型
- Python UDP Server and Client
- Does the RTSP implementation (client and/or server) support 'trick mode' operations