<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    itVincent Blog - Java Working Fun!

    技術(shù)引領(lǐng)時(shí)代!
    posts - 117, comments - 181, trackbacks - 0, articles - 12
    轉(zhuǎn)自http://hi.baidu.com/_fan/blog/item/70cc8e3896e0a4c9d46225fa.html

    tomcat6配置雙向認(rèn)證

    1、生成服務(wù)器端證書
    Java代碼
    keytool -genkey -keyalg RSA -dname "cn=localhost,ou=sango,o=none,l=china,st=beijing,c=cn" -alias server -keypass password -keystore server.jks -storepass password -validity 3650

    keytool -genkey -keyalg RSA -dname "cn=localhost,ou=sango,o=none,l=china,st=beijing,c=cn" -alias server -keypass password -keystore server.jks -storepass password -validity 3650
    2、生成客戶端證書
    Java代碼
    keytool -genkey -keyalg RSA -dname "cn=sango,ou=sango,o=none,l=china,st=beijing,c=cn" -alias custom -storetype PKCS12 -keypass password -keystore custom.p12 -storepass password -validity 3650

    keytool -genkey -keyalg RSA -dname "cn=sango,ou=sango,o=none,l=china,st=beijing,c=cn" -alias custom -storetype PKCS12 -keypass password -keystore custom.p12 -storepass password -validity 3650
    客戶端的CN可以是任意值。
    3、由于是雙向SSL認(rèn)證,服務(wù)器必須要信任客戶端證書,因此,必須把客戶端證書添加為服務(wù)器的信任認(rèn)證。由于不能直接將PKCS12格式的證書庫(kù)導(dǎo)入,我們必須先把客戶端證書導(dǎo)出為一個(gè)單獨(dú)的CER文件,使用如下命令,先把客戶端證書導(dǎo)出為一個(gè)單獨(dú)的cer文件:
    Java代碼
    keytool -export -alias custom -file custom.cer -keystore custom.p12 -storepass password -storetype PKCS12 -rfc

    keytool -export -alias custom -file custom.cer -keystore custom.p12 -storepass password -storetype PKCS12 -rfc
    然后,添加客戶端證書到服務(wù)器中(將已簽名數(shù)字證書導(dǎo)入密鑰庫(kù))
    Java代碼
    keytool -import -v -alias custom -file custom.cer -keystore server.jks -storepass password

    keytool -import -v -alias custom -file custom.cer -keystore server.jks -storepass password
    4、查看證書內(nèi)容
    Java代碼
    keytool -list -v -keystore server.jks -storepass password

    keytool -list -v -keystore server.jks -storepass password
    5、配置tomcat service.xml文件
    Xml代碼
    <Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true"
        maxThreads="150" scheme="https" secure="true"
        clientAuth="true" sslProtocol="TLS"
        keystoreFile="D:/server.jks" keystorePass="password"
        truststoreFile="D:/server.jks" truststorePass="password"
    />

    <Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true"
        maxThreads="150" scheme="https" secure="true"
        clientAuth="true" sslProtocol="TLS"
        keystoreFile="D:/server.jks" keystorePass="password"
        truststoreFile="D:/server.jks" truststorePass="password"
    />
    clientAuth="true"表示雙向認(rèn)證
    6、導(dǎo)入客戶端證書到瀏覽器
    雙向認(rèn)證需要強(qiáng)制驗(yàn)證客戶端證書。雙擊“custom.p12”即可將證書導(dǎo)入至IE

    7、java代碼實(shí)現(xiàn)

    DefaultHttpClient httpclient = new DefaultHttpClient();

            KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());  
            FileInputStream instream = new FileInputStream(new File("D:/server.jks"));
            try {
                trustStore.load(instream, "password".toCharArray());
            } finally {
                instream.close();
            }
           
            SSLSocketFactory socketFactory = new SSLSocketFactory(trustStore,"password",trustStore);
            Scheme sch = new Scheme("https", socketFactory, 443);
            httpclient.getConnectionManager().getSchemeRegistry().register(sch);

            HttpGet httpget = new HttpGet("https://localhost:8443/");

            System.out.println("executing request" + httpget.getRequestLine());
           
            HttpResponse response = httpclient.execute(httpget);
            HttpEntity entity = response.getEntity();

            System.out.println("----------------------------------------");
            System.out.println(response.getStatusLine());
            if (entity != null) {
                System.out.println("Response content length: " + entity.getContentLength());
            }
            if (entity != null) {
                entity.consumeContent();
            }
            httpclient.getConnectionManager().shutdown();       

    Feedback

    # re: 【轉(zhuǎn)】使用Httpclient實(shí)現(xiàn)SSL雙向認(rèn)證  回復(fù)  更多評(píng)論   

    2012-04-19 18:21 by 你爺爺
    請(qǐng)你寫東西的時(shí)候別這么操 蛋好吧,一段程序引用的包不知道是那種操蛋包

    # re: 【轉(zhuǎn)】使用Httpclient實(shí)現(xiàn)SSL雙向認(rèn)證  回復(fù)  更多評(píng)論   

    2012-12-25 16:50 by 頂樓上
    真操蛋,弄了半天都不知道是啥包

    # re: 【轉(zhuǎn)】使用Httpclient實(shí)現(xiàn)SSL雙向認(rèn)證  回復(fù)  更多評(píng)論   

    2015-08-19 16:34 by hadeed
    明顯是org.apache.http.client包,
    org.apache.commons.httpclient.HttpClient獲取連接管理是getHttpConnectionManager()方法,執(zhí)行請(qǐng)求是excuteMethod()方法。
    HttpGet,HttpEntity也很明顯是org.apache.http.client里的類

    # re: 【轉(zhuǎn)】使用Httpclient實(shí)現(xiàn)SSL雙向認(rèn)證  回復(fù)  更多評(píng)論   

    2015-08-19 17:04 by hadeed
    還有SSLSocketFacktory,其中一個(gè)(javax.net.ssl)是抽象類不能初始化對(duì)象。ps:
    這段代碼和微信支付證書的示例代碼如出一轍(sysout的分隔線都一樣==)

    只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


    網(wǎng)站導(dǎo)航:
     
    主站蜘蛛池模板: 久久久久免费看黄a级试看| 久久久久久影院久久久久免费精品国产小说| 99re6在线精品视频免费播放| 中文亚洲AV片不卡在线观看| 99久久亚洲精品无码毛片 | 国产精品观看在线亚洲人成网| 成人毛片免费播放| 国产亚洲欧美在线观看| 成人永久福利免费观看| 免费又黄又爽又猛大片午夜| 亚洲人成电影网站国产精品 | 久久精品国产亚洲Aⅴ香蕉 | 全部免费毛片在线| 黄网站色成年片大免费高清| 亚洲欧洲久久av| 久久久久久AV无码免费网站下载| 五月天网站亚洲小说| 一个人免费观看在线视频www| 亚洲色偷精品一区二区三区| 少妇人妻偷人精品免费视频| 亚洲精品国产福利片| 最近免费中文字幕视频高清在线看 | 亚洲精品视频免费看| 亚洲入口无毒网址你懂的| 特级淫片国产免费高清视频| 日亚毛片免费乱码不卡一区| 久久亚洲国产精品一区二区| 亚洲国产精品免费观看| 亚洲高清美女一区二区三区| 成年免费大片黄在线观看岛国| 亚洲av无码偷拍在线观看| 久久亚洲精品无码播放| 免费观看黄色的网站| 一级毛片高清免费播放| 中文字幕亚洲综合久久| 在线视频免费国产成人| 性xxxxx大片免费视频| 亚洲精品天堂成人片AV在线播放| 亚洲中文字幕无码不卡电影| 国产a视频精品免费观看| 色多多A级毛片免费看|