免费视频淫片aa毛片_日韩高清在线亚洲专区vr_日韩大片免费观看视频播放_亚洲欧美国产精品完整版

打開APP
userphoto
未登錄

開通VIP,暢享免費(fèi)電子書等14項(xiàng)超值服

開通VIP
在mina中實(shí)現(xiàn)TSL/SSL雙向認(rèn)證連接(1)
本文需要讀者對(duì)mina和SSl原理有一定的了解,所以本文中對(duì)mina和SSL的原理,不做詳細(xì)的介紹。

TSL/SSL雙向認(rèn)證連接:Server端和Client端通信,需要進(jìn)行授權(quán)和身份的驗(yàn)證,即Client只能接受Server的消息,Server只能接受Client的消息。這樣就可以在客戶機(jī)和服務(wù)器之間通過TCP/IP協(xié)議安全地傳輸數(shù)據(jù)。

在mina中實(shí)現(xiàn)TSL/SSL雙向認(rèn)證連接,本人目前所知有三種方式:
1.Server端和Client端各自擁有自簽名的私有密鑰證書,重寫的 javax.net.ssl.X509TrustManager接口中的三個(gè)方法實(shí)現(xiàn)Server端和Client端信認(rèn)證書。
2.Server端和Client端各自擁有自簽名的私有密鑰證書,并且互相交換公鑰,通過對(duì)方公鑰互相信認(rèn)對(duì)方證書。
3.Server端和Client端各自擁有可信認(rèn)的第三方認(rèn)證機(jī)構(gòu)(CA)簽名私有密鑰證書,通過CA互相信認(rèn)對(duì)方證書。
以上三種方式,實(shí)現(xiàn)復(fù)雜度從低到高,靈活度安全性也從低到高。本系列文章將將會(huì)從簡到難分別介紹三種方式的TSL/SSL雙向認(rèn)證連接。

下面我們介紹第一種實(shí)現(xiàn)方式:Server端和Client端各自擁有自簽名的私有密鑰證書,重寫的 javax.net.ssl.X509TrustManager接口中的三個(gè)方法實(shí)現(xiàn)Server端和Client端信認(rèn)證書。

首先,創(chuàng)建Server端和Client端各自的私有密鑰證書,在這里使用keytool,關(guān)于keytool的使用在這里不作詳細(xì)介紹,請(qǐng)參考它處。

1.創(chuàng)建Server端KeyStore文件serverKeys.jks,包含一個(gè)用于服務(wù)器的證書 :
引用
keytool -genkey -alias server -keysize 1024 -validity 3650 -keyalg RSA -dname "CN=sundoctor.com, OU=Developer,O=Techstar, L=Beijing, S=Beijing, C=CH" -keypass 123456 -storepass 123456 -keystore serverKeys.jks


2.創(chuàng)建Client端KeyStore文件clientKeys.jks,分別包含用于虛構(gòu)的通信者 Alice 和 Bob 的證書 :
引用
keytool -genkey -alias alice -keysize 1024 -validity 3650 -keyalg RSA -dname "CN=Aclie, OU=Developer,O=Techstar, L=Beijing, S=Beijing, C=CH" -keypass 123456 -storepass 123456 -keystore clientKeys.jks

keytool -genkey -alias bob -keysize 1024 -validity 3650 -keyalg RSA -dname "CN=Bob, OU=Developer,O=Techstar, L=Beijing, S=Beijing, C=CH" -keypass 123456 -storepass 123456 -keystore clientKeys.jks


其次重寫的 javax.net.ssl.X509TrustManager接口中的三個(gè)方法實(shí)現(xiàn)Server端和Client端信認(rèn)證書,代碼中BogusTrustManagerFactory類有關(guān)X509TrustManager實(shí)現(xiàn)的片斷,具體參考源碼
Java代碼
  1. static final X509TrustManager X509 = new X509TrustManager() {   
  2.   
  3.     /**  
  4.      * 確認(rèn)和信任將其用于基于身份驗(yàn)證類型的客戶端 SSL 身份驗(yàn)證  
  5.      */  
  6.     public void checkClientTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {   
  7.   
  8.         if (x509Certificates == null || x509Certificates.length == 0)   
  9.             throw new IllegalArgumentException("null or zero-length certificate chain");   
  10.         if (s == null || s.length() == 0)   
  11.             throw new IllegalArgumentException("null or zero-length authentication type");   
  12.   
  13.         boolean br = false;   
  14.         Principal principal = null;   
  15.         for (X509Certificate x509Certificate : x509Certificates) {   
  16.             principal = x509Certificate.getSubjectDN();   
  17.             if (principal != null && (StringUtils.contains(principal.getName(), "Alice") || StringUtils.contains(principal.getName(), "Bob"))) {   
  18.                 br = true;   
  19.                 return;   
  20.             }   
  21.         }   
  22.   
  23.         if (!br) {   
  24.             throw new CertificateException("連接認(rèn)證失??!");   
  25.         }   
  26.     }   
  27.   
  28.     /**  
  29.      * 確認(rèn)和信任將其用于基于身份驗(yàn)證類型的服務(wù)器 SSL 身份驗(yàn)證  
  30.      */  
  31.     public void checkServerTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {   
  32.         if (x509Certificates == null || x509Certificates.length == 0)   
  33.             throw new IllegalArgumentException("null or zero-length certificate chain");   
  34.         if (s == null || s.length() == 0)   
  35.             throw new IllegalArgumentException("null or zero-length authentication type");   
  36.   
  37.         boolean br = false;   
  38.         Principal principal = null;   
  39.         for (X509Certificate x509Certificate : x509Certificates) {   
  40.             principal = x509Certificate.getSubjectDN();   
  41.             if (principal != null && (StringUtils.contains(principal.getName(), "sundoctor.com"))) {   
  42.                 br = true;   
  43.                 return;   
  44.             }   
  45.         }   
  46.   
  47.         if (!br) {   
  48.             throw new CertificateException("連接認(rèn)證失?。?);   
  49.         }   
  50.     }   
  51.   
  52.     public X509Certificate[] getAcceptedIssuers() {   
  53.         return new X509Certificate[0];   
  54.     }   
  55. };  

在這里checkClientTrusted(X509Certificate[] x509Certificates, String s)驗(yàn)證客戶端證書,在這里只是簡單的驗(yàn)證一下客戶端證書CN是否為Alice或Bob, checkServerTrusted(X509Certificate[] x509Certificates, String s)驗(yàn)證服務(wù)端證書,這里只是簡單的驗(yàn)證一下服務(wù)端的CN是否為sundoctor.com。更復(fù)雜的驗(yàn)證,大家可以自行實(shí)現(xiàn)。

最后是創(chuàng)建服務(wù)端和客戶端SSLContext工廠類,分別初始化服務(wù)端和客戶端的SSLContext
Java代碼
  1. // Initialize the SSLContext to work with our key managers.   
  2. SSLContext sslContext = SSLContext.getInstance(PROTOCOL);   
  3. sslContext.init(getKeyManagers(serverKeys, serverKeysPassword), BogusTrustManagerFactory.X509_MANAGERS, null);  

初始化SSLContext需要KeyManagers和TrustManager,TrustManager參見BogusTrustManagerFactory,使用serverKeys.jks、clientKeys.jks分別構(gòu)建服務(wù)端和客戶端KeyManagers
Java代碼
  1. private static KeyManager[] getKeyManagers(String keysfile, String password) throws GeneralSecurityException,   
  2.         IOException {   
  3.   
  4.     // First, get the default KeyManagerFactory.   
  5.     KeyManagerFactory kmf = KeyManagerFactory.getInstance(KEY_MANAGER_FACTORY_ALGORITHM);   
  6.        
  7.     // Next, set up the TrustStore to use. We need to load the file into   
  8.     // a KeyStore instance.    
  9.     KeyStore ks = KeyStore.getInstance("JKS");   
  10.     InputStream in = BogusSslContextFactory.class.getResourceAsStream(keysfile);   
  11.     ks.load(in, password.toCharArray());   
  12.     in.close();   
  13.   
  14.     // Now we initialise the KeyManagerFactory with this KeyStore      
  15.     kmf.init(ks, password.toCharArray());   
  16.   
  17.     // And now get the TrustManagers   
  18.     return kmf.getKeyManagers();   
  19. }  


服務(wù)端(TLSServer)和客戶端(TLSClient)測試代碼比較簡單,具體請(qǐng)參考源碼,在這里有一點(diǎn)需要注意的就是在服務(wù)端添加加密過濾器 SslFilter時(shí)必須設(shè)置為:sslFilter.setNeedClientAuth(true),需要驗(yàn)證客戶端證書。
本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
【Android_網(wǎng)絡(luò)編程 HTTPS與SSL通信
Java網(wǎng)絡(luò)編程-用SSL構(gòu)建安全的Socket - moonsee - JavaEye技...
TLS/SSL Socket 實(shí)現(xiàn) - sariel - JavaEye技術(shù)網(wǎng)站
Android開發(fā)?實(shí)現(xiàn)SSL握手
瀏覽器 HTTPS 協(xié)議的相關(guān)知識(shí)點(diǎn)有哪些?
Akka-CQRS(13)- SSL/TLS for gRPC and HTTPS:自簽名證書產(chǎn)生和使用
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服