您好,登錄后才能下訂單哦!
本篇文章為大家展示了Java Socket如何使用加密協議傳輸對象,內容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。
前面幾篇博文提到了Socket中一些常用的用法,但是對于一些有安全要求的應用就需要加密傳輸的數據,此時就需要用到SSLSocket了。
還是一樣需要一個實現了java.io.Serializable接口的簡單Java對象:
package com.googlecode.garbagecan.test.socket.ssl; public class User implements java.io.Serializable { private static final long serialVersionUID = 1L; private String name; private String password; public User() { } public User(String name, String password) { this.name = name; this.password = password; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
SSL Server類,這里需要用到ServerSocketFactory類來創建SSLServerSocket類實例,然后在通過SSLServerSocket來獲取SSLSocket實例,這里考慮到面向對象中的面向接口編程的理念,所以代碼中并沒有出現SSLServerSocket和SSLSocket,而是用了他們的父類ServerSocket和Socket。在獲取到ServerSocket和Socket實例以后,剩下的代碼就和不使用加密方式一樣了。
package com.googlecode.garbagecan.test.socket.ssl; import java.io.BufferedInputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.net.ServerSocket; import java.net.Socket; import java.util.logging.Level; import java.util.logging.Logger; import javax.net.ServerSocketFactory; import javax.net.ssl.SSLServerSocketFactory; public class MyServer { private final static Logger logger = Logger.getLogger(MyServer.class.getName()); public static void main(String[] args) { try { ServerSocketFactory factory = SSLServerSocketFactory.getDefault(); ServerSocket server = factory.createServerSocket(10000); while (true) { Socket socket = server.accept(); invoke(socket); } } catch (Exception ex) { ex.printStackTrace(); } } private static void invoke(final Socket socket) throws IOException { new Thread(new Runnable() { public void run() { ObjectInputStream is = null; ObjectOutputStream os = null; try { is = new ObjectInputStream(new BufferedInputStream(socket.getInputStream())); os = new ObjectOutputStream(socket.getOutputStream()); Object obj = is.readObject(); User user = (User)obj; System.out.println("user: " + user.getName() + "/" + user.getPassword()); user.setName(user.getName() + "_new"); user.setPassword(user.getPassword() + "_new"); os.writeObject(user); os.flush(); } catch (IOException ex) { logger.log(Level.SEVERE, null, ex); } catch(ClassNotFoundException ex) { logger.log(Level.SEVERE, null, ex); } finally { try { is.close(); } catch(Exception ex) {} try { os.close(); } catch(Exception ex) {} try { socket.close(); } catch(Exception ex) {} } } }).start(); } }
SSL Client類和SSL Server類類似,只是將其中獲取Socket的方式有所變化,其余的代碼也和不使用加密方式一樣。
package com.googlecode.garbagecan.test.socket.ssl; import java.io.BufferedInputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.net.Socket; import java.util.logging.Level; import java.util.logging.Logger; import javax.net.SocketFactory; import javax.net.ssl.SSLSocketFactory; public class MyClient { private final static Logger logger = Logger.getLogger(MyClient.class.getName()); public static void main(String[] args) throws Exception { for (int i = 0; i < 100; i++) { Socket socket = null; ObjectOutputStream os = null; ObjectInputStream is = null; try { SocketFactory factory = SSLSocketFactory.getDefault(); socket = factory.createSocket("localhost", 10000); os = new ObjectOutputStream(socket.getOutputStream()); User user = new User("user_" + i, "password_" + i); os.writeObject(user); os.flush(); is = new ObjectInputStream(new BufferedInputStream(socket.getInputStream())); Object obj = is.readObject(); if (obj != null) { user = (User)obj; System.out.println("user: " + user.getName() + "/" + user.getPassword()); } } catch(IOException ex) { logger.log(Level.SEVERE, null, ex); } finally { try { is.close(); } catch(Exception ex) {} try { os.close(); } catch(Exception ex) {} try { socket.close(); } catch(Exception ex) {} } } } }
代碼寫完了,下面就需要產生keystore文件了,運行下面的命令
keytool -genkey -alias mysocket -keyalg RSA -keystore mysocket.jks
在提示輸入項中,密碼項自己給定,其它都不改直接回車,這里我使用的密碼是“mysocket”。
運行Server
java -Djavax.net.ssl.keyStore=mysocket.jks -Djavax.net.ssl.keyStorePassword=mysocket com.googlecode.garbagecan.test.socket.ssl.MyServer
運行Client
java -Djavax.net.ssl.trustStore=mysocket.jks -Djavax.net.ssl.trustStorePassword=mysocket com.googlecode.garbagecan.test.socket.ssl.MyClient
上述內容就是Java Socket如何使用加密協議傳輸對象,你們學到知識或技能了嗎?如果還想學到更多技能或者豐富自己的知識儲備,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。