您好,登錄后才能下訂單哦!
本篇文章為大家展示了Java Socket如何使用加密協(xié)議傳輸對象,內(nèi)容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細(xì)介紹希望你能有所收獲。
前面幾篇博文提到了Socket中一些常用的用法,但是對于一些有安全要求的應(yīng)用就需要加密傳輸?shù)臄?shù)據(jù),此時就需要用到SSLSocket了。
還是一樣需要一個實現(xiàn)了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類來創(chuàng)建SSLServerSocket類實例,然后在通過SSLServerSocket來獲取SSLSocket實例,這里考慮到面向?qū)ο笾械拿嫦蚪涌诰幊痰睦砟?,所以代碼中并沒有出現(xiàn)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) {} } } } }
代碼寫完了,下面就需要產(chǎn)生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
上述內(nèi)容就是Java Socket如何使用加密協(xié)議傳輸對象,你們學(xué)到知識或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識儲備,歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。