<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!

    技術引領時代!
    posts - 117, comments - 181, trackbacks - 0, articles - 12
    轉自http://hi.baidu.com/_fan/blog/item/70cc8e3896e0a4c9d46225fa.html

    tomcat6配置雙向認證

    1、生成服務器端證書
    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認證,服務器必須要信任客戶端證書,因此,必須把客戶端證書添加為服務器的信任認證。由于不能直接將PKCS12格式的證書庫導入,我們必須先把客戶端證書導出為一個單獨的CER文件,使用如下命令,先把客戶端證書導出為一個單獨的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
    然后,添加客戶端證書到服務器中(將已簽名數字證書導入密鑰庫)
    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、查看證書內容
    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"表示雙向認證
    6、導入客戶端證書到瀏覽器
    雙向認證需要強制驗證客戶端證書。雙擊“custom.p12”即可將證書導入至IE

    7、java代碼實現

    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: 【轉】使用Httpclient實現SSL雙向認證  回復  更多評論   

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

    # re: 【轉】使用Httpclient實現SSL雙向認證  回復  更多評論   

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

    # re: 【轉】使用Httpclient實現SSL雙向認證  回復  更多評論   

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

    # re: 【轉】使用Httpclient實現SSL雙向認證  回復  更多評論   

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

    只有注冊用戶登錄后才能發表評論。


    網站導航:
     
    主站蜘蛛池模板: 亚洲日本精品一区二区| 亚洲黄黄黄网站在线观看| 亚洲毛片αv无线播放一区| 日韩精品无码免费视频| 婷婷亚洲天堂影院| 亚洲av无码成人影院一区| 亚洲成av人片天堂网| 亚洲精品不卡视频| 99re6免费视频| 亚洲综合激情视频| 国产免费AV片在线播放唯爱网| 亚洲综合亚洲国产尤物| 亚洲成人免费电影| 亚洲狠狠成人综合网| 日本不卡免费新一二三区| 成人午夜免费视频| 亚洲五月综合缴情在线观看| 免费国产成人18在线观看| 亚洲黄色三级视频| 最近免费中文字幕视频高清在线看 | 亚洲成av人片在线看片| 18禁免费无码无遮挡不卡网站 | 三年片在线观看免费大全电影| 亚洲今日精彩视频| 四虎永久在线观看免费网站网址| 色老板亚洲视频免在线观| 国产一区二区三区在线免费观看| 免费一级特黄特色大片| 精品国产_亚洲人成在线高清| 18禁美女裸体免费网站| 亚洲一区二区三区免费在线观看| 浮力影院第一页小视频国产在线观看免费| 精品久久久久久亚洲中文字幕| 亚洲综合久久夜AV | 三年片在线观看免费大全电影 | 久久精品国产精品亚洲| 久9热免费精品视频在线观看| 美女视频黄免费亚洲| 国产a v无码专区亚洲av| **真实毛片免费观看| 午夜在线亚洲男人午在线|